COMPOSER
The missing package manager for PHP
PyPi
Common Web Languages
Communities inside PHP
SHARING WASN’T CARING
• Autoloading PHP Classes	

• Dependencies?	

• Where does this thing go?
WHY BOTHER?
• As a developer, you want to use consistent
version of libraries/dependencies and tools	

• Consistent versioning across all team members
• Share your own library
WHY BOTHER?
More developers, more dependency problem
PHPUnit 3.6.10	

Behat 2.3.5	

Monolog 1.0
PHPUnit 3.4.15	

Behat 2.2	

Monolog 1.2
Hasin Rifat
SOLUTIONS
• PEAR	

• SVN Externals	

• Git Submodules	

• Vendor Management Scripts
SOLUTIONS
• PEAR	

• SVN Externals	

• Git Submodules	

• Vendor Management Scripts
SOLUTIONS
• PEAR	

• SVN Externals	

• Git Submodules	

• Vendor Management Scripts
SOLUTIONS
• PEAR	

• SVN Externals	

• Git Submodules	

• Vendor Management Scripts
SOLUTIONS
• PEAR	

• SVN Externals	

• Git Submodules	

• Vendor Management Scripts
SOLUTIONS
• PEAR	

• SVN Externals	

• Git Submodules	

• Vendor Management Scripts
Composer!
Isn’t PEAR and the PEAR installer the
de-facto PHP package manager?
Isn’t PEAR and the PEAR installer the
de-facto PHP package manager?
Yep
Why not PEAR?
• PEAR tends to be used globally*	

• Composer is intended to be used in a 

per-project manner	

• Spread out channels	

• Strict Standards
* pear can be installed per-project. But we are not gonna talk about it.
COMMON SOLUTION
$ cd ~/projects/my_awesome_project/vendor/	
$ mkdir Behat && cd Behat	
$ wget https://github.com/downloads/Behat/Behat/behat.phar	
$ cd ../../	
$ php vendor/Behat/behat.phar tests/features/
COMMON SOLUTION
$ cd ~/projects/my_awesome_project/vendor/	
$ mkdir Behat && cd Behat	
$ wget https://github.com/downloads/Behat/Behat/behat.phar	
$ cd ../../	
$ php vendor/Behat/behat.phar tests/features/
Or use a git submodule…	

or an SVN external if it’s still 2007
COMMON SOLUTION
$ cd ~/projects/my_awesome_project/vendor/	
$ mkdir Behat && cd Behat	
$ wget https://github.com/downloads/Behat/Behat/behat.phar	
$ cd ../../	
$ php vendor/Behat/behat.phar tests/features/
Or use a git submodule…	

or an SVN external if it’s still 2007
BUT, we can do better…
http://getcomposer.org
Install Composer
Install Composer
With PEAR
Install Composer
With PEAR
For MaximumTrolling
Install Composer
With PEAR
For MaximumTrolling
Install Composer
With PEAR
For MaximumTrolling
I am
kidding!
INSTALLING COMPOSER
INSTALLING COMPOSER
Locally

$ curl -sS https://getcomposer.org/installer | php
INSTALLING COMPOSER
Locally

$ curl -sS https://getcomposer.org/installer | php
INSTALLING COMPOSER
Locally

$ curl -sS https://getcomposer.org/installer | php
Globally

$ curl -sS https://getcomposer.org/installer | php

$ mv composer.phar /usr/local/bin/composer
NowYou Can Use Composer
I Am Building A Project
How Composer can help me?
COMPOSER 101
Getting the dependencies
COMPOSER 101
Getting the dependencies
$ cd ~/Desktop/composer-test/
COMPOSER 101
Getting the dependencies
$ cd ~/Desktop/composer-test/
$ vi composer.json
COMPOSER 101
Getting the dependencies
$ cd ~/Desktop/composer-test/
$ vi composer.json put this in your project root
COMPOSER 101
Getting the dependencies
$ cd ~/Desktop/composer-test/
$ vi composer.json put this in your project root
COMPOSER 101
Getting the dependencies
$ cd ~/Desktop/composer-test/
$ vi composer.json put this in your project root
• Exact version
• Range
• Wildcard
• Tilde
COMPOSER 101
Getting the dependencies
$ cd ~/Desktop/composer-test/
$ vi composer.json
$ composer.phar install
put this in your project root
• Exact version
• Range
• Wildcard
• Tilde
Your dependency
Your dependency
Your dependency’s 	

dependencies
Your dependency
Your dependency’s 	

dependencies
Your dependency
Your dependency’s 	

dependencies
Your dependency
Suggested Package
Checking Server Requirements
I need your server to have these…
File Organisation
vendor/autoload.php
UsingThe Libraries
UsingThe Libraries
require_once 'vendor/autload.php';
UsingThe Libraries
require_once 'vendor/autload.php';
Include the libraries
UsingThe Libraries
require_once 'vendor/autload.php';
Include the libraries
Start using it
UsingThe Libraries
require_once 'vendor/autload.php';
Include the libraries
Start using it
As simple as that
Installing, Updating And Moving
On
How does Composer guarantee consistency?
composer.json	

