Detailed description of the basic operation of compoer


The original text is transferred from Wechat Public Number: qq1005349393

Introduction to Composer

Composer is a <font color=’red’> package dependency </font> management tool for PHP. We can declare the external tool libraries we depend on in the project. Composer will help you install these dependent library files. With it, we can easily use a command to refer other people’s excellent code to our project. Composer is not installed globally by default, but based on a directory (such as vendor) of a specified project. Composer requires PHP version 5.3.2 + or more, and OpenSSL needs to be turned on. Composer runs on Windows, Linux and OSX platforms.

Composer installation

1. Windows Installation
On the Wondows platform, we just need to download Composer-Setup.exe and install it step by step. Note that you need to turn on the OpenSSL configuration. Let’s open php. ini in the PHP directory and remove the semicolon before extension = php_openssl. dll.
Detailed description of the basic operation of compoer
After the installation is completed, check whether the installation is successful. have access to

composer --version

Command view, as follows:
Detailed description of the basic operation of compoer
2. Linux Installation

// Download the composer file
php -r "copy('', 'composer-setup.php');"
// Install composer using PHP interpreter
php composer-setup.php
// Move to the directory of executable files, so that we can use the composer command directly to make global calls later. 
mv composer.phar /usr/local/bin/composer

3. Mac Os Installation

Direct Installation
// Download and install
curl -sS | php
// Move to the directory of executable files for global invocation
sudo mv composer.phar /usr/local/bin/composer
Installation using brew package management tool on Mac
brew install composer
// Check if the installation is successful
composer --version

4. How to switch the composer mirror source
Now Ali handles its own composer image source, and can synchronize with Packagist official website in real time. It is recommended to use Ali’s composer image source.

// Switching Mirror Source
composer config -g repo.packagist composer
// Unconfigure
composer config -g --unset repos.packagist
// Switch mirror source only for current project (non-global handover)
composer config repo.packagist composer
// Cancel the mirror source for the current project
composer config --unset repos.packagist

5. Compooser updates
Updates to composer can be updated using its own commands

composer selfupdate

Composer uses

In the use of Composer, several common operations are as follows:

1.composer install
When our composer.json file adds project dependencies, the following is true:

    "require": {
        "topthink/framework": "5.0.5",

When we use composer install, we automatically install the corresponding packages according to the dependencies in the packages.

2.composer update
This command updates package versions based on package dependencies and corresponding versions in the composer. JSON configuration file, but it updates all packages to the latest version and requires careful use in actual projects, especially in production environments.

3.composer require
In Point 1, we talked about how to install packages according to the package dependency configuration in the composer. JSON configuration file. This command can install a package manually instead of relying on the configuration file. After installation, the corresponding dependency configuration is automatically added to the composer. JSON configuration file.

4.composer remove
This command removes the specified package dependencies, for example, we do not need to rely on a package to use the command + package name directly

composer remove "topthink/framework": "5.0.5"

Similarly, after executing this command, the package dependencies in the composer. JSON configuration file are automatically deleted without our manual operation.

5.composer show
The main purpose of this command is to view package-related information that has been installed in the current project.

// View information on all installed packages
composer show
// Specify information to view installed packages
composer show topthink/framework

What’s the difference between Composer. JSON and Compposer. lock

In the process of using composer, we will find that when we execute commands such as composer update, we will inadvertently find an additional composer. lock file. What is the main purpose of this document? This file is mainly used to manage the package version. When we use the composer update command, composer automatically generates the corresponding composer. lock file according to the package version dependency of composer. json. The next time we execute the composer command, we will first read the contents of the composer. lock file.

Composer version constraints

When we use composer to install packages, we have to consider a version problem, because different versions have compatibility problems, so we need to pay special attention to the package version when using the tool to install packages. If used improperly, it can easily lead to project paralysis because of the package version problem. The common ones are as follows:
1. Precise Version
Make it clear to install to that version, if you need to install package version 1.2.3

"topthink/think-captcha": "v1.2.3",

2. Wildcards
It can satisfy the specified range, ranging from 5.0 to 5.1 as follows

"topthink/framework": "5.0.*",

3. Scope
The commonly used operators for scopes are >,>=,<,<=,!=. You can define multiple scopes, using spaces or commas to represent logical AND and double vertical lines | | to represent logical OR. The priority of and will be greater than or.

// Represents versions greater than or equal to 0.90 and less than 3.0
"ruflin/elastica": ">=0.90 <3.0",

4. Wave Characters~
This operator limits the minimum version number.

Allow the last version number in the expression to reach the maximum

For example, ~1.2 is equal to >= 1.2 < 2.0, ~1.5.6 is equal to >= 1.5.6 < 1.6.0. That is to say, the major version number and the minor version number remain unchanged, and the repair version number can reach the maximum.
5. Fold notes^
This operator constrains locking the maximum version number.

Lock expression is unchanged by the first major version number, allowing upgraded version to a secure version number

If ^ 1.2 is equal to >= 1.2 < 2.0, then ^ 1.2.3 is equal to >= 1.2.3 < 2.0.


What is semantics? The simpler point is version number management. Our packages are generally divided into the following formats:
PHP main version number + minor version number + repair version
As in the example above, we all mentioned that some packages have version number x.x. The first is the main version number, the second is the secondary version number, and the third is the repair version number for some bugs. Specific reference can be made to

Composer usage optimization

1. Compooser load type
Compooser loading types, including classmap, psr-0, psr-4, file. psr-0, are gradually abandoned, and some projects are still using this rule because some old projects are still using it. Most of them use psr-4. Classmap is the mapping processing of package files, which is described below. File mainly loads some helper operations.
1.composer dump-autoload -o
This command generates a file mapping based on the command space and path of the package, and when loading the package, it loads the package file according to the mapping. This will speed up our package file access. When we execute the command, we can view the following interface. What is circled is the class mapping configuration.
Detailed description of the basic operation of compoer
How composer specifically handles this loading order logic can be seen by looking at the processing order of composer loading classes. The method circled in the following figure is to load the classmap first, but it is not found to load psr-4.
Detailed description of the basic operation of compoer
2.composer dump-autoload -a
The main function of this command is to generate a mapping file when the command is executed in our 1. If the mapping file to load is not found, the package file is prompted not to exist.