Dependency management with Composer
Espen Hovlandsdal
The problem?
The problem
- Keeping dependency code
- Up to date
- Out of version control
- require() gets old
The solution
Composer is...
- Dependency manager
- Not a package manager
- Inspired by
- npm
- ruby's bundler
- a CLI tool
Composer
|
vs |
PEAR
|
- Composer central repository
- 19 854 packages registered
- December 12th 2013
Installing
Locally:
$ curl -sS https://getcomposer.org/installer | php
Globally:
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
Protip:
$ ln -s /usr/bin/composer.phar /usr/bin/composer
Keeping it updated
$ composer self-update
Updating to version ae3b7ad5c07d1df35cf4c2d. Downloading: 100%
Getting started
$ cd <your-project>
$ composer init
[...] Follow step-by-step guide
$ cat composer.json
$ composer install
composer.json
{
"name": "imbo/imboclient",
"require": {
"php": ">=5.3.2",
"guzzle/guzzle": "~3.7.4"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"ImboClient": "library"
}
},
"description": "PHP client for the Imbo-project",
"keywords": ["image storage", "image transformation"],
"license": "MIT",
"authors": [{
"name": "Christer Edvartsen"
}],
"support": {
"issues": "https://github.com/imbo/imboclient-php/issues",
"irc": "irc://irc.freenode.net/imbo"
}
}
The lock file
-
Generated on install
- Ensures same versions
- Updated by composer update
- Add it to version control!
Versioning
- Composer encourages semver
- X.Y.Z / X.Y
- Supports a range of version selectors
- >= 1.2.0
- >= 1.2.4, <=1.4.0
- 1.2.*
- ~1.2.4
Autoloading
- require() is for suckers
- Define an autoloader type
- PSR-0
- Classmap
- Specific files
Autoloading, in use
<?php require BASE_PATH . '/vendor/autoload.php'; use ImboClient\Client as ImboClient, Silex\Application;
$imbo = new ImboClient(); $app = new Application();
Private repositories
- Basically just a JSON file
- Satis will get you started
- Define repository in projects:
"repositories": [
{
"type": "composer",
"url": "http://private.satis.example.org"
},
{
"type": "vcs",
"url": "git://example.org/MyRepo.git"
},
{
"packagist": false
}
]
Bootstrapping projects
$ composer create-project imbo/imbo
- Installs package
- Installs dependencies
- Nice for skeletons etc
Post/pre-scripts
- Run PHP-code
- ...or shell commands
- Lots of flexibility
Publishing
- Sign up on packagist.org
- Add your repository URL
- If using Github, add service hook
- Packagist updated on every push
- Do it! Share your stuff!
That's all!
Thanks.
Questions?
Dependency management: composer
By Espen Hovlandsdal
Dependency management: composer
- 2,725