Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be seen in this "new era of Composer PHP dependency management." This article describes the use of composer five tips, hoping to give you the convenience of PHP development.
1. Update only a single library
Just to update a particular library and not to update all its dependencies, it's simple:
Composer Update Foo/bar
In addition, this technique can be used to solve "warning message problems". You must have seen this warning message:
Warning:the lock file is not up to date with the latest changes in Composer.json, you may be getting outdated dependencie s, run update to update them.
Rub, what's wrong? Don't Panic! If you edit it composer.json
, you should see this information. For example, if you add or update details, such as the library's description, the author, more parameters, or even just a single space, it will change the md5sum of the file. Then composer will warn you that the hash value composer.lock
differs from the one documented in.
So what do we do? update
command to update the lock file, but if you add only a few descriptions, you should not want to update any libraries. In this case, just update nothing
:
$ composer Update Nothingloading composer repositories with package informationupdating dependenciesnothing to install or updatewriting Lock filegenerating autoload files
This way, composer does not update the library, but it is updated composer.lock
. Note nothing
is not update
a keyword for the command. Just without nothing
the result of this package. If you type foobar
, the result is the same.
If your composer version is new enough, you can use the option directly --lock
:
Composer Update--lock
2. Do not edit
composer.json
Install the library in case of
You may feel that every installation of a library needs to be modified composer.json
too much, so you can use the require
command directly.
Composer require "foo/bar:1.0.0"
This method can also be used to quickly open a new project. init
commands have --require
options that can be automatically written composer.json
: (Note we use -n
, so you don't have to answer questions)
$ composer Init--require=foo/bar:1.0.0-n$ cat composer.json{ "require" : { "foo/bar""1.0.0" }}
3. Derivation is easy
Did you try the command when it was initialized create-project
?
Composer Create-project Doctrine/orm Path 2.2.0
This will automatically clone the warehouse and check out the specified version. Cloning the library with this command is very convenient, do not need to search the original URI.
4. Consider caching, Dist packet first
The composer of the last year will automatically archive the dist package you downloaded. By default, the dist package is used to add a tag version, for example "symfony/symfony": "v2.1.4"
, a wildcard or version interval, "2.1.*"
or ">=2.2,<2.3-dev"
(if you are using stable
as your minimum-stability
.
The dist package can also be used for branches such as dev-master
GitHub, which allows you to download a compressed package from a git reference. In order to force the use of the compressed package instead of cloning the source code, you can use install
update
the and --prefer-dist
options.
Here's an example (I used the --profile
option to show the execution time):
$ composer Init--require= "twig/twig:1.*"-N--profilememory usage:3.94mb (PEAK:4.08MB), time:0s$ composer Install--PR Ofileloading composer repositories with package informationinstalling dependencies -Installing Twig/twig (v1.12.2) downloading:100%writing lock filegenerating autoload filesmemory usage:10.13mb (PEAK:12.65MB), time:4.71s$ RM-RF vendor$ Composer Install--profileloading composer repositories with package informationinstalling dependencies from lock File -Installing Twig/twig (v1.12.2) Loading from cachegenerating autoload filesmemory usage:4.96mb (peak:5.57 MB), time:0.45s
Here, twig/twig:1.12.2
the compression package is saved in the ~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip
. Used directly when reinstalling packages.
5. Consider the changes, the source code first
When you need to modify the library, cloning the source code is more convenient than downloading the package. You can use it --prefer-source
to force the selection of cloned source code.
Composer Update Symfony/yaml--prefer-source
Next you can modify the file:
Composer Status-vyou has changes in the following dependencies:/path/to/app/vendor/symfony/yaml/symfony/component/ Yaml: M dumper.php
When you try to update a modified library, composer will remind you to ask if you want to discard the changes:
$ composer Updateloading composer repositories with package informationupdating dependencies -Updating symfony/ Symfony v2.2.0 (v2.2.0-= v2.2.0) The package has modified files: M dumper.php Discard changes [Y,n,v,s,? ]?
Preparing for the production environment
Finally, when deploying the code to the production environment, don't forget to optimize the automatic loading:
Composer Dump-autoload--optimize
The same can be used when installing packages --optimize-autoloader
. Without this option, you may find a performance penalty of 20% to 25%.
If you need help, or want to know the details of a command, you can read the official documentation or check out this interactive cheat sheet from Jolicode.
Reprinted from: 1190000000355928
"Reprint" PHP developers should know the 5 Composer tips