Role of the composer. lock file, role of composer. lock _ PHP Tutorial

Source: Internet
Author: User
Tags composer install
The role of the composer. lock file and the role of composer. lock. Composer. the role of the lock file, composer. the role of lock is the basic use of Composer in the project. json uses composer in the project. you need a composer. json file, this article composer. the role of the lock file, composer. lock function

Basic use of Composer

Use composer. json in the project

To use composer in a project, you need a composer. json file. This file is mainly used to declare the relationship between packages and other element labels.


Require keyword

The first thing in composer. json is to use the require keyword. you will tell composer which packages are required by your project.

The code is as follows:
{
"Require ":{
"Monolog/monolog": "1. 0 .*"
}
}

As you can see, The require object will map the package name (monolog/monolog) and the package version is 1. 0 .*


Package name

Basically, the package name is the primary name/project name (monolog/monolog). the primary name must be unique, but the project, that is, the package name, can be the same. for example: igorw/json, and seldaek/json

Package Version

We need to use the monolog version 1. 0. *, which means that as long as the version is 1.0 branch, such as 1.0.0, 1.0.2 or 1.0.99

Two versions are defined:

1. Standard version: defines a version package file with minimum guarantee, for example, 1.0.2
2. versions within a certain range: use comparison symbols to define the range of valid versions. Valid symbols include >,>=,<,<= ,! =
3. wildcard: a special matching symbol *. for example, 1. 0. * is equivalent to> = 1.0. <1.1
4. next important version :~ The best explanation of the symbol is ,~ 1.2 is equivalent to> 1.2, <2.0, ~ 1.2.3 is equivalent to> = 1.2.3 and <1.3.

Installation package

Run in the project file path
The code is as follows:
$ Composer install

In this way, he will automatically download the monolog/monolog file to your vendor Directory.

The next thing to note is:

Composer. lock-lock file

After all required packages are installed, composer generates a standard package version file in the composer. lock file. This will lock the versions of all packages.

Use composer. lock (together with composer. json of course) to control the version of your project

This is very important. when we use the install command to process it, it will first determine composer. whether the lock file exists. If yes, the corresponding version will be downloaded (not in composer. json configuration), which means that anyone who downloads the project will get the same version.

If no composer. lock exists, composer will read the required package and relative version through composer. json, and then create the composer. lock file.

In this way, after your package has a new version, you will not automatically update it. upgrade it to a new version and use the update command, in this way, you can get the latest version of the package and update your composer. lock file.

$ Php composer. phar update
Or
$ Composer update

Packagist, I can learn from the code of many people, and it is more convenient !)
Packagist is the main warehouse of composer. you can check it out. the basis of the composer warehouse is the package source code. you can obtain it at will. Packagist aims to build a warehouse that anyone can use, this means any require package in your file.

About automatic loading

To facilitate the loading of package files, Composer automatically generates a file named "vendor/autoload. php". you can only use it wherever you need it.
Require 'vendor/autoload. php ';

This means that you can use third-party code very conveniently. if your project needs to use monlog, you can directly use it. they have all loaded it automatically!

The code is as follows:
$ Log = new Monolog \ Logger ('name ');
$ Log-> pushHandler (new Monolog \ Handler \ StreamHandler ('app. log', Monolog \ Logger: WARNING ));
$ Log-> addWarning ('Foo ');

Of course, you can also load your code in composer. json:

The code is as follows:
{
"Autoload ":{
"Psr-0": {"Acme": "src /"}
}
}

Composer will register the psr-0 as an Acme namespace

You can define a ing to the file directory through the namespace. The src directory is your root directory, and the vendor directory is at the same level. for example, a file is src/Acme/Foo. php contains the Acme \ Foo class.

After you add autoload, you must re-install it to generate the file vendor/autoload. php.

When we reference this file, we will return the strength of an autoloader class, so you can put the returned value into a variable and then add more namespaces, this is very convenient in the development environment, for example:
The code is as follows:
$ Loader = require 'vendor/autoload. php ';
$ Loader-> add ('Acme \ test', _ DIR __);

Roles of composer. lock files

The install command reads the composer. json file from the current directory, processes the dependency, and installs it in the vendor directory.

The code is as follows:
Composer install

If the composer. lock file exists in the current directory, it reads the dependent version from the file instead of obtaining the dependency based on the composer. json file. This ensures that each user of the database can obtain the same dependent version.

If no composer. lock file exists, composer will create it after processing the dependency.

To get the latest dependent version and upgrade the composer. lock file, you should use the update command.

The code is as follows:
Composer update

This resolves all dependencies of the project and writes the exact version number to composer. lock.

If you only want to update several packages, you can list them as follows:

The code is as follows:
Composer update vendor/package vendor/package2

You can also use wildcards for batch update:

The code is as follows:
Composer update vendor /*

Use Composer. json in the project to use composer. json in the project. you need a composer. json file, this article...

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.