composer-php for managing dependencies (dependency)
Defined
- Composer is a tool used in PHP to manage dependency (dependency) relationships. You can declare your dependencies on the External tool Library (libraries) in your project, and composer will help you install these dependent library files.
- Composer need to php5.3.2+ above the environment to run. There are several sensitive PHP settings and compilation flags that are required, but the installer warns you when there are any incompatibilities.
The Composer is a multi-platform compatible operation for Windows,linux and OSX.
Installation
Linux
- Curl-ss Https://getcomposer.org/installer | Php
- You can specify the installation directory for Composer (which can be an absolute or relative path) by using the--install-dir option
- Global Installation
MV Composer.phar/usr/local/bin/composer
Now just run the composer command to use composer without the need to enter PHP Composer.phar.
- Windows
Download Https://getcomposer.org/Composer-Setup.exe
Windows may need to set environment variables for composer
- Composer-v View version number
Composer-help Viewing command Help
- Reference documents
http://docs.phpcomposer.com/
Composer use of Chinese mirrors
http://pkg.phpcomposer.com/
Composer.json File Description
Require key
{ "require": { "monolog/monolog": "1.0.*" }}
- Require requires a package name (for example, Monolog/monolog) to map to the object of the package version (for example, 1.0.*).
- The package name is comprised of the vendor name and its project name
Version operators
- Exact version 1.0.2
- Wildcard 1.0.* all versions of 1.0
- Range >, >=, <, <=,! =, you can define multiple ranges, separated by commas, which are treated as a logical and. A pipe symbol | will be processed as a logical OR.
- The assignment operator ~1.2,~1.2 is equivalent to >=1.2,<2.0, allowing any version of more than 1.2, but excluding 2.0. You'll also see another usage of it, using ~ To specify the minimum version, but allow the last digit of the version number to rise.
Perform installation of PHP Composer.phar install
- Following the previous example, this will find the latest version of Monolog/monolog and download it to the vendor directory. It is a convention to put a third-party code into a specified directory vendor. If it is Monolog, the Vendor/monolog/monolog directory will be created.
- The other thing is that the install command will create a composer.lock file to your project's root directory.
- If you are using Git to manage your project, you may want to add vendor to your. gitignore file. You don't want to add all of your code to your repository.
Composer.lock-Lock file
- After the installation is dependent, Composer will write the exact version number list at the time of installation to the Composer.lock file. This locks the specific version of the project.
- Use the update command. This will get the latest matching version (depending on your Composer.json file) and update the new version into the lock file. PHP Composer.phar Update
- If you only want to install or update a dependency, you can whitelist them:
PHP composer.phar update monolog/monolog [...]
Auto Load
- For the library's automatic loading information, Composer generates a vendor/autoload.php file. You can simply introduce this file and you will get a free auto-load support.
Require ' vendor/autoload.php ';
You can add your own autoloader in the AutoLoad field of Composer.json.
{ "autoload": { "psr-4": {"Acme\\": "src/"} }}
- You can define a mapping from a namespace to a directory. At this point, SRC will be in the root directory of your project, with the vendor folder sibling. For example, the src/foo.php file should contain the Acme\foo class.
After you add the AutoLoad field, you should run the install command again to generate the vendor/autoload.php file.
Referencing this file will also return an instance of Autoloader, where you can store the return value of the containing call in a variable and add more namespaces. This is useful for automatically loading class files in a test suite, for example.
$loader = require 'vendor/autoload.php';$loader->add('Acme\\Test\\', __DIR__);
Using the example
- Use of BASIC commands
A minimized composer.json, switch to the project directory, create a new Composer.json
{ "require-dev": { "phpunit/phpunit": "4.3.*" }}
Execute composer Install
- will create the vendor directory, which is the relevant code.
- The Composer.lock file is created
- Generates a PHP file that vendor/autoload.php is automatically loaded
The above describes the use of PHP composer, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.