The function _php Foundation of Composer.lock Document

Source: Internet
Author: User
Tags autoload composer install

Basic use of composer

Using Composer.json in your project

To use composer in your project, you need to have a Composer.json file that functions primarily to declare the interrelationships between packages and some other element tags.


require keyword

The first thing in Composer.json is to use the Require keyword, and you will tell composer which packages are needed for your project

Copy Code code 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.*


Name of package

Basically, the name of the package is the master name/project name (Monolog/monolog), the primary name must be unique, but the project is the same as the names of our packages, for example: Igorw/json, and Seldaek/json

Version of Package

We need to use the Monolog version is 1.0.*, he means as long as the version is 1.0 branches can, such as 1.0.0,1.0.2 or 1.0.99

There are two ways to define a version:

1. Standard Version: Definition of the version package file, such as: 1.0.2
2. A range of versions: Use comparison notation to define the range of valid versions, valid symbols for >=, <,<=,!=
3. Wildcard characters: Special matching symbols *, such as 1.0.* is equivalent to the >=1.0,<1.1 version of the
4. Next important version: ~ Symbol The best explanation 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 under the project file path

Copy Code code as follows:

$ composer Install

This way he will automatically download Monolog/monolog files to your vendor directory.

The next thing to say is that

Composer.lock-Lock file

After all the required packages have been installed, composer generates a standard package version of the file in the Composer.lock file. This will lock the version 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 will first determine whether the Composer.lock file exists, if it exists, will download the corresponding version (not in the Composer.json inside the configuration), This means that anyone who downloads a project will get the same version.

If it does not exist composer.lock,composer will read the required package and the relative version through Composer.json, and then create the Composer.lock file

This way you can have a new version of your package, you will not automatically update, upgrade to a new version, use the update command, you can get the latest version of the package and also updated your Composer.lock file.

$ PHP Composer.phar Update
Or
$ composer Update

Packagist (This should be composer, feel a bit like Python bag, although not so strong, oh, with this standard, after the development of the site will definitely be very easy, you can learn from a lot of people's code, and more convenient! )
Packagist is the main warehouse of composer, we can go to see, composer Warehouse is based on the source of the package, you can be free to obtain, packagist the purpose of building a warehouse that anyone can use, This means that any require package in your file.

About automatic loading

To facilitate loading of package files, composer automatically generates a file vendor/autoload.php that you can conveniently use only in any place you need to use it
Require ' vendor/autoload.php ';

This means that you can use a third party code very, very easily, assuming that your project needs to use Monlog, you directly use it, they have been automatically loaded!

Copy Code code 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 own code in the Composer.json:

Copy Code code as follows:

{
"AutoLoad": {
"psr-0": {"ACME": "src/"}
}
}

Composer will register psr-0 as the Acme namespace

You can define a map by namespace to file directory, src directory is your root directory, vendor is the same level of directory, such as a file is: src/acme/foo.php contains the Acme\foo class

When you add AutoLoad, you have to install to generate vendor/autoload.php files

When we refer to this file, we will return the strength of a autoloader class, so you can put the returned value into a variable and then add more namespaces if this is convenient in the development environment, for example:

Copy Code code as follows:

$loader = Require ' vendor/autoload.php ';
$loader->add (' acme\test ', __dir__);

The role of Composer.lock files

The install command reads the Composer.json file from the current directory, processes the dependencies, and installs them into the vendor directory.

Copy Code code as follows:

Composer Install

If there is a Composer.lock file in the current directory, it will read the dependent version from this file instead of relying on the Composer.json file. This ensures that each user of the library gets the same dependent version.

If there is no Composer.lock file, composer creates it after the dependency is processed.

To get the latest version of the dependency and upgrade the Composer.lock file, you should use the Update command.

Copy Code code as follows:

Composer Update

This resolves all dependencies on the project and writes the exact version number to the Composer.lock.

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

Copy Code code as follows:

Composer Update Vendor/package Vendor/package2

You can also use wildcard characters for batch updates:

Copy Code code as follows:

Composer Update vendor/*

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.