1.安裝
http://getcomposer.org/doc/00-intro.md$ curl -sS https://getcomposer.org/installer | php
2.使用
http://getcomposer.org/doc/03-cli.mdCommand-line interface
init
In the
Libraries chapter we looked at how to create a
composer.json by hand. There is also an
initcommand available that makes it a bit easier to do this.
When you run the command it will interactively ask you to fill in the fields, while using some smart defaults.
$ php composer.phar init
install
The install command reads the composer.json file from the current directory, resolves the dependencies, and installs them into vendor.
$ php composer.phar install
If there is a composer.lock file in the current directory, it will use the exact versions from there instead of resolving them. This ensures that everyone using the library will get the same versions of the dependencies.
If there is no composer.lock file, composer will create one after dependency resolution.
update
In order to get the latest versions of the dependencies and to update the composer.lock file, you should use theupdate command.
$ php composer.phar update
This will resolve all dependencies of the project and write the exact versions into composer.lock.
If you just want to update a few packages and not all, you can list them as such:
$ php composer.phar update vendor/package vendor/package2
You can also use wildcards to update a bunch of packages at once:
$ php composer.phar update vendor/*
require
The require command adds new packages to the composer.json file from the current directory.
$ php composer.phar require
After adding/changing the requirements, the modified requirements will be installed or updated.
If you do not want to choose requirements interactively, you can just pass them to the command.
$ php composer.phar require vendor/package:2.* vendor/package2:dev-master
global
The global command allows you to run other commands like
install,
require or
update as if you were running them from the
COMPOSER_HOME directory.
This can be used to install CLI utilities globally and if you add $COMPOSER_HOME/vendor/bin to your $PATH environment variable. Here is an example:
$ php composer.phar global require fabpot/php-cs-fixer:dev-master
Now the php-cs-fixer binary is available globally (assuming you adjusted your PATH). If you wish to update the binary later on you can just run a global update:
$ php composer.phar global update
search
The search command allows you to search through the current project's package repositories. Usually this will be just packagist. You simply pass it the terms you want to search for.
$ php composer.phar search monolog
You can also search for more than one term by passing multiple arguments.
show
To list all of the available packages, you can use the show command.
$ php composer.phar show
If you want to see the details of a certain package, you can pass the package name.
$ php composer.phar show monolog/monolog
name : monolog/monolog
versions : master-dev, 1.0.2, 1.0.1, 1.0.0, 1.0.0-RC1
type : library
names : monolog/monolog
source : [git] http://github.com/Seldaek/monolog.git 3d4e60d0cbc4b888fe5ad223d77964428b1978da
dist : [zip] http://github.com/Seldaek/monolog/zipball/3d4e60d0cbc4b888fe5ad223d77964428b1978da 3d4e60d0cbc4b888fe5ad223d77964428b1978da
license : MIT
autoload
psr-0
Monolog : src/
requires
php >=5.3.0
You can even pass the package version, which will tell you the details of that specific version.
$ php composer.phar show monolog/monolog 1.0.2
depends
The depends command tells you which other packages depend on a certain package. You can specify which link types (require, require-dev) should be included in the listing. By default both are used.
$ php composer.phar depends --link-type=require monolog/monolog
nrk/monolog-fluent
poc/poc
propel/propel
symfony/monolog-bridge
symfony/symfony
validate
You should always run the validate command before you commit your composer.json file, and before you tag a release. It will check if your composer.json is valid.
$ php composer.phar validate
status
If you often need to modify the code of your dependencies and they are installed from source, the statuscommand allows you to check if you have local changes in any of them.
$ php composer.phar status
With the --verbose option you get some more information about what was changed:
$ php composer.phar status -v
You have changes in the following dependencies:
vendor/seld/jsonlint:
M README.mdown
self-update
To update composer itself to the latest version, just run the self-update command. It will replace your composer.phar with the latest version.
$ php composer.phar self-update
If you would like to instead update to a specific release simply specify it:
$ composer self-update 1.0.0-alpha7
If you have installed composer for your entire system (see
global installation), you may have to run the command with
root privileges
$ sudo composer self-update