Composer is a very popular PHP package dependency management tool that has replaced the PEAR package manager. Mastering Composer is a must for PHP developers.
Composer is very simple for users. Download the required code package to the vendor
directory with a simple command, and then the developer can import the package and use it.
The key lies in your project definition composer.json
, you can define the project needs to depend on The package (there may be more than one), and the dependent package may depend on other packages (this is the benefit of components), these do not have to worry about you, Composer will automatically download everything you need, everything depends on composer.json
the definition.
Composer is very transparent to users, but the concept behind it still needs to be understood, and its birth is not accidental. Thanks to the rapid development of Github, the PHP language is becoming more and more modern, and it looks taller. .
In order to understand Composer, let's first understand its structure:
Structure of Composer
- Composer command line tool:
This understanding is relatively simple.Composer.json
You can download the code you need through user definition. If you simply use Composer, you can master some specific commands. - Autoloading code loader:
Through Composer, developers can use it in a variety of ways, and the key lies in the concept of PHP namespace and the development of the PSR-4 standard. Composer just developed a code autoloader based on the two - Github:
With Github, PHP developers can host open source code on it, and the development of Composer originates from Github. Composer essentially downloads the code on Github to the local. - Packagist:
For the user, the command line tool of Composer is used, so how does the command line tool know how many packages can be used by the user? This is mainly dependent on Packagist, which is the main package information repository of Composer. The developer hosts the specific code on Github, and submits the package information to Packagist, so that users can use it through Composer. Composer queries Packagist
according to the locally defined information, Packagist parses the information according to the information, and finally corresponds to the github warehouse, Composer The final download of the code also depends on the Github repository. There are three types involved here , with different meanings.composer.json
Composer.json/Package.json
Composer.json
composer.json
- Composer.json:
This is the core of Composer and the rules of Composer. The three types are also mentioned above. YouComposer.json
must pay attention to the distinction when using them. I always messed up when I was a beginner.
Composer Command Line Tool
Composer init
users can create under their own projects composer.json
to define your project's dependencies, or composer init
create them interactively composer.json
.
composer install
should be the most commonly used command, composer will composer.json
put the downloaded package into the vendor
directory under the project according to the local installation package, and at the same time put the package version information during installation in composer.lock
order to lock
the version. In fact, when installing , if it is found that the version is the same as the code version in the composer.lock
current vendor
directory, Composer will do nothing, composer.lock
the purpose is to let you work in the current version without getting the latest version of the package.
composer update
So how to update composer.lock
to get the latest version of the package? This command can update the latest version of the package
The composer config
command is still recommended to understand, the global configuration is stored in COMPOSER_HOME/config.json
, and the non-global configuration information is stored in the project directory.
1
2
3
|
composer config --list -g
composer config -g notify-on-install false
composer global config bin-dir --absolute
|
The composer create-project
command is not commonly used, but I think it is still very important. Using the ordinary install command is to download all the dependencies of the project to the project vendor
directory. With this command, all the code and its dependencies are downloaded. Putting a package in a directory is equivalent to executing a git clone
command, which is generally used by package developers to fix bugs.
composer global
This is a global installation command, which allows you COMPOSER_HOME
to execute Composer commands in a directory, such as install
, update
. Of course, yours COMPOSER_HOME
must be in the $PATH
environment.
For example, execution 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 runcomposer global update
composer dump-autoload
When you modify composer.json
the files under the project, you don't have to run composer update
the 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), This command will be explained in practice later.
composer require
If you create files manually or interactively composer.json
, you can use this command directly to install packages
1
2
|
composerrequire cerdic/css-tidy:1.5.2
composerrequire"ywdblog/phpcomposer:dev-master"
|
--prefer-source and --prefer-dist parameters
--prefer-dist: For stable packages, this parameter is generally used by default for Composer installation, which can also speed up the installation. For example, it is possible to install the corresponding package directly from packagist without using Actually go to Github to download the package.
--prefer-source: If this parameter is used, it will be installed directly from Github. After the package is installed, the vendor
directory also contains.git信息
1
2
|
composer require "ywdblog/phpcomposer:dev-master" --prefer-source
#在vendor/ywdblog/phpcomposer目录下含有.git信息
|
How to add a proxy
to Composer. Downloading using Composer in China is very slow. It can be accelerated by two methods
- composer config repo.packagist composer “https://packagist.phpcomposer.com“
- Edit composer.json
123456"repositories":{"packagist":{"type":"composer","url":"https://packagist.phpcomposer.com"}}Autoloading code loader
Composer itself integrates a
autoloader
, supportPSR-4
,PSR-0
,classmap
,files autoloading
.Here is an example to illustrate how to reference through Composer
classmap
,files
,本地符合PSR-4标准的代码
- 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 PSR-4, it is equivalent to register a PSR-4 autoloader (from the
FooBar
namespace).
If you do not want to use Composer's autoloader, you can directly include thevendor/composer/autoload_*.php
file and configure your own loader.
The specific example is hosted on github , Available for reference.Repositories
关于Repositories,了解其不是必须的,但是假如掌握则更能理解Composer,对于Repositories,其中文文档和英文文档解释的很好,这里也进行了一些摘抄.
基本概念
包:
Composer是一个依赖管理工具,它在本地安装一些资源包和包的描述(比如包名称和对应的版本),比较重要的元数据描述是dist
和source
,dist
指向一个存档,该存档是对一个资源包的某个版本的数据进行的打包.source
指向一个开发中的源,这通常是一个源代码仓库(比如git)资源库:
一个资源库是一个包的来源.它是一个packages/versions
的列表.
Composer将查看所有你定义的repositories
以找到项目需要的资源包(这句话很重要).
默认情况下已经将Packagist.org注册到Composer(或者理解为Packagist.org是Composer资源库默认的仓库类型)Composer资源库类型
Composer资源库包括四种类型,默认的是composer类型
,也就是packagist.org所使用的资源类型.
它使用一个单一的packages.json
文件,包含了所有的资源包元数据.当你将包发布到pckagist.org上,则默认系统会创建一个packages.json
,不过我没有找到我的包对应的文件.VCS资源库类型
假如你想构建一个私有的Composer私有资源库类型,可以使用该类型,这里举一个例子,比如你在自己项目的composer.json
定义如下,则就可以使用对应的Github上的代码了.1234567891011{"repositories":[{"type":"vcs","url":"http://www.renti51.cn"}],"require":{"ywdblog/phpcomposer":"dev-master"}}当运行
composer update
的时候,Comoser实际上是从Github上下载包而不是从pckagist.org上下载.另外假如需要使用
Package资源库类型
或者PEAR资源库类型
,参考官方文档即可,一般在composer.json
中定义name、version属性即可.Composer.json
在本文上面也多次提到了
composer.json
,比如你希望使用第三方包则需要在本地定义composer.json
,Composer安装第三方包后,也会在第三方包目录下发现composer.json
,那么这二者都叫composer.json
,有什么区别呢?理解这非常的重要.假如你在自己的项目下面定义一个
composer.json
,则这个包称之为ROOT包
,这个composer.json
定义你项目需要的条件(比如你的项目可能依赖一个第三方包).composer.json
中有些属性只能被ROOT包
使用,比如config
属性只在ROOT包
中生效.一个资源包是不是
ROOT包
,取决于它的上下文,比如你git clone ywdblog/phpcomposer
,则这时候本地phpcomposer目录就是ROOT包
,假如你在本地phpcomposer目录下composer require ywdblog/phpcomposer
,则这时候你的项目phpcomposer就是ROOT包
.了解
composer-schema.json
可参考该网址,Laravel作为一个成熟的框架,其定义的composer.json非常经典关于包的版本
当使用者在本地配置composer.json
的时候,可以指定需要包的特定版本,Composer支持从Github仓库中下载Tag
或者分支
下的包.对于Github上的Tag来说,Packagist会创建对应包的版本,它符合
X.Y.Z
,vX.Y.Z
,X.Y.Z-包类型
,就是说Github上虽然只有一个特定版本的包,但Composer支持多种形式的引用方式,比如:1234composer require monolog/monolog 1.0.0-RC1composer require monolog/monolog v1.0.0-RC1composer require monolog/monolog 1.0.*composer require monolog/monolog ~1.10对于Github上的分支来说,Packagist会创建对应包的版本,假如分支名看起来像一个版本,将创建
{分支名}-dev
的包版本号,如果分支名看起来不像一个版本号,它将会创建dev-{分支名}
形式的版本号12composerrequiremonolog/monolog master-devcomposerrequiremonolog/monolog master.x-dev总结:
理解Composer,最重要的是实践,最后也能明白
PSR-4
和命名空间,也可以尝试将你的项目发布到pckagist.org上.来自:http://www.girl4493.cn
- Edit composer.json _