Composer is a new generation of PHP-dependent 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, I hope to be able to bring your PHP development convenience.
1. Update only a single library
Just want to update a particular library, do not want to update all its dependencies, very simple:
In addition, this technique can also 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 dependencies, run update to update them.
Rub, what's wrong? Don't Panic! If you edit it composer.json
, you should see such a message. For example, if you add or update details, such as library descriptions, authors, more parameters, or even just a single space, you will change the md5sum of the file. Then composer will warn you of the difference between the hash value and the composer.lock
record.
So what do we do? update
command can update the lock file, but if you add only a few more descriptions, you should not be updating any libraries. In this case, just update nothing
:
$ composer Update nothing
Loading composer repositories with package
information updating dependencies Nothing to install or update
writing lock file
generating autoload files
As a result, composer does not update the library, but updates it composer.lock
. Note that nothing
is not update
a keyword of the command. Just not nothing
The result of this package. If you enter foobar
, the result is the same.
If you use a composer version that is new enough, you can use the --lock
option directly:
2. composer.json
Install the library without editing
You may feel that each installation of a library needs to be changed composer.json
too cumbersome, 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 written automatically composer.json
: (Note that we use -n
them so that we 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 to order the initialization create-project
?
Composer Create-project Doctrine/orm Path 2.2.0
This automatically clones the warehouse and checks out the specified version. This command is handy when cloning a library, and you don't need to search for the original URI.
4. Consider caching, dist
package priority
The last year's composer will automatically archive your downloaded dist
packages. By default, the dist
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 use it stable
as your minimum-stability
).
Dist packages can also be used for branches such as dev-master
, GitHub allows you to download a package that a git references. In order to force the use of compression packs instead of cloning the source code, you can use install
update
the and --prefer-dist
options.
Here is an example (I used the --profile
option to display the execution time):
$ composer Init--require= "twig/twig:1.*"-N--profile
Memory usage:3.94mb (PEAK:4.08MB), time:0s
$ composer in Stall--profile
Loading composer repositories with package information installing
- 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--profile Loading composer
R Epositories with package information
installing dependencies from lock file
-Installing Twig/twig (v1.12.2) C16/>loading from cache
generating autoload files
Memory usage:4.96mb (PEAK:5.57MB), time:0.45s
Here, twig/twig:1.12.2
the compressed package is saved in the ~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip
. Use directly when reinstalling the package.
5. To modify, the source code first
When you need to modify the library, cloning the source code is more convenient than the download package. You can use --prefer-source
to force the selection of 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 changes:
$ composer Update
Loading 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,?]?
Preparing for the production environment
Finally, when you deploy your code to a production environment, don't forget to optimize for automatic loading:
Composer Dump-autoload--optimize
The same can be used when installing packages --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 particular command, you can read the official document or the Chinese document, or you can view the interactive memo made by Jolicode.
Original address: 5 features to know about Composer PHP
Address: 5 Composer tips that PHP developers should know
Composer is a new generation of PHP-dependent 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, I hope to be able to bring your PHP development convenience.
1. Update only a single library
Just want to update a particular library, do not want to update all its dependencies, very simple:
In addition, this technique can also 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 dependencies, run update to update them.
Rub, what's wrong? Don't Panic! If you edit it composer.json
, you should see such a message. For example, if you add or update details, such as library descriptions, authors, more parameters, or even just a single space, you will change the md5sum of the file. Then composer will warn you of the difference between the hash value and the composer.lock
record.
So what do we do? update
command can update the lock file, but if you add only a few more descriptions, you should not be updating any libraries. In this case, just update nothing
:
$ composer Update nothing
Loading composer repositories with package
information updating dependencies Nothing to install or update
writing lock file
generating autoload files
As a result, composer does not update the library, but updates it composer.lock
. Note that nothing
is not update
a keyword of the command. Just not nothing
The result of this package. If you enter foobar
, the result is the same.
If you use a composer version that is new enough, you can use the --lock
option directly:
2. composer.json
Install the library without editing
You may feel that each installation of a library needs to be changed composer.json
too cumbersome, 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 written automatically composer.json
: (Note that we use -n
them so that we 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 to order the initialization create-project
?
Composer Create-project Doctrine/orm Path 2.2.0
This automatically clones the warehouse and checks out the specified version. This command is handy when cloning a library, and you don't need to search for the original URI.
4. Consider caching, dist
package priority
The last year's composer will automatically archive your downloaded dist
packages. By default, the dist
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 use it stable
as your minimum-stability
).
Dist packages can also be used for branches such as dev-master
, GitHub allows you to download a package that a git references. In order to force the use of compression packs instead of cloning the source code, you can use install
update
the and --prefer-dist
options.
Here is an example (I used the --profile
option to display the execution time):
$ composer Init--require= "twig/twig:1.*"-N--profile
Memory usage:3.94mb (PEAK:4.08MB), time:0s
$ composer in Stall--profile
Loading composer repositories with package information installing
- 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--profile Loading composer
R Epositories with package information
installing dependencies from lock file
-Installing Twig/twig (v1.12.2) C15/>loading from cache
generating autoload files
Memory usage:4.96mb (PEAK:5.57MB), time:0.45s
Here, twig/twig:1.12.2
the compressed package is saved in the ~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip
. Use directly when reinstalling the package.
5. To modify, the source code first
When you need to modify the library, cloning the source code is more convenient than the download package. You can use --prefer-source
to force the selection of 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 changes:
$ composer Update
Loading 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,?]?
Preparing for the production environment
Finally, when you deploy your code to a production environment, don't forget to optimize for automatic loading:
Composer Dump-autoload--optimize
The same can be used when installing packages --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 particular command, you can read the official document or the Chinese document, or you can view the interactive memo made by Jolicode.
Original address: 5 features to know about Composer PHP
Address: 5 Composer tips that PHP developers should know