Composer PHP Dependency Manager notes
Composer Install
================
Download: curl -s https://getcomposer.org/installer | php
Install: mv composer.phar /usr/local/bin/composer
composer.json
=============
contains information of project dependencies
Example (Remember - no comments in json):
{
"name": "<Namespace>/<Project>", //Identify own package, optional, use GitHub account for Namespace
"version" : "<version>", //Own package version, optional
"require": {
"<namespace>/<project>": "<version>"
},
"minimum-stability": "dev" //Set minimum stability across the project, default is stable, optional
"require-dev" : { //Dev only dependencies (use --no-dev to skip)
"<namespace>/<project>": "<version>"
}
}
Version numbers can be specified in the following ways:
Examples
Exact version 1.0.2
Range >=1.0 >=1.0 <2.0 >=1.0 <1.1 || >=1.2
Hyphen Range 1.0 - 2.0
Wildcard 1.0.*
Tilde Operator ~1.2
Caret Operator ^1.2.3
- Ranges can use || (or), space and comma (and)
- Tilde states >= up to the next major version according to specificity, e.g:
- ~1.2 -> >=1.2 <2.0
- ~1.2.3 -> >=1.2.3 <1.3.0
- Caret is the same as tilde but ignores specificity, e.g.
- ~1.2.3 -> >=1.2.3 <2.0
Composer Commands
=================
(In project directory)
composer install - Install dependencies, create aut0loader
- Use --no-dev to skip dependencies in require-dev section of composer.json
composer update - Update dependencies
- Use --no-dev to skip dependencies in require-dev section of composer.json
composer validate - Validate composer.json formatting
composer init - Create composer.json interactively
Composer Packaging
==================
Official repo = https://packagist.org/
Additional items in composer.json to define own package as above
Common Libraries
================
monolog/monolog
PSR-0 - Autoloading Standard
============================
- Composer follows PSR-0 which defines a standard structure for libraries to enable easy autoloading
- Autoloader is created when issuing the install command
To use libraries in a script:
require 'vendor/autoload.php';
Example expected structure (File name is Classname, note underscore behaviour):
\Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\Symfony\Core\Request => /path/to/project/lib/vendor/Symfony/Core/Request.php
\Zend\Acl => /path/to/project/lib/vendor/Zend/Acl.php
\Zend\Mail\Message => /path/to/project/lib/vendor/Zend/Mail/Message.php
\namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php
\namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php