Php developers and composer have to tell stories, and composer has to say
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 users, Composer is very simple. You can use a simple command to download the required code packagevendor
Directory, then the developer can introduce the package and use it.
The key lies in the definition of your project.composer.json
You can define the packages that the project depends on (there may be multiple), and the dependent Packages may depend on other packages (this is the benefit of components, composer automatically downloads everything you need.composer.json
.
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.Composer.json
Download the code you need. 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 local definitioncomposer.json
Information to query Packagist, Packagist accordingComposer.json/Package.json
Information parsing will eventually correspond to the github repository, and Composer will depend onComposer.json
, Three typescomposer.json
The meaning is different.
- Composer. json:
This is the core of Composer and is the Composer rule. Three typesComposer.json
Be sure to differentiate when using it, and I will always be confused when I am a beginner.
Composer command line tool
Composer init
Users can createcomposer.json
To define the dependency packages of your project, you can also usecomposer init
Interactive Creationcomposer.json
.
Composer install
It should be the most common command, and composer willcomposer.json
Installation Package, put the downloaded package intovendor
Directory, and add the package version information during installationcomposer.lock
To lock the version.
In fact, during the install, if you findcomposer.lock
Version and currentvendor
The Code versions in the directory are the same, so Composer does nothing,composer.lock
The purpose is to allow you to work in the current version without obtaining the latest package.
Composer update
So how to updatecomposer.lock
To get the latest version of the package? Use this command to update the latest package
Composer config
We recommend that you keep the global configuration inCOMPOSER_HOME/config.json
Non-global configuration information is stored in the project directory.
123 |
Composer config -- list-gcomposer config-g restart y-on-install falsecomposer 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 common install command to download all the dependent packages of the project to the project.vendor
Directory. With this command, all the code and its dependent packages are put in a directory, which is equivalent to executinggit clone
Command. Generally, the package developer may use this command to fix the bug.
Composer global
This is a global installation command that allows youCOMPOSER_HOME
Directory to execute the Composer command, suchinstall
,update
Of course, yoursCOMPOSER_HOME
To$PATH
Environment.
For examplecomposer global require fabpot/php-cs-fixer
Now the php-cs-fixer command line can be run globally. If you want to update it later, you only need to runcomposer global update
Composer dump-autoload
When you modifycomposer.json
File, not necessarily runcomposer update
Command to update. Sometimes you can use this command to update the loader. For example, if you want to reference a local custom package (not from packagist), the command will be described later through practice.
Composer require
For example, manually or interactivelycomposer.json
File, you can directly use this command to install the package
12 |
Composerrequire cerdic/css-tidy: 1.5.2composerrequire "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 packagevendor
The directory also contains. Git Information
12 |
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
123456 |
"Repositories": {"packagist": {"type": "composer", "url": "https://packagist.phpcomposer.com "}} |
Autoloading code LoaderComposer itself integratesautoloader
, SupportedPSR-4
,PSR-0
,classmap
,files autoloading
.
Here is an example to illustrate how to use Composer to referenceclassmap
,files
,Locally compliant PSR-4 Standard Code
- Edit composer. json
12345 |
"Autoload": {"classmap": ["othsrc/", "classsrc. php "]," files ": [" othsrc/filesrc. php "]," psr-4 ": {" Foo \ Bar \ ":" src "}} |
- Composer dump-autoload
Through the above operations, for the PSR-4 is equivalent to registering a PSR-4 autoloader (fromFooBar
Namespace)
If you do not want to use Composer autoloader, you can directly includevendor/composer/autoload_*.php
File, configure your own loader.
The specific example is hosted on github. For details, refer.
RepositoriesFor Repositories, it is not necessary to understand it, but Composer can be better understood if you are familiar with it. For Repositories, the Chinese and English documents of Repositories are well explained.
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 important metadata description isdist
Andsource
,dist
Point to an archive. This archive is used to package data of a specific version of a resource package.source
Point 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 ispackages/versions
.
Composer will view all your definedrepositories
To find the resource package required by the project (this sentence is very 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 isComposer type
That is, the resource type used by packagist.org.
It uses a singlepackages.json
File, including all the resource package metadata. When you publish the package to pckagist.org, the system will createpackages.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:composer.json
If the definition is as follows, the corresponding code on Github can be used.
1234567891011 |
{"Repositories": [{"type": "vcs", "url": "http://www.renti51.cn"}], "require": {"ywdblog/phpcomposer ": "dev-master "}} |
When runningcomposer update
Comoser actually downloads the package from Github instead of pckagist.org.
In addition, if you want to usePackage resource library type
OrPEAR resource library type
, Refer to the official documentation, generally incomposer.json
To define the name and version attributes.
Composer. jsonAs mentioned abovecomposer.json
For example, if you want to use a third-party package, you need to define it locallycomposer.json
After Composer installs a third-party package, it will also be found in the third-party package directorycomposer.json
, Then both are calledcomposer.json
What is the difference? Understanding this is very important.
Assume that you definecomposer.json
, This package is calledROOT package
, Thiscomposer.json
Define the conditions required by your project (for example, your project may depend on a third-party package ).
composer.json
Some attributes can only beROOT package
For exampleconfig
OnlyROOT package
.
Is a resource packageROOT package
Depends on its context, such as yougit clone ywdblog/phpcomposer
In this case, the local phpcomposer directory isROOT package
If you are in the phpcomposer directorycomposer require ywdblog/phpcomposer
In this case, your project phpcomposer isROOT package
.
Understandingcomposer-schema.json
For details, refer to this URL. Laravel is a mature Framework and Its composer. json definition is very classic.
Package version
When the user configurescomposer.json
You can specify the specific version of the package to be downloaded from the Github repository.Tag
OrBranch
Package.
For the Tag on Github, Packagist creates the version of the corresponding package, which matchesX.Y.Z
,vX.Y.Z
,X.Y. Z-Package Type
That is to say, although there is only one specific version of the package on Github, Composer supports multiple forms of reference, such:
1234 |
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 creates the version of the corresponding package. If the branch name looks like a version{Branch name}-dev
If the branch name does not look like a version number, it will createDev-{branch name}
Format version number
12 |
Composerrequiremonolog/monolog master-devcomposerrequiremonolog/monolog master. x-dev |
Summary:Understand Composer. The most important thing is practice.PSR-4
And namespace, you can also try to publish your project to pckagist.org.
From: http://www.girl4493.cn