Home > Backend Development > PHP Tutorial > The role of composer.lock file, the role of composer.lock_PHP tutorial

The role of composer.lock file, the role of composer.lock_PHP tutorial

WBOY
Release: 2016-07-12 08:59:23
Original
1002 people have browsed it

The role of composer.lock file, the role of composer.lock

Basic use of Composer

Use composer.json in your project

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


require keyword

The first thing to do in composer.json is to use the require keyword. You will tell composer which packages your project needs

Copy code 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 naming

Basically, the name of the package is the main name/project name (monolog/monolog). The main name must be unique, but the name of the project, which is our package, can have the same name, for example: igorw/json, and seldaek/json

Package version

The version of monolog we need to use is 1.0.*, which means that as long as the version is the 1.0 branch, such as 1.0.0, 1.0.2 or 1.0.99

Two ways of version definition:

1. Standard version: Define a guaranteed version package file, such as: 1.0.2
2. A certain range of versions: Use comparison symbols to define the range of valid versions. Valid symbols include >, >=, <,<=, !=
3. Wildcard: special matching symbol *, for example, 1.0.* is equivalent to >=1.0, 4. The next important version: The best explanation of the ~ symbol is that ~1.2 is equivalent to >1.2,<2.0, but ~1.2.3 is equivalent to >=1.2.3,<1.3 version.

Installation package

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

In this way, it will automatically download the monolog/monolog file to your vendor directory.

The next thing I need to explain is

composer.lock - lock file

After installing all required packages, composer will generate a standard package version file in the composer.lock file. This will lock versions of all packages.

Use composer.lock (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 whether the composer.lock file exists. If it exists, it will download the corresponding version (it will not depend on the configuration in composer.json) , meaning anyone who downloads the project will get the same version.

If composer.lock does not exist, 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 be automatically updated. To upgrade to the new version, just use the update command. In this way, you can get the latest version of the package and also update you. composer.lock file.

$ php composer.phar update
or
$ composer update

Packagist (this should be composer, it feels a bit like a python package, although not so powerful, haha, with this standard, it will definitely be easy for everyone to develop websites in the future, and you can learn from many people's codes, and it will be more Convenient! )
Packagist is the main warehouse of composer. You can check it out. The basis of the composer warehouse is the source code of the package. You can obtain it at will. The purpose of Packagist is to build a warehouse that anyone can use. This means that in your file Any require package is included.

About automatic loading

In order to conveniently load package files, Composer automatically generates a file vendor/autoload.php, which you can conveniently use wherever you need to use 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 use it directly, they have been automatically loaded!

Copy code The code is as follows:
$log = new MonologLogger('name');
$log->pushHandler(new MonologHandlerStreamHandler('app.log', MonologLogger::WARNING));
$log->addWarning('Foo');

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

Copy code The code is as follows:
{
"autoload": {
"psr-0": {"Acme": "src/"}
}
}

Composer will register psr-0 as the Acme namespace

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

After you add autoload, you must reinstall to generate the vendor/autoload.php file

When we reference this file, an autoloader class will be returned, so you can put the returned value into a variable and then add more namespaces. This is very convenient if you are in a development environment , for example:
Copy code The code is as follows:
$loader = require 'vendor/autoload.php';
$loader->add('AcmeTest', __DIR__);

The role of composer.lock file

The

install command reads the composer.json file from the current directory, handles dependencies, and installs it into the vendor directory.

Copy code The code is as follows:
composer install

If the composer.lock file exists in the current directory, it will read the dependency version from this file instead of obtaining the dependency from the composer.json file. This ensures that every consumer of the library gets the same dependency version.

If there is no composer.lock file, composer will create it after handling dependencies.

In order to get the latest versions of dependencies and update the composer.lock file, you should use the update command.

Copy code The code is as follows:
composer update

This will resolve all dependencies of the project and write the exact version number to composer.lock.

If you just want to update a few packages, you can list them individually like this:

Copy code The code is as follows:
composer update vendor/package vendor/package2

You can also use wildcards for batch updates:

Copy code The code is as follows:
composer update vendor/*

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1098975.htmlTechArticleThe role of composer.lock file, the role of composer.lock The basic use of Composer Use composer.json in the project To use composer, you need to have a composer.json file, this article...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template