Home >Development Tools >composer >[Composer] PHP developers must understand!
The following column will introduce Composer in detail in the composer usage tutorial column. I hope it will be helpful to friends in need!
Composer is a very popular PHP package dependency management tool. It has replaced the PEAR package manager. Mastering Composer is a must for PHP developers. .
For users, Composer is very simple. With a simple command, the required code package is downloaded to the vendor directory, and then the developer can introduce the package and use it.
The key is the composer.json defined by your project. You can define the packages that the project needs to depend on (there may be multiple), and the dependent packages may depend on other packages (this is the benefit of components). You don’t need to worry about these. , Composer will automatically download everything you need, everything lies in the definition of composer.json.
Composer is very transparent to users, but the concept behind it still needs to be understood, and its birth is not Occasionally, thanks to the rapid development of Github, the PHP language has become more and more modern and appears higher.
In order to understand Composer, first have a general understanding of its structure:
The structure of Composer
Composer command line tool:
This understanding is relatively simple, through the user-defined Composer. json to download the code you need. If you simply use Composer, then mastering some specific commands is enough.
Autoloading code loader:
Through Composer, Developers can use it in a variety of ways, and the key lies in the namespace concept of PHP and the development of the PSR-4 standard. Composer has only developed a code autoloader based on these two
Github:
With Github, PHP developers can host open source code on it, and the development of Composer originated from Github. Composer essentially downloads the code on Github locally.
Packagist:
For users, 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? It mainly relies on Packagist, which is Composer's main package information repository. Package developers host specific codes on Github and submit package information to Packagist, so that users can use it through Composer.
Composer queries Packagist based on the locally defined composer.json information. Packagist parses the Composer.json/Package.json information and finally corresponds to the github repository. When Composer finally downloads the code, it also depends on Composer.json on the Github repository. , three types of composer.json are involved here, and the meanings are different.
Composer.json:
This is the core of Composer and the rules of Composer , the three types of Composer.json are also mentioned above. You must pay attention to the distinction when using them. I always messed up when I first learned.
Composer Command Line Tool
composer init
Users can create composer.json under their own projects to define the dependency packages of your project, or they can create composer.json interactively through composer init .
composer install
should be the most commonly used command. Composer will install the package according to the local composer.json and put the downloaded package into the vendor directory under the project. At the same time, put the package version information during installation into composer.lock to lock the version.
In fact, during installation, if it is found that the composer.lock version is consistent with the code version in the current vendor directory , then Composer will do nothing. The purpose of composer.lock is to allow you to 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? Use this command to update the latest version of the package
composer config
This command It is still recommended to understand that the global configuration is stored in COMPOSER_HOME/config.json, and 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 still very important. Use The ordinary install command downloads all the dependent packages of the project to the vendor directory of the project. This command puts all the code and its dependent packages into one directory, which is equivalent to executing a git clone command. Generally, The developer of the package may use this command to fix bugs.
composer global
This is a global installation command that allows you to execute it in the COMPOSER_HOME directory Composer commands, such as install, update. Of course, your COMPOSER_HOME must be in the $PATH environment.
For example, execute composer global require fabpot/php-cs-fixer, and now the php-cs-fixer command line can run globally If you want to update it later, just run composer global update
composer dump-autoload
当你修改项目下的composer.json的文件,并不一定要运行composer update命令进行更新,有的时候可以使用该命令来更新加载器,比如你要引用本地自定义的包(不是来自于packagist),后面会通过实践来说明该命令.
composer require
假如手动或者交互式创建composer.json文件,可以直接使用该命令来安装包
composer require cerdic/css-tidy:1.5.2 composer require "ywdblog/phpcomposer:dev-master" –prefer-source和–prefer-dist参数
–prefer-dist:对于稳定的包来说,一般Composer安装默认使用该参数,这也能加快安装,比如有可能直接从packagist安装了相应的包,而不用实际去Github上下载包.
–prefer-source:假如使用该参数,则会直接从Github上安装,安装包后vendor目录下还含有.git信息
composer require "ywdblog/phpcomposer:dev-master" --prefer-source #在vendor/ywdblog/phpcomposer目录下含有.git信息
如何给Composer添加代理
在国内使用Composer下载特别慢,可以通过二个方法进行加速
composer config repo.packagist composer “https://packagist.phpcomposer.com“ 编辑composer.json "repositories": { "packagist": { "type": "composer", "url": "https://packagist.phpcomposer.com" } }
Autoloading代码加载器
composer本身集成一个autoloader,支持PSR-4,PSR-0,classmap,files autoloading.
这里通过一个例子来说明通过Composer如何引用classmap,files,本地符合PSR-4标准的代码
编辑composer.json
"autoload": { "classmap": ["othsrc/","classsrc.php"], "files": ["othsrc/filesrc.php"], "psr-4": {"Foo\Bar\": "src"} } composer dump-autoload
通过上述的操作,对于PSR-4来说等同注册了一个PSR-4 autoloader(从FooBar命名空间)
假如不想使用Composer的autoloader,可以直接包含vendor/composer/autoload_*.php文件,配置自己的加载器.
具体的例子托管在github上,可参考.
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上的代码了.
{ "repositories": [ { "type": "vcs", "url": "https://github.com/ywdblog/phpcomposer" } ], "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支持多种形式的引用方式,比如:
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
对于Github上的分支来说,Packagist会创建对应包的版本,假如分支名看起来像一个版本,将创建{分支名}-dev的包版本号,如果分支名看起来不像一个版本号,它将会创建dev-{分支名}形式的版本号
composer require monolog/monolog master-dev composer require monolog/monolog master.x-dev
总结:
理解Composer,最重要的是实践,最后也能明白PSR-4和命名空间,也可以尝试将你的项目发布到pckagist.org上.
The above is the detailed content of [Composer] PHP developers must understand!. For more information, please follow other related articles on the PHP Chinese website!