The role of composer.lock files, composer.lock effects _php Tutorials

Source: Internet
Author: User
Tags autoload composer install

The role of composer.lock files, Composer.lock role


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 is used primarily to declare the interrelationships between packages and other element tags.


Require keywords

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

Copy the 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 the package

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

Version of the package

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

Versions are defined in two ways:

1. Standard Version: Defines the categorization malleability version package file, such as: 1.0.2
2. A range of versions: Use the comparison symbol to define the range of valid versions, valid symbols are available;, >=, <,<=,! =
3. Wildcard: Special match symbol *, for example 1.0.* is equivalent to >=1.0,<1.1 version
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.

Install package

Run under the project file path
Copy the Code code as follows:
$ composer Install

This way he will automatically download the Monolog/monolog file to your vendor directory below.

The next thing to note is

Composer.lock-Lock file

After all the required packages have been installed, composer will generate a standard package version of the file in the Composer.lock file. This will lock the version 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 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 relative version via 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, using the update command, this will be able to 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's package, although not so strong, hehe, with this standard, after all the development of the site will 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 the basis of the package of source code, you can freely get, packagist the purpose of a person can use the warehouse, This means that any require package is in your file.

About automatic loading

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

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

Copy the 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 Composer.json:

Copy the Code code as follows:
{
"AutoLoad": {
"psr-0": {"ACME": "src/"}
}
}

Composer will register psr-0 as the Acme namespace

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

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

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 it is convenient in the development environment, for example:
Copy the Code code as follows:
$loader = Require ' vendor/autoload.php ';
$loader->add (' acme\test ', __dir__);

The role of the Composer.lock file

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

Copy the Code code as follows:
Composer Install

If the Composer.lock file exists under the current directory, it will read the dependent version from this file, rather than relying on the Composer.json file to obtain the dependency. This ensures that each user of the library will be given the same dependent version.

If you do not have a Composer.lock file, composer will create it after you finish processing the dependency.

In order to obtain the latest version of the dependency and upgrade the Composer.lock file, you should use the Update command.

Copy the Code code as follows:
Composer Update

This will resolve all dependencies on the project and write the exact version number to Composer.lock.

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

Copy the Code code as follows:
Composer Update Vendor/package Vendor/package2

You can also use wildcard characters for batch updates:

Copy the Code code as follows:
Composer Update vendor/*

http://www.bkjia.com/PHPjc/1098975.html www.bkjia.com true http://www.bkjia.com/PHPjc/1098975.html techarticle The role of the Composer.lock file, the Composer.lock effect composer The basic use of Composer.json in the project using composer, you need to have 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.