PHP developer should know five tips Composer

Composer is a new generation of PHP dependency management tool. This article describes the use Composer's five tips, I hope to give you convenient PHP development brings.

1. Update only a single library

Just want to update a particular library, do not want to update all of its dependencies is very simple

composer update foo/bar

In addition, this technique can also be used to solve the "problem of the warning message." 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 went wrong? Do not panic! If you edit composer.json, you should see such information. For example, if you add or update the details, such as the description of the library, author, more parameters, or even just adds a space md5sum will change the file. Composer will then warn you different hash values ​​and composer.lock described.

So how can we do it? update command to update the lock file, but if you just add some description, it should be does not intend to update any libraries. In this case, simply 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 database, but will update composer.lock. Note Keyword nothing not update command. This result is not nothing but a result of the package. If you enter foobar, the result is the same.

If you're using the new version of Composer enough, then you can directly use --lock options:

composer update --lock

2. Under no circumstances install the library of editing composer.json

You might think you install each library will need to modify composer.json too much trouble, 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. init command has --require option to automatically write composer.json :( Note that we use -n, so do not answer questions)

$ composer init --require=foo/bar:1.0.0 -n

$ cat composer.json

{

    "require": {

        "foo/bar": "1.0.0"

    }

}

3. Derived easily

Initialized when you tried to create-project command it?

composer create-project doctrine/orm path 2.2.0

This automatically clone warehouse and detects the specified version. Clone library when this command is very easy to use, do not need to search for the original URI.

4. Consider caching, dist packet prioritization

Composer will automatically archive you downloaded dist package since the last year. In the default setting, dist package for added tag version, for example, "symfony / symfony": "v2.1.4", version or the wildcard or interval. "2.1 *" or "> = 2.2, <2.3-dev" (If you're using stable as your minimum-stability).

dist package can also be used, such as branch dev-master like, Github allows you to download an archive git references. To force the use of compressed, rather than cloning the source code, you can use the install and update the --prefer-dist option.

Here is an example (I used --profile options to display execution time):

$ composer init --require="twig/twig:1.*" -n --profile

Memory usage: 3.94MB (peak: 4.08MB), time: 0s

  

$ composer install --profile

Loading 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 --profile

Loading composer repositories with package information

Installing dependencies from lock file

  - Installing twig/twig (v1.12.2)

    Loading from cache

  

Generating autoload files

Memory usage: 4.96MB (peak: 5.57MB), time: 0.45s

Here, twig / twig: 1.12.2 compressed packet is stored in ~ / .composer / cache / files / twig / twig / 1.12.2.0-v1.12.2.zip. Directly re-install package.

5. To modify the source code priority

When you need to modify the library when it is easier than cloning the source code download package. You can use to force --prefer-source clones were selected source.

composer update symfony/yaml --prefer-source

Then 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 attempt to update a modified library when, Composer will remind you, asking whether 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,?]?

In preparation for the production environment

Finally a reminder, when deploying code to the production environment, do not forget about automatic load optimization:

composer dump-autoload --optimize

When using the installation packages can also --optimize-autoloader. Without this option, you may find that 20% to 25% performance loss.

These are the details of PHP developers should know five tips of Composer

Above hope to help everyone, many PHPer always encounter some problems and bottlenecks in the advanced time, write more business code no sense of direction, I do not know from where to start to ascend, which I compiled some information, including but not limited to: a distributed architecture, highly scalable, high-performance, high-concurrency, server performance tuning, TP6, laravel, YII2, Redis , Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, micro-services, Nginx, etc. more advanced knowledge required for advanced dry goods can be free for everyone to share, I need to join the official group , click here .

Guess you like

Origin www.cnblogs.com/a609251438/p/12121938.html