As PHP projects grow and mature so does their list of dependencies and third party code. Managing all these external packages during development and especially deploy is not an easy task and can be very error prone. Enter Composer, a tool that allows you to keep a consistent list of dependencies and versions across your whole team and all your environments, managing and making discovery of new libraries a breeze. Let's see how Composer can solve all your problems with a simple command line interface and a json configuration file.
1. Rafael Dohms / @rdohms
C!"p#$r
Putting your dependencies on the score
2. Rafael Dohms
photo credit: Eli White
@rdohms
Evangelist, Speaker and
Contributor.
Developer at WEBclusive.
Enabler at AmsterdamPHP.
3. %$ E&$v'(!r P)(*+
a general introduction to Composer
Ev$r,-', *!"p#)./
the basic stuff you need to know
Up/r'-)./ (! ' M'$0(r!
advanced features for more complex scenarios
1.-)./ ,!2r (2.$
discovering and sharing libraries
5. per project system wide
central repository spread out channels
open acceptance strict standards
6. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
7. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
S!&2()!.:
8. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
S!&2()!.:
PEAR
9. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
S!&2()!.:
PEAR
SVN Externals
10. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
S!&2()!.:
PEAR
SVN Externals
Git Submodules
11. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
S!&2()!.:
PEAR
SVN Externals
Git Submodules
vendor management script
12. Pr!b&$":
I need my team and my deployments to use consistent
versions of the dependencies of my project
S!&2()!.:
PEAR
SVN Externals
Git Submodules
vendor management script
C!"p#$r!
13. A per-project dependency
manager that allows you to
declare a consistent list of
dependencies and versions for
your application, as well as a
consistent way of sharing your
libraries and making them
discoverable using
packagist.org
41. B!!(0(r'pp)./ C!.(r)b2()!.0
gimme dev packages
$ composer.phar create-project dms/dms --dev ~/dev/oss/dms
"require-dev": {
"symfony/symfony": ">=2.1-dev",
"doctrine/orm": "dev-master"
},
“require-dev”: only needed if you are going
to contribute
42. H!w -! I 6.-/&!'- (+$ 6&$0?
PSR-0 and the modern autoloader
46. “autoload”: describes the autoloading needed for your library
"autoload": {
"psr-0": { "MyNamespace": "<root>" },
"classmap": ["src/", "lib/", "Something.php"],
"files": ["src/MyLibrary/functions.php"]
},
47. “autoload”: describes the autoloading needed for your library
"autoload": {
"psr-0": { "MyNamespace": "<root>" }, “psr-0”: PSR-0 Compatible libraries
"classmap": ["src/", "lib/", "Something.php"],
"files": ["src/MyLibrary/functions.php"]
},
48. “autoload”: describes the autoloading needed for your library
"autoload": {
"psr-0": { "MyNamespace": "<root>" }, “psr-0”: PSR-0 Compatible libraries
“classmap”: Old PEAR packages and other
"classmap": ["src/", "lib/", "Something.php"],
libraries
"files": ["src/MyLibrary/functions.php"]
},
49. “autoload”: describes the autoloading needed for your library
"autoload": {
"psr-0": { "MyNamespace": "<root>" }, “psr-0”: PSR-0 Compatible libraries
“classmap”: Old PEAR packages and other
"classmap": ["src/", "lib/", "Something.php"],
libraries
"files": ["src/MyLibrary/functions.php"]
“files”: for php functions or initializations
},
55. “composer.json”: metadata and list of your
dependencies.
composer.json
composer.lock
“composer.lock”: existing dependencies and
current commit hashes.
65. D$v$&!p)./ '. App ). ' ($'"?
Commit you composer.lock file
into the repository, and use
composer install.
66. D$v$&!p)./ '. App ). ' ($'"?
Commit you composer.lock file
into the repository, and use
composer install.
will ensure everyone is on
the same “page”
67. I’" -$v$&!p)./ ' &)br'r,, +$&p!
here are some fields you should care about
69. “name”: this should be unique, pick a good one!
{
"name": "vendor-namespace/package-name",
"type": "symfony-bundle",
"description": "A sample package for examples",
"keywords": ["php", "package"],
"homepage": "http://doh.ms",
"license": "MIT",
"support": {
"email": "support@mylib.com",
"issues": "http://issues.lib.com"
}
"target-dir": "/folder/to/install",
}
70. “name”: this should be unique, pick a good one!
{
"name": "vendor-namespace/package-name",
“type”: will be used for more advanced
"type": "symfony-bundle", “custom”installs
"description": "A sample package for examples",
"keywords": ["php", "package"],
"homepage": "http://doh.ms",
"license": "MIT",
"support": {
"email": "support@mylib.com",
"issues": "http://issues.lib.com"
}
"target-dir": "/folder/to/install",
}
71. “name”: this should be unique, pick a good one!
{
"name": "vendor-namespace/package-name",
“type”: will be used for more advanced
"type": "symfony-bundle", “custom”installs
"description": "A sample package for examples",
"keywords": ["php", "package"],
"homepage": "http://doh.ms",
"license": "MIT", “license”: very important!
"support": {
"email": "support@mylib.com",
"issues": "http://issues.lib.com"
}
"target-dir": "/folder/to/install",
}
72. “name”: this should be unique, pick a good one!
{
"name": "vendor-namespace/package-name",
“type”: will be used for more advanced
"type": "symfony-bundle", “custom”installs
"description": "A sample package for examples",
"keywords": ["php", "package"],
"homepage": "http://doh.ms",
"license": "MIT", “license”: very important!
"support": {
"email": "support@mylib.com",
“support”: point people the right way.
"issues": "http://issues.lib.com"
}
"target-dir": "/folder/to/install",
}
73. “name”: this should be unique, pick a good one!
{
"name": "vendor-namespace/package-name",
“type”: will be used for more advanced
"type": "symfony-bundle", “custom”installs
"description": "A sample package for examples",
"keywords": ["php", "package"],
"homepage": "http://doh.ms",
"license": "MIT", “license”: very important!
"support": {
"email": "support@mylib.com",
“support”: point people the right way.
"issues": "http://issues.lib.com"
}
"target-dir": "/folder/to/install",
}
“target-dir”: great for installing sub-dir splits repositories
ex: Symfony Bundles: /Acme/Bundle/MyBundle
74. M, *!-$ )0 PHP 5.4 !.&,!
managing system dependencies
83. “#<ref>”: Get this specific commit
"acme/foo": "1.0.x-dev#3ebbe75"
"acme/foo": "@dev"
"acme/foo": "1.0.*@beta"
84. “#<ref>”: Get this specific commit
"acme/foo": "1.0.x-dev#3ebbe75"
"acme/foo": "@dev"
"acme/foo": "1.0.*@beta"
“@<state>”: Get a version outside
your default stability
85. I .$$- (! 7$*2($ ' f$w 0*r)p(0
how to automate tasks with Composer
104. alias
{
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"require": {
"monolog/monolog": "dev-bugfix as 1.0.x-dev"
}
}
replace provide
105. alias
{
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"require": {
"monolog/monolog": "dev-bugfix as 1.0.x-dev"
}
}
replace provide
“replace”: allows you to
replace other packages, and
be used them in their place.
106. alias
{
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"require": {
"monolog/monolog": "dev-bugfix as 1.0.x-dev"
}
}
replace provide
“replace”: allows you to “provide”: allows you to say
replace other packages, and a package provides a
be used them in their place. expectation.
112. $ composer.phar search filter
dms/dms-filter-bundle : DMS Filter Bundle, makes Annotation based ...
lexik/form-filter-bundle : This bundle aim to provide classes to build...
rollerworks/recordfilter-bundle : Record search-filtering bundle for Symfony
brikou/zend_filter : Zend Framework Filter Library
ext-filter : The filter PHP extension
dms/dms-filter : DMS Library, includes various bundles and ...
shtumi/useful-bundle : Symfony ShtumiUsefulBundle
121. U0)./ ,!2r !w. S'()0
{
"repositories": [ “composer”: use this just like it was Packagist
{
"type": "composer",
"url": "http://packages.yourdomain.net"
}
],
“require”: {
“myvendor/mypackage”: “dev-master”
}
}
122. W+$r$ 0+!2&- I /$( +$&p?
http://getcomposer.org
#composer on irc.freenode.org