composer.lock
composer.json	

composer.lock
Metadata and list your dependencies
composer.json	

composer.lock
Metadata and list your dependencies
Existing dependencies and current
commit hashes
composer.json	

composer.lock
composer.json	

composer.lock
Update
composer.json	

composer.lock
Update
composer.json	

composer.lock
Update
reads
composer.json	

composer.lock
Update
reads
composer.json	

composer.lock
Update
reads
gets latest
composer.json	

composer.lock
Update
reads
gets latest
composer.json	

composer.lock
Update
reads
gets latest
writes
composer.json	

composer.lock
Update Install
reads
gets latest
writes
composer.json	

composer.lock
Update Install
reads
gets latest
writes
composer.json	

composer.lock
Update Install
reads
gets latest
writes
reads
composer.json	

composer.lock
Update Install
reads
gets latest
writes
reads
composer.json	

composer.lock
Update Install
reads
gets latest
writes
reads
compares
composer.json	

composer.lock
Update Install
reads
gets latest
writes
reads
compares
composer.json	

composer.lock
Update Install
reads
gets latest
writes
reads
compares
gets locked version
KeepTheTeam OnThe Same Page
• Commit composer.lock into the repository	

• Use composer install
• Ignore the vendor directory
A Contributor Appears
A Contributor Appears
$ git clone git@github.com:tareq1988/composer-test.git
A Contributor Appears
$ git clone git@github.com:tareq1988/composer-test.git
$ composer install
A Contributor Appears
$ git clone git@github.com:tareq1988/composer-test.git
$ composer install
A Contributor Appears
$ git clone git@github.com:tareq1988/composer-test.git
$ composer install
A Contributor Appears
$ git clone git@github.com:tareq1988/composer-test.git
$ composer install
Lock File
Adding A New Package
$ composer.phar require symfony/yaml=~2.1.0
Finding A Package
https://packagist.org/
How Do I LoadThe Files?
PSR-0 and the modern autoloader
Composer Generates A
Autoload File For AllYour
Dependencies
vendor/autoload.php
Types Of Autoloading
PSR-4 is the recommended way
Types Of Autoloading
PSR-4 is the recommended way
Types Of Autoloading
PSR-0 auto loading
PSR-4 is the recommended way
Types Of Autoloading
PSR-0 auto loading
PEAR packages and other libraries
PSR-4 is the recommended way
Types Of Autoloading
PSR-0 auto loading
PEAR packages and other libraries
PHP Functions and helpers
PSR What?
PHP FIG: PHP Framework Interop Group
PSR-0:Autoloading Standard

PSR-1: Basic Coding Standard

PSR-2: Coding Style Guide

PSR-3: Logger Interface

PSR-4: Improved Autoloading:
http://www.php-fig.org/
PSR What?
PHP FIG: PHP Framework Interop Group
PSR-0:Autoloading Standard

PSR-1: Basic Coding Standard

PSR-2: Coding Style Guide

PSR-3: Logger Interface

PSR-4: Improved Autoloading:
http://www.php-fig.org/
Post Standards Recommendation
OptimisingThe Autoloader
• Adding huge library slows down	

• It calls file_exists every time
OptimisingThe Autoloader
• Adding huge library slows down	

• It calls file_exists every time
$ composer dump-autoload -o
OptimisingThe Autoloader
• Adding huge library slows down	

• It calls file_exists every time
$ composer dump-autoload -o
OptimisingThe Autoloader
• Adding huge library slows down	

• It calls file_exists every time
$ composer dump-autoload -o
Optimize flag
OptimisingThe Autoloader
• Adding huge library slows down	

• It calls file_exists every time
$ composer dump-autoload -o
Optimize flag
A bit slower, but performance boost
I Am Developing A Library
How Composer can spread my library around?
https://packagist.org/about

http://semver.org/
https://packagist.org/about

http://semver.org/
Name of the library
https://packagist.org/about

http://semver.org/
Name of the library
Description
https://packagist.org/about

http://semver.org/
Name of the library
Description
tag
https://packagist.org/about

http://semver.org/
Name of the library
Description
tag
About you
https://packagist.org/about

http://semver.org/
Name of the library
Description
tag
About you
Autoloader type
I Have A Private Repository
Will it work if I am developing applications in-house?
• Composer 	

• VCS	

• PEAR
https://getcomposer.org/doc/05-repositories.md
Find Out More
http://getcomposer.org	

https://packagist.org	

https://github.com/composer	

composer-dev google group	

#composer & #composer-dev
Can I Use Composer with
WordPress?
http://wpackagist.org/	

http://composer.rarst.net/resources
Thats All
Tareq Hasan
@tareq_cse
Founder, Lead Developer - weDevs
http://tareq.weDevs.com
http://wedevs.com
Thanks

Composer - The missing package manager for PHP