[Composer] PHP developers must understand!

Composer is a very popular PHP packages dependency management tool, has been replaced by the PEAR package manager for PHP developers to master Composer is a must.

Composer for the user is very simple, you will need to download the code package to the next vendor directory with a simple command, then the developer can package introduced and used.

The key is that you project definition composer.json, you can define the project relies on packages (there may be more), while the dependent packages may well depend on other packages (this is the advantage of the component), which you do not have to worry, Composer will automatically download everything you need, everything lies in the definition of composer.json.

Composer is transparent for the user, but the idea behind it or need to know about the birth of their no accident, thanks to the rapid development of Github, PHP language more and more modern look taller on the .

More PHP knowledge please pay attention to my column PHP zhuanlan.zhihu.com

To understand the Composer, probably first understand its structure:

Composer structure

Composer command line tool:

This understanding is relatively simple, user-defined Composer.json by the code you need to download, if simply using the Composer, then have some specific command is entirely possible

Autoloading Code loader:

By Composer, developers can use a variety of ways to go, and the key lies in the concept, and the development of PSR-4 standard namespaces of the PHP, but developed a Code Composer autoloader according to both

Github:

With Github, PHP developers can open source code is hosted in it, and Composer from the development of Github, the Composer is essentially the code on Github downloaded to the local.

Packagist:

For users who are using the Composer command-line tool, the command-line tools how to know how many packages it can be used by the user, which is mainly dependent on Packagist, Packagist Composer is a major package of information repository, developers package the specific code hosted on Github, will be submitted to the package information Packagist, so that the user can use to Composer.

Composer locally defined according to composer.json information to query Packagist, Packagist according to Composer.json / Package.json information analysis, ultimately correspond to github repository, download the code Composer final time also depends on the Composer.json on Github repository here involves three types of composer.json, the meaning is not the same.

Composer.json:

This is the core of Composer, Composer is the rule, also mentioned above, three types of Composer.json, when in use must pay attention to distinguish, I am a novice when he always messing.

Composer command-line tool

composer init

Users can create their own projects composer.json in order to define the dependencies of your project, you can also create interactive composer.json by composer init.

composer install

Should be the most commonly used commands, composer installs packages based on local composer.json, under the vendor directory will be downloaded package into the project, while the installation package version information when put into composer.lock, in order to lock version.

In fact, install time, if it is found under composer.lock code version and the current version of the vendor directory is the same, the Composer will do nothing, composer.lock purpose is to make you feel at ease working in the current version, without get the latest version of the package.

composer update

So how composer.lock update to the latest version in order to get the bag? Through this command to update the latest version of the package

composer config

This command is recommended to understand, the overall configuration is saved in COMPOSER_HOME / config.json, non-global configuration information is stored in the directory under this project.

composer config --list -g

composer config -g notify-on-install false

composer global config bin-dir --absolute

composer create-project

This command is not used, but personally think it is very important to use common command is to install all project dependencies to download under the item vendor catalog. And by this command sucked all of the code and its dependencies into packages a directory, equivalent to the implementation of a git clone command, usually the developer package of bug fixes will be possible to use this command.

composer global

This is a global installation command, which allows you to execute commands in the Composer COMPOSER_HOME directory, such as install, update. Of course, under your COMPOSER_HOME to the $ PATH environment.

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

composer dump-autoload

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

composer require

If manually or interactively create composer.json file can be directly used to install the command packet

composer require  cerdic/css-tidy:1.5.2

composer require "ywdblog/phpcomposer:dev-master"

–prefer-source和–prefer-dist参数

-prefer-dist: For stability package, the general Composer install the default parameters, which can speed up the installation, for example, it is possible to install the appropriate package directly from packagist, without actually going on Github download package.

-prefer-source: if the parameter, directly from the Github are installed, the installation package also contains information about the vendor directory .git

composer require "ywdblog/phpcomposer:dev-master" --prefer-source 

#在vendor/ywdblog/phpcomposer目录下含有.git信息

How to add agents to the Composer

Domestic use Composer particularly slow to download, can be accelerated by two methods

