As a PHP developer, be sure to understand Composer

Source: Internet
Author: User
Tags autoloader composer install
Composer is a very popular PHP package dependency management tool that has replaced PEAR package manager. it is necessary for PHP developers to master Composer.

Composer is a very popular PHP package dependency management tool that has replaced PEAR package manager. it is necessary for PHP developers to master Composer.

For the user, Composer is very simple. a simple command is used to download the required code package to the vendor directory, and then the developer can introduce the package and use it.

The key lies in the composer defined by your project. json: you can define the packages that the project depends on (multiple packages may exist), and dependent packages may depend on other packages (this is the benefit of components, composer automatically downloads everything you need. everything lies in composer. json definition.

Composer is transparent to users, but the concept behind it still needs to be understood. its birth is not accidental. thanks to the rapid development of Github, PHP language is becoming more and more modern, it seems higher.

To understand Composer, let's take a rough look at its structure:

Composer structure
  • Composer command line tool:
    This is easy to understand. download the code you need through the user-defined Composer. json. if you simply use Composer, you can master some specific commands.
  • Autoloading code loader:
    Through Composer, developers can use in a variety of ways, and the key lies in the concept of PHP namespace, and the development of PSR-4 standards, Composer only according to the two developed a code loader
  • Github:
    With Github, PHP developers can host open-source code here, and the development of Composer comes from Github. Composer essentially downloads the code on Github locally.
  • Packagist:
    For the user, the command line tool of Composer is used. how does the command line tool know how many packages can be used by the user? this is mainly dependent on Packagist, packagist is a major package information repository of Composer. Packagist developers host specific code on Github and submit the package information to Packagist, so that users can use it through Composer.
    Composer according to the locally defined composer. json information to query Packagist, Packagist according to Composer. json/Package. json information parsing corresponds to the github repository. Composer depends on Composer on the Github repository when downloading code. json. three types of composer are involved here. json, meaning is different.
  • Composer. json:
    This is the core of Composer and is the Composer rule. three types of Composer. json are also mentioned above. when using Composer, you must differentiate them and I will always mess up when I am a beginner.
Composer command line tool

Composer init

You can create composer. json in your project to define the dependency packages of your project. you can also create composer. json in composer init interactive mode.

Composer install

It should be the most common command. composer will follow the local composer. the json installation package puts the downloaded package under the vender Directory of the project, and the package version information during installation into composer. lock to lock the version.

In fact, during the install, if you find that composer. the lock version is the same as the current version of the code under the vendor directory, so Composer does nothing. composer. the purpose of lock is to allow you to work in the current version without obtaining the latest package.

Composer update

So how to update composer. lock to get the latest package? Use this command to update the latest package

Composer config

We recommend that you keep the global configuration in COMPOSER_HOME/config. json. The non-global configuration information is stored in the project directory.

composer config --list -g composer config -g notify-on-install false composer global config bin-dir --absolute

Composer create-project

This command is not commonly used, but I personally think it is very important to use the normal install command to download all the dependent packages of the project to the vendor directory of the project. this command puts all the code and its dependent packages in one directory, which is equivalent to executing a git clone command, generally, the package developer may use this command to fix the bug.

Composer global

This is a global installation command that allows you to execute the Composer command in the COMPOSER_HOME Directory, such as install and update. of course, your COMPOSER_HOME must be in the $ PATH environment.

For example, execute composer global require fabpot/php-cs-fixer. now the php-cs-fixer command line can be run globally. if you want to update it later, just run composer global update

Composer dump-autoload

When you modify the composer under the project. to update a json file, you do not have to run the composer update command. sometimes you can use this command to update the loader. for example, you need to reference a local custom package (not from packagist ), this command will be explained later through practice.

Composer require

If you manually or interactively create the composer. json file, you can directly use this command to install the package

composer require cerdic/css-tidy:1.5.2 composer require "ywdblog/phpcomposer:dev-master"

-Prefer-source and-prefer-dist parameters

-Prefer-dist: for stable packages, this parameter is usually used by Composer for installation by default, which can accelerate installation. for example, a package may be installed directly from packagist, instead of downloading the package on Github.

-Prefer-source: If this parameter is used, it will be directly installed from Github. after the installation package is installed, the vendor directory also contains. git information.

Composer require "ywdblog/phpcomposer: dev-master" -- prefer-source # The. git information is contained in the vendor/ywdblog/phpcomposer directory.

How to add a proxy to Composer

It is particularly slow to use Composer for download in China. you can use two methods for acceleration.

  • Composer config repo. packagist composer "https://packagist.phpcomposer.com"
  • Edit composer. json
"repositories": { "packagist": { "type": "composer", "url": "https://packagist.phpcomposer.com" }}
Autoloading code loader

Composer itself integrates an autoloader, supporting PSR-4, PSR-0, classmap, files autoloading.

Here is an example to illustrate how Composer references classmap, files, locally compliant with PSR-4 Standards Code

Edit composer. json

"autoload": { "classmap": ["othsrc/","classsrc.php"], "files": ["othsrc/filesrc.php"], "psr-4": {"Foo\Bar\": "src"} }

Composer dump-autoload
With the above operations, for the PSR-4 is equivalent to registering a PSR-4 autoloader (from The FooBar namespace)

If you do not want to use the Composer autoloader, you can directly include the file vendor/composer/autoload _ *. php and configure your own loader.
The specific example is hosted on github. for details, refer.

Repositories

For Repositories, it is not necessary to understand it. but if you have mastered it, you can better understand Composer. For Repositories, its Chinese and English documents are well explained.Excerpt.

Basic concepts

Package:

Composer is a dependency management tool. it installs some resource packages and package descriptions locally (such as the package name and corresponding version). The most important metadata descriptions are dist and source, dist points to an archive, which is used to package data of a certain version of a resource package. source points to a development source, which is usually a source code repository (such as git)

Resource Library:

A resource library is the source of a package. it is a list of packages/versions.

Composer will view all your defined repositories to find the resource packages required by the project (This sentence is important ).

By default, Packagist.org has been registered to Composer (or it can be understood that Packagist.org is the default repository type of the Composer Resource Library)

Composer Resource Library type

The Composer resource library includes four types. the default is the composer type, which is the resource type used by packagist.org.

It uses a single packages. json file, including all resource package metadata. when you publish a package to pckagist.org, A packages is created by default. json, but I cannot find the file corresponding to my package.

VCS Resource Library type

If you want to build a private Composer private Resource Library type, you can use this type. here is an example, such as the composer in your project. json is defined as follows, and the corresponding code on Github can be used.

{ "repositories": [    { "type": "vcs", "url": "https://github.com/ywdblog/phpcomposer" }    ], "require": { "ywdblog/phpcomposer": "dev-master" }}

When running composer update, Comoser actually downloads the package from Github instead of pckagist.org.

In addition, if you need to use the Package Resource Library type or PEAR Resource Library type, refer to the official documentation. Generally, you can define the name and version attributes in composer. json.

Composer. json

Composer is also mentioned many times in this article. json. for example, if you want to use a third-party package, you need to define the composer locally. after installing a third-party package, Composer is also found in the third-party package directory. json, so both are called composer. what is the difference between json and json? Understanding this is very important.

Assume that you define a composer under your project. json, the package is called the ROOT package, and the composer. json defines the conditions required by your project (for example, your project may depend on a third-party package ).

Some attributes in composer. json can only be used by the ROOT package. for example, the config attribute takes effect only in the ROOT package.

Whether a resource package is a ROOT package depends on its context. for example, if you clone ywdblog/phpcomposer by git, the local phpcomposer directory is the ROOT package, if you are in the local phpcomposer directory composer require ywdblog/phpcomposer, then your project phpcomposer is the ROOT package.

For more information about the composer-schema.json, refer to this URL, Laravel as a sophisticated framework that defines the composer. json very classic

Package Version

When you configure composer. json locally, you can specify a specific version of the package. Composer supports downloading packages under tags or branches from the Github repository.

For the Tag on Github, Packagist creates the version of the corresponding package, which complies with X.Y. z, vX. y.Z, X.Y. the Z-package type means that, although there is only one specific version of the package on Github, Composer supports multiple forms of reference, such:

composer require monolog/monolog 1.0.0-RC1 composer require monolog/monolog  v1.0.0-RC1 composer require monolog/monolog 1.0.*composer require monolog/monolog  ~1.10

For the branches on Github, Packagist will create the version of the corresponding package. if the branch name looks like a version, the package version of {branch name}-dev will be created, if the branch name does not look like a version number, it will create a version number in the form of dev-{branch name }.

composer require monolog/monolog  master-devcomposer require monolog/monolog  master.x-dev
Summary:

Understanding Composer, the most important thing is practice, and finally understanding the PSR-4 and namespace, you can also try to release your project to pckagist.org.

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.