Composer
A composer is a tool for dependency management in PHP.
Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.
It was developed by Nils Adermann and Jordi Boggiano, who continue to manage the project.
Installation:
- Download Windows installer Composer-Setup.exe from the link https://getcomposer.org/download/
- Run the Composer-Setup.exe file
- You must have PHP installed on your system as Composer requires PHP 5.3.2+ to run.
- Set PHP path c:\xampp\php\php.exe while setup.
- Finish
- Check installed composer version using the command
C:\Users\username>composer — version
Composer version 1.8.4 2019–02–11 10:52:10
Usage:
To start using Composer in your project, all you need is a composer.json
file.
- composer.json:
This file describes the dependencies of your project and may contain other metadata as well. - require key:
The first thing you specify incomposer.json
is therequire
key. You are simply telling Composer which packages your project depends on.
{
"require": {
"monolog/monolog": "1.0.*"
}
}
As you can see, require
takes an object that maps package names (e.g. monolog/monolog
) to version constraints (e.g. 1.0.*
).
- vendor directory:
Thevendor
directory is the conventional location for all third-party code in a project.
In our example from above, you would end up with the Monolog source files invendor/monolog/monolog/
. If Monolog listed any dependencies, those would also be in folders undervendor/
.
Autoloading
For libraries that specify autoload information, Composer generates a vendor/autoload.php
file. You can simply include this file and start using the classes that those libraries provide without any extra work:
require __DIR__ . '/vendor/autoload.php';
$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
$log->addWarning('Test');
You can even add your own code to the autoloader by adding an autoload
field to composer.json
.
{
"autoload": {
"psr-4": {"DemoPackage\\": "src/"}
}
}
Composer will register a PSR-4 autoloader for the DemoPackage
namespace.
You define a mapping from namespaces to directories. The src
directory would be in your project root, on the same level as vendor
directory is. An example filename would be src/Test.php
containing an DemoPackage\Test
class.
After adding the autoload
field, you have to re-run this command:
php composer.phar dump-autoload
This command will re-generate the vendor/autoload.php
file.
Including that file will also return the autoloader instance, so you can store the return value of the include call in a variable and add more namespaces. This can be useful for autoloading classes in a test suite, for example.
$loader = require __DIR__ . '/vendor/autoload.php';
$loader->addPsr4('DemoPackage\\Test\\', __DIR__);
In addition to PSR-4 autoloading, Composer also supports PSR-0, classmap and files autoloading.
Deprecated — As of 2014–10–21 PSR-0 has been marked as deprecated. PSR-4 is now recommended as an alternative.