Jordi Boggiano
@seldaek
http://nelm.io/


In-Depth with Composer

Belgian living in Z├╝rich, Switzerland

Building the internet for 10 years
 http://seld.be

Symfony2, Composer and other OSS contributions
 http://github.com/Seldaek

Working at Nelmio
 http://nelm.io
 Symfony2 & frontend performance consulting

Composer

Agenda

Use Cases

Using a forked project

Using a forked project

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/Seldaek/symfony"
        }
    ],
    "require": {
        "symfony/symfony": "dev-master"
    }
}
            

Additional repositories take priority over the default ones

Using a forked project

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/Seldaek/symfony"
        }
    ],
    "require": {
        "symfony/symfony": "dev-my-patch"
    }
}
            

Your branches are available as well

Use composer show -v symfony/symfony to check

Using a forked project

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/Seldaek/symfony"
        }
    ],
    "require": {
        "symfony/symfony": "dev-my-patch as 2.1.0"
    }
}
            

Private packages with Satis

Private packages with Satis

composer.json

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/nelmio/NelmioSecretBundle"
        },
        {
            "type": "vcs",
            "url": "https://github.com/nelmio/lib"
        }
    ],
    "require": {
        "nelmio/secret-bundle": "1.*",
        "nelmio/lib": "1.5.3"
    }
}
            

Private packages with Satis

satis.json

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/nelmio/NelmioSecretBundle"
        },
        {
            "type": "vcs",
            "url": "https://github.com/nelmio/lib"
        }
    ]
}
            
composer create-project composer/satis myrepo
cd myrepo
bin/satis build satis.json www/
            

Private packages with Satis

composer.json

{
    "repositories": [
        {
            "type": "composer",
            "url": "http://satis.example.org/"
        }
    ],
    "require": {
        "nelmio/secret-bundle": "1.*",
        "nelmio/lib": "1.5.3"
    }
}
            

Private packages with Satis

Set-up a system-wide repository

Unix: /home/example/.composer/config.json

Windows: C:\Users\example\AppData\Roaming\Composer\config.json

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

Private packages with Satis

composer.json

{
    "require": {
        "nelmio/secret-bundle": "1.*",
        "nelmio/lib": "1.5.3"
    }
}
            

Proxying with Satis

Proxying with Satis

satis.json

{
    "repositories": [
        {
            "type": "composer",
            "url": "http://packagist.org"
        }
    ],
    "require": {
        "symfony/symfony": "*"
    }
}
            

Renaming packages safely

Renaming packages safely

{
    "name": "new/name",
    "replace": {
        "old/name": "self.version"
    }
}
            
{
    "name": "new/name",
    "replace": {
        "old/name": "*"
    }
}
            

Virtual interface packages

Virtual interface packages

Interface Package

{
    "name": "acme/interface",
    "require": {
        "acme/implementation": "1.0.0"
    }
}
            

Implementer (provider) Package

{
    "name": "zorg/amazing",
    "provide": {
        "acme/implementation": "1.0.0"
    }
}
            

Virtual interface packages

Library

{
    "name": "example/library",
    "require": {
        "acme/interface": "1.0.0"
    }
}
            

Project

{
    "require": {
        "example/library": "*",
        "zorg/amazing": "*"
    }
}
            

Tips & Tricks

Branch Aliases

Branch Aliases

{
    "name": "symfony/symfony",
    "extra": {
        "branch-alias": {
            "dev-master": "2.2.x-dev"
        }
    }
}
            
{
    "require": {
        "symfony/symfony": "2.1.*"
    }
}
            
{
    "require": {
        "symfony/symfony": "2.1.*@dev"
    }
}
            

Version Ranges

Version Ranges

Wrong

{
    "require": {
        "example/library": "1.1.*,1.2.*",
    }
}
            

Right

{
    "require": {
        "example/library": ">=1.1,<1.3-dev",
    }
}
            

Version Ranges

Wrong

{
    "require": {
        "example/library": ">=1.*"
    }
}
            

Right

{
    "require": {
        "example/library": "1.*"
    }
}
            

View Installed Packages

View Installed Packages

$ composer show --installed
installed:
  justinrainbow/json-schema [1.1.0] :
  seld/jsonlint [1.0.0] :
  symfony/console [v2.1.0] :
  symfony/finder [v2.1.0] :
  symfony/process [v2.1.0] :
            
$ composer show --platform
platform:
  ext-gd [0] : The gd PHP extension
  ext-intl [1.1.0] : The intl PHP extension
  ext-json [1.2.1] : The json PHP extension
  ext-PDO [1.0.4dev] : The PDO PHP extension
  ext-pdo_mysql [1.0.2] : The pdo_mysql PHP extension
  ext-pdo_pgsql [1.0.2] : The pdo_pgsql PHP extension
  ext-pdo_sqlite [1.0.1] : The pdo_sqlite PHP extension
  ext-Phar [2.0.1] : The Phar PHP extension
  lib-curl [7.24.0] : The curl PHP library
  lib-iconv [1.11] : The iconv PHP library
  lib-libxml [2.7.3] : The libxml PHP library
  lib-openssl [0.9.8.20] : The openssl PHP library
  lib-pcre [8.12] : The pcre PHP library
  php [5.4.3] : The PHP interpreter
            

Update Tricks

Update Tricks - Viewing New Commits

$ composer update -v (--verbose)
Loading composer repositories with package information
Updating dependencies
  - Updating symfony/symfony dev-master (487b8c => 885d47)
    Checking out 885d4733664b040765f2faab68f3aacef58d1216
    Pulling in changes:
      885d473 - Fabien Potencier: merged branch Tobion/empty-requ....
      13937de - Fabien Potencier: replaced self.version/2.1.* by ...
      a9a0f42 - Fabien Potencier: merged 2.1
      3c32fd9 - Fabien Potencier: replaced self.version by 2.1.*...
      c5edce7 - Fabien Potencier: merged branch eventhor...
      4d6dd46 - Fabien Potencier: merged branch eventhor...
            

Update Tricks - Updating The Lock

$ composer install
Installing dependencies from lock file
Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies
            
$ composer update nothing
Loading composer repositories with package information
Updating dependencies
Nothing to install or update
Writing lock file
Generating autoload files
            

Update Tricks - Dry Run

$ composer update --dry-run
Loading composer repositories with package information
Updating dependencies
  - Updating symfony/console (v2.1.0) to symfony/console (v2.1.1)
  - Updating symfony/finder (v2.1.0) to symfony/finder (v2.1.1)
  - Updating symfony/process (v2.1.0) to symfony/process (v2.1.1)
            

Optimized Autoloader

Optimized Autoloader

$ composer dump-autoload --optimize
                

Saves you a lot of framework overhead

Disabling Scripts

Disabling Scripts

Deploying with full control over post-install scripts

$ composer install --no-scripts
$ app/console assetic:dump --env=prod --no-debug web/
            

Troubleshooting

Package not auto-updating?

Validate your config

$ composer validate
./composer.json is valid
            

503 from GitHub when downloading zips?

Install from source instead

$ composer install --prefer-source
            

Experiencing a strange behavior?

Update composer

$ composer self-update
                

Update your deps

$ composer update -v
                

Still not working? Reinstall the deps

$ rm -rf vendor/
$ composer update -v
                

Still not working?

Report a bug with full --verbose output

Find Out More

Thank you.

Questions?

jordi@nelm.io

@seldaek

slides.seld.be


Feedback:

http://joind.in/7057