Skip to content

Instantly share code, notes, and snippets.

@IngmarBoddington
Last active December 12, 2015 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngmarBoddington/4716014 to your computer and use it in GitHub Desktop.
Save IngmarBoddington/4716014 to your computer and use it in GitHub Desktop.
Composer PHP Dependency Manager notes - Full example json: http://composer.json.jolicode.com/
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment