Php developers and composer have to tell stories, and composer has to say

Source: Internet
Author: User
Tags autoload autoloader composer install

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 packagevendorDirectory, then the developer can introduce the package and use it.
The key lies in the definition of your project.composer.jsonYou 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.jsonDownload 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.jsonInformation to query Packagist, Packagist accordingComposer.json/Package.jsonInformation parsing will eventually correspond to the github repository, and Composer will depend onComposer.json, Three typescomposer.jsonThe meaning is different.
  • Composer. json:
    This is the core of Composer and is the Composer rule. Three typesComposer.jsonBe 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.jsonTo define the dependency packages of your project, you can also usecomposer initInteractive Creationcomposer.json.

Composer install
It should be the most common command, and composer willcomposer.jsonInstallation Package, put the downloaded package intovendorDirectory, and add the package version information during installationcomposer.lockTo lock the version.
In fact, during the install, if you findcomposer.lockVersion and currentvendorThe Code versions in the directory are the same, so Composer does nothing,composer.lockThe purpose is to allow you to work in the current version without obtaining the latest package.

Composer update
So how to updatecomposer.lockTo 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.jsonNon-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.vendorDirectory. With this command, all the code and its dependent packages are put in a directory, which is equivalent to executinggit cloneCommand. Generally, the package developer may use this command to fix the bug.

Composer global
This is a global installation command that allows youCOMPOSER_HOMEDirectory to execute the Composer command, suchinstall,updateOf course, yoursCOMPOSER_HOMETo$PATHEnvironment.

For examplecomposer global require fabpot/php-cs-fixerNow 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.jsonFile, not necessarily runcomposer updateCommand 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.jsonFile, 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 packagevendorThe 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 Loader

    Composer 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 (fromFooBarNamespace)
    If you do not want to use Composer autoloader, you can directly includevendor/composer/autoload_*.phpFile, configure your own loader.
    The specific example is hosted on github. For details, refer.

    Repositories

    For 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 isdistAndsource,distPoint to an archive. This archive is used to package data of a specific version of a resource package.sourcePoint 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 definedrepositoriesTo 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 typeThat is, the resource type used by packagist.org.
    It uses a singlepackages.jsonFile, including all the resource package metadata. When you publish the package to pckagist.org, the system will createpackages.jsonBut 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.jsonIf 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 updateComoser actually downloads the package from Github instead of pckagist.org.

    In addition, if you want to usePackage resource library typeOrPEAR resource library type, Refer to the official documentation, generally incomposer.jsonTo define the name and version attributes.

    Composer. json

    As mentioned abovecomposer.jsonFor example, if you want to use a third-party package, you need to define it locallycomposer.jsonAfter Composer installs a third-party package, it will also be found in the third-party package directorycomposer.json, Then both are calledcomposer.jsonWhat is the difference? Understanding this is very important.

    Assume that you definecomposer.json, This package is calledROOT package, Thiscomposer.jsonDefine the conditions required by your project (for example, your project may depend on a third-party package ).

    composer.jsonSome attributes can only beROOT packageFor exampleconfigOnlyROOT package.

    Is a resource packageROOT packageDepends on its context, such as yougit clone ywdblog/phpcomposerIn this case, the local phpcomposer directory isROOT packageIf you are in the phpcomposer directorycomposer require ywdblog/phpcomposerIn this case, your project phpcomposer isROOT package.

    Understandingcomposer-schema.jsonFor details, refer to this URL. Laravel is a mature Framework and Its composer. json definition is very classic.

    Package version
    When the user configurescomposer.jsonYou can specify the specific version of the package to be downloaded from the Github repository.TagOrBranchPackage.

    For the Tag on Github, Packagist creates the version of the corresponding package, which matchesX.Y.Z,vX.Y.Z,X.Y. Z-Package TypeThat 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}-devIf 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-4And namespace, you can also try to publish your project to pckagist.org.

    From: http://www.girl4493.cn

  •  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.