PHP7 Study notes (13) composer detailed one

Source: Internet
Author: User
Tags autoload pear composer install

Summary

From copying third-party code into the Project (1994), to pear Installing the Dependency Pack (1999), to the rise of composer, the PHP community has been exploring for nearly 20 years. The ancient language of PHP, also in the continuous development of updates, in the web has been glowing fever. Composer is currently the best tool for PHP package dependency management and deserves to be mastered by every PHP developer.

official website

https://www.phpcomposer.com/

What is composer?

is the tool that PHP uses to manage dependency (dependency) relationships. You can declare your dependencies on the External tool Library (libraries) in your project, and Composer will help you install these dependent library files.

Expansion and Package

1, expansion and package are two very similar concepts. In the PHP world, it is generally possible to understand and differentiate between the two: the extension (extension) and the module equivalent, is written in C language function collection.

2. The package and library are equivalent, the main function is to use PHP to implement the feature collection; expand to load in the form of a dynamic link library (. dll or. So), and the package is loaded by Require/include mode. Most of the time, mixed use of the two will not cause difficulties in understanding.

3, Common expansion: GD, ZIP, XML, mysqli, Opcache and so on.

4, the common package: Phpmailer, Phpoffice, Htmlpurifier and so on.

Pear and pecl

Before the composer epidemic, pear and pecl were the two tools (communities) known to PHP developers. Pear is the abbreviation for PHP extension and application repository (PHP Extension and Application Repository), and the website Http://pear.php.net;PECL is PHP extension Community Library (PHP Extension Community Library) abbreviation, official website http://pecl.php.net.

The difference between the two can be extended and package to differentiate:Pecl managed to expand, the source code is more C files, such as APC, AMPQ, etc.Pear managed package, function PHP implementation, such as PHP codesniffer, HTTP request, etc. Pear corresponds to the pear command, pecl corresponds to the pecl command, which can be used to install and manage extensions and packages (Pear build/pickle subcommands may also compile extensions in pecl). The two complement each other, the official website with the sister (sisters) describe the relationship between the two.

PECL is the official expansion of the supplement, is still active, some excellent expansion has become the potential of official expansion. Hantian Great God's Swoole expansion is also hosted in Pecl, the domestic reputation is very high. By contrast pear has been a very outdated. PEAR2 and Pyrus (the next generation of pear package installation tools, based on php5.3+ build, official website Http://pear2.php.net) also failed to save pear. The decline of pear accompanied by the rise of the protagonist composer in this paper.

Pear's positioning is "providing reusable PHP components" to provide the developer with a functional package in a centralized manner. The centralized release method guarantees the quality of the code, while maintaining the inconvenience of maintenance: Through the review of the package to be published, package outdated phenomenon is serious. Pear installed packages are global, cannot install dependent packages for individual projects, and non-privileged users cannot install dependent packages on their own. Other drawbacks include poor dependency management. With the advent of GitHub and composer, package management has entered the composer era. Pear has completed its historical mission, and can go with peace of mind.

Composer

Strictly speaking,the location of composer is dependent on the management tool , not the package Manager . Composer Chinese network to composer work is described below. Composer will solve this problem for you:

A) You have a project that relies on a number of libraries.

b) Some of these libraries depend on other libraries.

c) You declare what you depend on.

d) Composer will find out which version of the package needs to be installed and install them (download them to your project).

Pear can do things, composer can do (including installation pecl expansion), some can do better. Composer By default, the package is installed in the project directory, normal users can use (composer official recommendation not to execute the composer command as root); Encourage adherence to best practices (i.e., the famous PSR specification, see Php-fig official website https:// www.php-fig.org), greatly promote the PHP community coding style of normalization; composer is a centralized platform, anyone can publish code packages, release packages without review, the quality of the package is determined by the user vote ... As a successor to pear, composer's performance withstood the test of the community and became a de facto tool of reliance on management standards.

Composer has now formed a huge ecological, in the number of composer bags far beyond pear. Since anyone can freely publish the package without review, the packages in the composer ecosystem may be plagued by uneven code quality, different code styles, and backdoor vulnerabilities. In addition, composer relies on the management of the project as a unit, which may install the same package multiple times on a single machine. But his flaws, in general, composer greatly changed the development ecology of PHP, promoting code Exchange and community development.

Composer Usage

Composer is dependent on the managed project, and the Composer.json file in the project is the basis for its work. The most important part of the file is the Require section, which tells composer which packages and their versions are expected to be installed, such as:

{    "name": "Tinywan/easy-live",    "description": "Nginx Live Module",    "type": "Library",    "license": " MIT ",    " authors ": [        {            " name ":" Tinywan ",            " email ":" [email protected] "        }    ],    " Require ": {        " php ":" >=7.0 "    },    " AutoLoad ": {        " psr-4 ": {            " live\\ ":" src "        }}    }

Then run composer install  the command, composer will automatically analyze dependencies, install the most appropriate package into the vendor directory. The plus-V (-vv,-vvv) option prints detailed information during the execution of the command. After the installation is complete, the vendor files are generated in the directory autoload.php . Include this file in the entry file for the project: require __DIR__ . "/vendor/autoload.php"; Next, you can reference the interfaces and classes in the dependent package anywhere in the project.

installIn addition to the command, composer provides many other command management dependencies. Common command scenarios include: Find dependencies, introduce dependencies, install dependencies, update dependencies. The commands corresponding to each are:

  1. Composer Search: find dependent packages based on keywords, such as finding the package I published: Composer Search live. This command is equivalent to the https://packagist.org for packet lookup;

  2, composer require: to introduce a dependency, declare the project or global (global, user name globally, non-system global) dependent on a package, such as claims need to Swiftmailer package: Composer require [global] " Swiftmailer/swiftmailer:dev-master " The command updates the Composer.json file and installs dependencies by default (the--no-update option blocks the default installation), and the effect is equivalent to editing the Composer.json file and then executing the install command;

  3, composer install: installs the Composer.json declaration the dependency package, the final installs the dependent package version may depend on has the Composer.lock file;

  4, composer update: update depends on the latest version, equivalent to delete composer.lock file after the execution of composer install.

The above four commands cover most of the scenes using composer. Here are a few common auxiliary commands that are related to dependency analysis:

  1, composer info:  Check the installation of the dependency package information, and composer show equivalent;

  2, composer Dumpautoload: plus-o option to export the optimized loader;

  3. Composer Why (-not): view (not) The reason for installing a package.

Summary

。。。

Reference

1, https://my.oschina.net/u/1030865/blog/1788303

2, https://benramsey.com/blog/2013/11/the-fall-of-pear-and-the-rise-of-composer/

3, http://fabien.potencier.org/the-rise-of-composer-and-the-fall-of-pear.html

4, http://docs.phpcomposer.com/

PHP7 Study notes (13) composer detailed one

Related Article

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.