Five tips for PHP developers to know about Composer and the developer's composer_PHP tutorial

Source: Internet
Author: User
Tags autoloader composer install
PHP developers should know the five tips of Composer, developer composer. PHP developers should know five tips about Composer. developer composerComposer is a new generation of PHP dependency management tools. Its introduction and basic usage can be found in the article ComposerPHP dependency manager PHP developer's five tips on Composer and developer 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?updateThe 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:nothingNotupdateCommand keyword. Just nonothingResult of this package. If you enterfoobarAnd the results are the same.

If the Composer version you are using is new enough, you can directly use--lockOption:

composer update --lock 
2. do not edit composer.jsonIn the case

You may feel that every time you install a library, you need to modify it.composer.jsonIt is too troublesome, so you can directly userequireCommand.

composer require "foo/bar:1.0.0" 

This method can also be used to quickly create a new project.initCommand has--requireOption, which can be automatically writtencomposer.json:( Note that we use-nIn 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-projectCommand?

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, distPackage priority

In the last year, the Composer will automatically archive your downloadeddistPackage. By default,distThe 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 usestableAs yourminimum-stability).

The dist package can also be useddev-masterGithub allows you to download a compressed package referenced by git. You can useinstallAndupdateOf--prefer-dist.

The following is an example (I used--profileTo 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.2The 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-sourceTo 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?updateThe 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:nothingNotupdateCommand keyword. Just nonothingResult of this package. If you enterfoobarAnd the results are the same.

If the Composer version you are using is new enough, you can directly use--lockOption:

composer update --lock 
2. do not edit composer.jsonIn the case

You may feel that every time you install a library, you need to modify it.composer.jsonIt is too troublesome, so you can directly userequireCommand.

composer require "foo/bar:1.0.0" 

This method can also be used to quickly create a new project.initCommand has--requireOption, which can be automatically writtencomposer.json:( Note that we use-nIn 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-projectCommand?

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, distPackage priority

In the last year, the Composer will automatically archive your downloadeddistPackage. By default,distThe 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 usestableAs yourminimum-stability).

The dist package can also be useddev-masterGithub allows you to download a compressed package referenced by git. You can useinstallAndupdateOf--prefer-dist.

The following is an example (I used--profileTo 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.2The 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-sourceTo 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

Http://www.bkjia.com/PHPjc/1098969.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1098969.htmlTechArticlePHP developers should know the five Composer tips, developer composer Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be found in this article Composer PHP dependency manager...

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.