Composer is a new generation of PHP-dependent management tools. Its introduction and basic usage can be seen in this "PHP Management dependency (dependency) Relationship tool Composer installation and use." 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, your may be getting outdated s, run update to update them.
Rub, what's wrong? Don't Panic! If you edit the Composer.json, you should see this information. 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? The update command can update the lock file, but if you add only a few more descriptions, you should not want to update 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 the composer.lock. Note that nothing is not a keyword of the update command. Just don't have nothing to do with this package result. 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. Install the library without editing the Composer.json
You may feel that each installation of a library needs to be modified Composer.json too cumbersome, then 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. The init command has the--require option to automatically write Composer.json: (Note that we use-n so we don't have to answer the question)
$ composer Init--require=foo/bar:1.0.0-n
$ cat Composer.json
{
"require": {"
Foo/bar": "1.0.0" c17/>}
}
3. Derivation is easy
Did you try the Create-project command when initializing?
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 packs. By default, the dist package is used to add the tag version, such as "Symfony/symfony": "v2.1.4", or wildcard or version range, "2.1.*" or ">=2.2,<2.3-dev" (If you use stable as your minimum-stability.)
Dist packages can also be used with branches such as Dev-master, and GitHub allows you to download a package that a git references. You can use the install and update--prefer-dist options to force the use of compression packs instead of cloning the source code.
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 's compressed package is stored in ~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip. Use directly when reinstalling the package.
5. Consider modification, 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
You can also use--optimize-autoloader when installing a package. 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 view the interactive memo that Jolicode made.