Jordi Boggiano
@seldaek


Introduction to Composer

Composer

Managing
Packages vs Dependencies

The Composer Ecosystem

github.com/composer

The Composer Ecosystem

Composer - CLI Tool

The Composer Ecosystem

Packagist - Package Repository

The Composer Ecosystem

Toran Proxy - Private Repo / Cache

Usage Instructions

Using a Composed Project

git clone https://github.com/symfony/standard-edition myproject
            
Cloning into myproject...
cd myproject/
            
curl -sS https://getcomposer.org/installer | php
            
All settings correct for using Composer Composer successfully installed to: /home/bob/myproject/composer.phar Use it: php composer.phar

Using a Composed Project

composer install
            
Installing from lock file - Package twig/extensions (dev-master) Downloading Unpacking archive Cleaning up [...] - Package twig/twig (1.8.0) Downloading Unpacking archive Cleaning up - Package symfony/symfony (dev-master) Downloading Unpacking archive Cleaning up Generating autoload files

Using a Composed Project

vendor/
    autoload.php
    composer/
    monolog/
        monolog/
    symfony/
        symfony/
        monolog-bundle/
    twig/
        twig/
        extensions/
    [...]
            

One-line Project Initialization

composer create-project <package> [<dir>] [<version>]

composer create-project symfony/framework-standard-edition
            

Downloading Project Dependencies

composer.json

{
    "require": {
        "php": ">=5.4",
        "composer/installers": "^1.0.12",
        "vlucas/phpdotenv": "^2.0.1",
        "johnpbloch/wordpress": "4.2.2"
    },
    "require-dev": {
        "phpunit/phpunit": "^4.5"
    }
}
            

Version Constraints

Exact

1.0.0
dev-master#26622600f
            

Ranges

>=1.0.3 <1.2    // AND
1.* || 2.*      // OR
1.0.*           // >=1.0 AND <1.1
            

Semver Operator

^1.0            // >=1.0 AND <2.0
^1.1            // >=1.1 AND <2.0
^1.2.0          // >=1.2.0 AND <2.0
^1.2.3          // >=1.2.3 AND <2.0
            

Creating a Package Definition

{
    "name": "predis/predis",
    "type": "library",
    "description": "Flexible and feature-complete Redis client",
    "keywords": ["nosql", "redis", "predis"],
    "homepage": "http://github.com/nrk/predis",
    "license": "MIT",
    "authors": [
        {
            "name": "Daniele Alessandri",
            "email": "suppakilla@gmail.com",
            "homepage": "http://clorophilla.net"
        }
    ],
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {"Predis\\": "src/"}
    }
}
            

Note: Package Definition === Application/Root Definition

Note: No version is defined since it is inferred from VCS info

Avoiding version chaos
in your team

composer.lock


Benefits

Autoloading

Libraries/projects define the way they should be loaded:

"autoload": {
    "psr-4": {
        "Acme\\Example\\": "src/"
    },
    "classmap": ["src/", "Class.php"]
},
"autoload-dev": {
    "psr-4": {
        "Acme\\": "tests/"
    }
}
            

Composer builds an autoloader for you:

vendor/autoload.php
                

Use the generated autoloader:

require __DIR__.'/vendor/autoload.php';

use Acme\Example\Foo;

$foo = new Foo();
// ...
                

Alternative Repositories

"repositories": [
    {
        "type": "composer",
        "url": "http://wpackagist.org"
    }
]
            

See docs for more

Wrap up

Install dependencies (from lock file)

composer install
            

Update deps to their latest versions

composer update
            

Install dependencies in production

composer install --optimize-autoloader
composer install -o
            

Composer and WordPress

State of the Project

Adoption

Find Out More

Thank you.

Questions?

jordi@nelm.io

@seldaek

slides.seld.be