PHP developers should know the 5 Composer tips

Source: Internet
Author: User
Tags autoload composer install
Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be seen in this "PHP dependency management tool composer Getting Started" tutorial. This article describes the use of composer five tips, hoping to give you the convenience of PHP development.

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 is not up to date with the latest changes in Composer.json, you may be getting outdated dependencie s, run update to update them.

Rub, what's wrong? Don't Panic! If you edit 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 differs from the one recorded in the Composer.lock.

So what do we do? The update command updates 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 informationupdating dependenciesnothing to install or Update Writing lock filegenerating autoload files

This way, composer does not update the library, but it updates the composer.lock. Note Nothing is not a keyword for the update command. Just nothing this package results in. If you enter Foobar, the result is the same.

If your composer version is new enough, you can use the--lock option directly:

Composer Update--lock

2. Install the library without editing the Composer.json

You may feel that every installation of a library needs to be modified 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. The init command has the--require option to automatically write Composer.json: (Note that we use-n so that you 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

When initializing, did you try the create-project command?

Composer Create-project Doctrine/orm Path 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 packet first

The composer of the last year will automatically archive the dist package you downloaded. By default, the dist package is used for tagged versions, 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 for branches such as Dev-master, where GitHub allows you to download a compressed package that is referenced by a git. To force the use of a compressed package instead of cloning the source code, you can use the--prefer-dist option for install and update.

Here's an example (I used the--profile option to show the execution time):

$ composer Init--require= "twig/twig:1.*"-N--profile Memory usage:3.94mb (PEAK:4.08MB), time:0s$ composer Install--p Rofile Loading composer repositories with package informationinstalling dependencies  -Installing Twig/twig (v1.12.2 )    downloading:100%writing lock filegenerating autoload filesmemory usage:10.13mb (PEAK:12.65MB), time:4.71s$ rm-r F vendor$ Composer Install--profile Loading composer repositories with package informationinstalling dependencies from Lo CK file  -Installing Twig/twig (v1.12.2)    Loading from cache generating AutoLoad filesmemory USAGE:4.96MB (peak:5 .57MB), time:0.45s

Here, twig/twig:1.12.2 's compressed package is saved in ~/.composer/cache/files/twig/twig/1.12.2.0-v1.12.2.zip. Used directly when reinstalling packages.

5. Consider the changes, 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--prefer-source to force the selection of clone source code.

Composer Update Symfony/yaml--prefer-source

Next you can modify the file:

Composer Status-vyou has 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 Updateloading composer repositories with package informationupdating 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

You can also use--optimize-autoloader when installing a package. Without this option, you may find a performance penalty of 20% to 25%.

  • 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.