PHP developers should know the 5 Composer tips

Source: Internet
Author: User
Tags autoload

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  < Span style= "color: #ff0000;" >is  not up  < Span style= "color: #ff0000;" >to  date  with  the latest changes  in Composer.json, you could 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 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? updatecommand 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 information  Updating dependencies  install  or update  file  

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 :

2. composer.json Install the library without editing

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.

" foo/bar:1.0.0 "  

This method can also be used to quickly open a new project. initcommands 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 -cat  composer.json{    "require": {         "foo/bar""1.0.0"    }}
3. Derivation is easy

Did you try the command when it was initialized create-project ?

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 package first

The composer of the last year will automatically archive the packages you download dist . By default, the dist package is used for tagged versions, such as "symfony/symfony": "v2.1.4" wildcard or version bands, "2.1.*" or ">=2.2,<2.3-dev" (if you use 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$ composerInstall--profileloading Composer repositories with package information installing dependencies-Installing Twig/twig (v1.12.2) Downloading: -%Writing LockfileGenerating autoload files Memory usage:Ten.13MB (Peak: A.65MB), Time:4. 71s$RM-RF vendor$ ComposerInstall--profileloading Composer repositories with package information installing dependencies from lockfile-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 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. To modify, 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-v  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 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 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 an official document or a Chinese document, or you can view this interactive cheat sheet made by Jolicode.

Original address: http://www.phpcomposer.com/5-features-to-know-about-composer-php/, reproduced in reserve.

PHP developers should know the 5 Composer tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.