This article mainly introduces the five tips that PHP developers should know about. For more information, see the next article. it mainly introduces the five tips that PHP developers should know about, for more information, see
Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be viewed in the new age of Composer PHP dependency management. This article describes five tips for using Composer, hoping to facilitate PHP development.
1. update only a single database
You only want to update a specific library and do not want to update all its dependencies. this is simple:
composer update foo/bar
In addition, this technique can be used to solve the "warning information problem ". You must have seen the following warning information:
Warning: The lock file is not up to date with the latest changes in composer.json, you may be getting outdated dependencies, run update to update them.
Why? Don't panic! If you have editedcomposer.json
, You should see such information. For example, if you add or update detailed information, such as the Library description, author, more parameters, or even add only one space, the md5sum of the file will be changed. Then Composer will warn you about the hash value andcomposer.lock
.
So what should we do?update
The command can update the lock file, but if only some descriptions are added, it is not intended to update any library. In this caseupdate nothing
:
$ composer update nothingLoading composer repositories with package information Updating dependencies Nothing to install or update Writing lock file Generating autoload files
In this way, Composer will not update the library, but will updatecomposer.lock
. Note:nothing
Notupdate
Command keyword. Just nonothing
Result of this package. If you enterfoobar
And the results are the same.
If the Composer version you are using is new enough, you can directly use--lock
Option:
composer update --lock
2. do not edit
composer.json
In the case
You may feel that every time you install a library, you need to modify it.composer.json
It is too troublesome, so you can directly userequire
Command.
composer require "foo/bar:1.0.0"
This method can also be used to quickly create a new project.init
Command has--require
Option, which can be automatically writtencomposer.json
:( Note that we use-n
In this way, you do not need to answer questions)
$ composer init --require=foo/bar:1.0.0 -n$ cat composer.json{ "require": { "foo/bar": "1.0.0" }}
3. easy to derive
During initialization, you triedcreate-project
Command?
composer create-project doctrine/orm path 2.2.0
This will automatically clone the repository and check out the specified version. You can use this command to clone a database without searching for the original URI.
4. consider caching,
dist
Package priority
In the last year, the Composer will automatically archive your downloadeddist
Package. By default,dist
The package is used to add the tag version, for example"symfony/symfony": "v2.1.4"
, Or a wildcard or version range,"2.1.*"
Or">=2.2,<2.3-dev"
(If you usestable
As yourminimum-stability
).
The dist package can also be useddev-master
Github allows you to download a compressed package referenced by git. You can useinstall
Andupdate
Of--prefer-dist
.
The following is an example (I used--profile
To display the execution time ):
$ composer init --require="twig/twig:1.*" -n --profileMemory usage: 3.94MB (peak: 4.08MB), time: 0s$ composer install --profileLoading composer repositories with package information Installing dependencies - Installing twig/twig (v1.12.2) Downloading: 100%Writing lock file Generating autoload files Memory usage: 10.13MB (peak: 12.65MB), time: 4.71s$ rm -rf vendor$ composer install --profileLoading composer repositories with package information Installing dependencies from lock file - Installing twig/twig (v1.12.2) Loading from cacheGenerating autoload files Memory usage: 4.96MB (peak: 5.57MB), time: 0.45s
Here,twig/twig:1.12.2
The compressed package is saved in~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip
. Use it directly when you reinstall the installation package.
5. if you want to modify the source code, the source code is preferred.
When you need to modify the library, it is easier to clone the source code than to download the package. You can use--prefer-source
To forcibly select the clone source code.
composer update symfony/yaml --prefer-source
Next you can modify the file:
composer status -v You have 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 modification:
$ composer updateLoading composer repositories with package information Updating 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,?]?
Prepare the production environment
Finally, we will remind you not to forget to optimize automatic loading when deploying code to the production environment:
composer dump-autoload --optimize
The installation package can be used in the same way.--optimize-autoloader
. Without this option, you may find 20% to 25% performance loss.
If you need help or want to know the details of a command, you can read the official or Chinese documents, or view the notebook memo prepared by JoliCode.
Original article Address: 5 features to know about Composer PHP
PHP developers should know five tips on Composer
Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be viewed in the new age of Composer PHP dependency management. This article describes five tips for using Composer, hoping to facilitate PHP development.
1. update only a single database
You only want to update a specific library and do not want to update all its dependencies. this is simple:
composer update foo/bar
In addition, this technique can be used to solve the "warning information problem ". You must have seen the following warning information:
Warning: The lock file is not up to date with the latest changes in composer.json, you may be getting outdated dependencies, run update to update them.
Why? Don't panic! If you have editedcomposer.json
, You should see such information. For example, if you add or update detailed information, such as the Library description, author, more parameters, or even add only one space, the md5sum of the file will be changed. Then Composer will warn you about the hash value andcomposer.lock
.
So what should we do?update
The command can update the lock file, but if only some descriptions are added, it is not intended to update any library. In this caseupdate nothing
:
$ composer update nothingLoading composer repositories with package information Updating dependencies Nothing to install or update Writing lock file Generating autoload files
In this way, Composer will not update the library, but will updatecomposer.lock
. Note:nothing
Notupdate
Command keyword. Just nonothing
Result of this package. If you enterfoobar
And the results are the same.
If the Composer version you are using is new enough, you can directly use--lock
Option:
composer update --lock
2. do not edit
composer.json
In the case
You may feel that every time you install a library, you need to modify it.composer.json
It is too troublesome, so you can directly userequire
Command.
composer require "foo/bar:1.0.0"
This method can also be used to quickly create a new project.init
Command has--require
Option, which can be automatically writtencomposer.json
:( Note that we use-n
In this way, you do not need to answer questions)
$ composer init --require=foo/bar:1.0.0 -n$ cat composer.json{ "require": { "foo/bar": "1.0.0" }}
3. easy to derive
During initialization, you triedcreate-project
Command?
composer create-project doctrine/orm path 2.2.0
This will automatically clone the repository and check out the specified version. You can use this command to clone a database without searching for the original URI.
4. consider caching,
dist
Package priority
In the last year, the Composer will automatically archive your downloadeddist
Package. By default,dist
The package is used to add the tag version, for example"symfony/symfony": "v2.1.4"
, Or a wildcard or version range,"2.1.*"
Or">=2.2,<2.3-dev"
(If you usestable
As yourminimum-stability
).
The dist package can also be useddev-master
Github allows you to download a compressed package referenced by git. You can useinstall
Andupdate
Of--prefer-dist
.
The following is an example (I used--profile
To display the execution time ):
$ composer init --require="twig/twig:1.*" -n --profileMemory usage: 3.94MB (peak: 4.08MB), time: 0s$ composer install --profileLoading composer repositories with package information Installing dependencies - Installing twig/twig (v1.12.2) Downloading: 100%Writing lock file Generating autoload files Memory usage: 10.13MB (peak: 12.65MB), time: 4.71s$ rm -rf vendor$ composer install --profileLoading composer repositories with package information Installing dependencies from lock file - Installing twig/twig (v1.12.2) Loading from cacheGenerating autoload files Memory usage: 4.96MB (peak: 5.57MB), time: 0.45s
Here,twig/twig:1.12.2
The compressed package is saved in~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip
. Use it directly when you reinstall the installation package.
5. if you want to modify the source code, the source code is preferred.
When you need to modify the library, it is easier to clone the source code than to download the package. You can use--prefer-source
To forcibly select the clone source code.
composer update symfony/yaml --prefer-source
Next you can modify the file:
composer status -v You have 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 modification:
$ composer updateLoading composer repositories with package information Updating 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,?]?
Prepare the production environment
Finally, we will remind you not to forget to optimize automatic loading when deploying code to the production environment:
composer dump-autoload --optimize
The installation package can be used in the same way.--optimize-autoloader
. Without this option, you may find 20% to 25% performance loss.
If you need help or want to know the details of a command, you can read the official or Chinese documents, or view the notebook memo prepared by JoliCode.