From http://blog.csdn.net/zzulp/article/details/18981029
Composer is a project-based dependency manager that installs the packages or libraries that PHP projects depend on in the directory of the project. By default, composer does not install any data globally. It is used to replace the previously used pear tool.
1. Install composer
[Plain]View plaincopyprint?
- Curl-SS https://getcomposer.org/installer | PHP
- MV composer. phar/usr/local/bin/Composer
2. Simple Application
We only need to declare the packages or libraries that a project depends on, and then other things will be handled by composer. Therefore, the most important thing to use composer is to compile the composer. JSON configuration file in the project directory. For example, if you have a project that depends on monolog, you can write the composer. JSON file as follows:
{
"Require ":
{
"Monolog/monolog": "1. 2 .*"
}
}
With this file, execute composer install in the project directory to create the dependent libraries under the vendor/directory of the project directory.
On the other hand, a directory containing composer. JSON can also be released to allow others to install it through composer. You only need to upload this package to the packagelist website.
3. configuration file description
Composer uses JSON as the configuration file format. The package defined by composer. JSON under the root directory of the project is called the root package. The configuration file has the following attributes.
3.1 require
Format: "require": {"vendor-name/package-name": "version ",...}
The name part is created as the path under the vendor.
The exact version number is supported, and the range is greater than or equal to 1.0;> = 1.0, <2.0; "," as the logic and, and "! "As logic or meaning. Wildcard characters are used in the example *
The version also supports tag or branch names.
Similar to require-Dev, the former is used to declare the dependent package of the Project release version, and the latter is used to declare the dependent package in the project development or test.
3.2 autoload
Composer supports PSR-0, PSR-4, classmap, and files inclusion to support automatic file loading. PSR-4 is recommended.
3.2.1 files type
Format: "autoload": {"Files": ["path/to/1.php"," path/to/2.php",...]}
Supports automatic loading of files in the array. The file path is relative to the project root directory. The disadvantage is that it is troublesome to write all files into the configuration.
3.2.2 classmap type
Format: "autoload": {"classmap": ["path/to/src1", "path/to/src2",...]}
Supports automatic loading of files in the path in the array. It is very convenient, but the disadvantage is that once a new file is added, You need to execute the dump-autoload command to regenerate the Vening file vendor/Composer/autoload_classmap.php.
3.2.3 psr-0 type
Formats: "autoload": {"psr-0 ":{
"Name1 \ Space \": ["path/",...],
"Name2 \ Space \": ["path2/",...],
}
}
You can map namespaces to paths. \ At the end of the namespace cannot be omitted. When install or update is executed, the loading information will be written to the file vendor/Composer/autoload_namespace.php. If you want to resolve all namespaces in the specified path, set the namespace to a null string.
Note that the path of the class file corresponding to the name2 \ Space \ Foo class is path2/name2/SPACE/Foo. php.
3.2.4 psr-4 type
Formats: "autoload": {"psr-4 ":{
"Name1 \ Space \": ["path/",...],
"Name2 \ Space \": ["path2/",...],
}
}
You can map namespaces to paths. \ At the end of the namespace cannot be omitted. When install or update is executed, the loading information is written to the file vendor/Composer/autoload_psr4.php. If you want to resolve all namespaces in the specified path, set the namespace to a null string.
Note that the path of the class file corresponding to the name2 \ Space \ Foo class is path2/SPACE/Foo. php, and name2 cannot appear in the path.
The biggest difference between PSR-4 and PSR-0 is the definition of underscore. In a PSR-4, the use of underscores in the class name has no special meaning. The PSR-0 specifies that the underline _ in the class name will be converted into a directory separator.
3.3 name
Format: "name": "vendor/package"
To publish a package, you must specify the package name.
3.4 version
Format: "version": "1.0.2"
To release a package, you must specify the version number of the package. The version number is in the format of x.y. Z or vx. y.z. You can add a suffix such as-Dev,-patch,-Alpha,-Beta, or-RC. In addition to Dev, you can add a number to the end, such as 1.0.0-alpha3.
3.5 description
Format: "Description": "Your own description at here! "
To publish a package, you can specify a brief introduction.
3.5 type
Format: "type": "library"
Description of the package type. The following libraries, projects, metapackage, and composer-plugin are supported. The default value is library.
3.6 keywords
Format: "keywords": ["logging", "Database", "redis"]
An array keyword used for search or filtering.
3.7 Homepage
Optional, indicating the project's website address
3.8 time/license
Description project time and license, in the format of YY-MM-DD hh: mm: SS
3.9 authors
Format: "Authors ":[
{"Name": "SS", "email": "[email protected]", "Homepage": "", "role ":""},...
]
Description of the Project author. Optional.
3.10 support
Format: "support": {"emial": "", "Issues": "", "Forum": "", "Wiki": "", "irc ": ""}
Used to describe Project Support Information
3.11 conflict
It is used to declare the version of a package that conflicts with this package, and is similar to require.
3.12 replace
Used to declare the package to be replaced.
3.13 provided
This package implements the interface of a certain package.
3.14 suggest
Format: "suggest": {"vendor/package": "Some description! "}
Optional Description: the package and description for enhancement.
4. command line tool
With the configuration file, you can use the composer command to execute the following commands:
Init enters the interactive wizard and generates the composer. JSON file in the current directory.
Install install according to configuration
Option -- prefer-Source
-- Prefer-Dist
-- Dry-run
-- DEV/-- no-Dev
-O converts psr0/4 to classmap to improve performance
Update [Package-name...] upgrade the specified package according to the configuration
Search [pattern] search package from packagelist
Show [Package-name] displays package information
Dependencies in the depends package analysis project
Validate Configuration File Content
Status check code is to modify Information
Self-Update: Upgrade composer itself
Config to configure the local or global options of composer. You can -- list to display all the configurable configuration items in the format of conifg [Option] [Key] [value...]
Create-Project vendor/package path version: Download a project in the specified path. By default, the project is searched from packagelist.
Dump-autoload: used to update the loader when a new class is added to the class ing.
5. Automatic Loading
For composer management dependencies, If You Want To automatically load, you only need to add: require 'vendor/autoload. php' to the file'
Taking the above example, the dependent class is very simple. You can directly access it using packet-name \ class-name. For example
$ Log = new monolog \ logger ();
6 Miscellaneous
The composer. Lock file is used to record the versions of each dependency. If VCs is used, this file also needs to be submitted. If the file exists, the composer downloads the file according to the version in the file and ignores the composer. JSON file. To update the version, modify composer. JSON and run the update command. The new version is downloaded and the composer. Lock file is updated.