composer config repo.packagist composer “https://packagist.phpcomposer.com“

编辑composer.json

"repositories": {

  "packagist": {

      "type": "composer",

      "url": "https://packagist.phpcomposer.com"

  }

}

 

Autoloading Code loader

a composer integrated itself autoloader, supporting PSR-4, PSR-0, classmap, files autoloading.

Here an example to illustrate how a reference classmap, files, the native code compliance PSR-4 standard by Composer

Edit composer.json

"autoload": {

  "classmap": ["othsrc/","classsrc.php"],

  "files": ["othsrc/filesrc.php"],

  "psr-4": {"Foo\Bar\": "src"}  }

composer dump-autoload

 

Through the above operation, PSR-4 is equivalent to a register PSR-4 autoloader (namespace from FooBar)

If you do not want to use the autoloader Composer, you can directly contain vendor / composer / autoload _ *. Php files, configure your own loader.

Specific examples hosted on github, refer.

Repositories

About Repositories, understand that it is not necessary, but if you grasp more understanding Composer, for Repositories, its Chinese documents and English documents explain very well, there have also been some excerpts.

basic concepts

package:

Composer is a dependency management tool, which is installed some resources package and described the package (such as package name and the corresponding version) locally, the more important metadata description is dist and source, dist point to an archive, the archive is a resource .source a packaged version of the package of data points to a source of development, which is often a source code repository (such as git)

Repository:

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

Composer will look at all the repositories you define the package to find the resources required for the project (this sentence is very important).

By default've registered to Composer (or understood as is Composer repository default warehouse type)

Composer repository type

Composer repository includes four types, the default type is a composer, that is, resource type used.

It uses a single packages.json file that contains all the resource package metadata when you publish to the package. on, the default system creates a packages.json, but I did not find my packet corresponding file.

VCS repository type

If you want to build a private repository type Composer private, you can use this type, here give an example, such as you define your own projects in composer.json below, you can use the corresponding code on Github up.

 

{

    "repositories": [

    {

        "type": "vcs",

        "url": "https://github.com/ywdblog/phpcomposer"

    }

    ],

    "require": {

        "ywdblog/phpcomposer": "dev-master"

    }

}

 

When running the composer update, Comoser actually download package from the Github rather than download.

If you need to use another type or Package Repository PEAR repository type, you can refer to the official documents, the general definition name, version attribute to the composer.json in.

Composer.json

In the above article also mentioned several times composer.json, for example, you want to use a third-party package is required, after the installation of third-party packages Composer, will find composer.json in third-party package directory locally defined composer.json, then this both call composer.json, what difference does it make? it is very important to understand.

If you are a composer.json in your own projects as defined below, then this package is called ROOT package, you define the condition that composer.json desired item (such as your project may rely on a third-party packages).

composer.json Some attribute ROOT package can only be used, such as config property takes effect only on ROOT package.

A resource package is not ROOT package, depending on its context, for example, you git clone ywdblog / phpcomposer, then this is the ROOT directory phpcomposer local time bag, if you're in a local directory phpcomposer composer require ywdblog / phpcomposer, this is your time phpcomposer project is the ROOT package.

Learn composer-schema.json The URL may refer to, as a mature Laravel framework, which is the classic definition composer.json

About version of the package

When the user configuration composer.json in local time, you can specify a particular version of the package needs, Composer Tag support download from Github repository or package under the branches.

For Tag on Github speaking, Packagist will create the corresponding version of the package, it is consistent with XYZ, vX.YZ, XYZ- package type, that is to say on Github although only one particular version of the package, but Composer supports multiple reference ,such as:

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 branch on Github, Packagist creates a corresponding version of the package, if the branch name looks like a version, branch name is created {} -dev package version number, if the branch name does not look like a version number, it branch will be created dev- {name} in the form of version number

to sum up:

Understanding Composer, the most important thing is practice, and finally can understand PSR-4 and namespace, you can also try to publish your project to on.

These are the [Composer] PHP developers must understand! Details

Guess you like

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