PHP developers and composer have to tell a story

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 vendordirectory 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.jsonthe 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.jsonYou 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.jsonComposer.json/Package.jsonComposer.jsoncomposer.json
  • Composer.json:
    This is the core of Composer and the rules of Composer. The three types are also mentioned above. You Composer.jsonmust 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.jsonto define your project's dependencies, or composer initcreate them interactively composer.json.

composer install
should be the most commonly used command, composer will composer.jsonput the downloaded package into the vendordirectory under the project according to the local installation package, and at the same time put the package version information during installation in composer.lockorder 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.lockcurrent vendordirectory, Composer will do nothing, composer.lockthe 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.lockto 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.

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 vendordirectory. With this command, all the code and its dependencies are downloaded. Putting a package in a directory is equivalent to executing a git clonecommand, which is generally used by package developers to fix bugs.

composer global
This is a global installation command, which allows you COMPOSER_HOMEto execute Composer commands in a directory, such as install, update. Of course, yours COMPOSER_HOMEmust be in the $PATHenvironment.

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.jsonthe files under the project, you don't have to run composer updatethe 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

--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 vendordirectory also contains.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

    Autoloading code loader

    Composer itself integrates a autoloader, support PSR-4, PSR-0, classmap, files autoloading.

    Here is an example to illustrate how to reference through Composer classmap, files,本地符合PSR-4标准的代码

    • Edit composer.json _
    • composer dump-autoload

    Through the above operations, for PSR-4, it is equivalent to register a PSR-4 autoloader (from the FooBarnamespace).
    If you do not want to use Composer's autoloader, you can directly include the vendor/composer/autoload_*.phpfile and configure your own loader.
    The specific example is hosted on github , Available for reference.

    Repositories

    关于Repositories,了解其不是必须的,但是假如掌握则更能理解Composer,对于Repositories,其中文英文解释的很好,这里也进行了一些摘抄.

    基本概念
    包:
    Composer是一个依赖管理工具,它在本地安装一些资源包和包的描述(比如包名称和对应的版本),比较重要的元数据描述是distsource,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上的代码了.

    当运行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支持多种形式的引用方式,比如:

    对于Github上的分支来说,Packagist会创建对应包的版本,假如分支名看起来像一个版本,将创建{分支名}-dev的包版本号,如果分支名看起来不像一个版本号,它将会创建dev-{分支名}形式的版本号

     

    总结:

    理解Composer,最重要的是实践,最后也能明白PSR-4和命名空间,也可以尝试将你的项目发布到pckagist.org上.

    来自:http://www.girl4493.cn

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326552579&siteId=291194637