Drupal has Dependencies
Let's manage them
 

Drupal has Dependencies
Let's manage them
With Composer

About Me

Passionate Free Software Developer
  http://github.com/naderman

phpBB Development Lead
 http://www.phpbb.com

Just launched
 https://www.forumatic.com
 Professional Managed phpBB Hosting

Composer

Package Management in PHP

The Composer Ecosystem

Composer - CLI Tool

The Composer Ecosystem

Packagist - Package Repository

The Composer Ecosystem

Satis - Micro Repository

The Composer Ecosystem

composer/installers

Custom Package Installers for

  • Magento
  • phpBB
  • PPI
  • SilverStripe
  • Symfony1
  • Wordpress
  • Zend Framework 1
  • CakePHP
  • CodeIgniter
  • FuelPHP
  • Joomla!
  • Kohana
  • Laravel
  • Lithium

and Drupal

Usage Instructions

Using a Composed Project

git clone https://github.com/symfony/standard-edition myproject
            
Cloning into myproject...
cd myproject/
            
curl -s http://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

php composer.phar 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

php composer.phar create-project <package> [<dir>] [<version>]

php composer.phar create-project symfony/framework-standard-edition
            

Downloading Project Dependencies

composer.json

{
    "require": {
        "silex/silex": ">=1.0.0-dev",
        "symfony/finder": "2.1-dev",
        "twig/twig": "1.*",
        "predis/service-provider": "dev-master"
    },
    "require-dev": {
        "mikey179/vfsStream": "*"
    }
}
            

Source install: With install --prefer-source it clones/checks out the code.

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-0": {"Predis": "lib/"}
    }
}
            

Note: Package Definition === Application/Root Definition

Avoiding version chaos
in your team

composer.lock


Benefits

Autoloading

Libraries/projects define their namespaces:

"autoload": {
    "psr-0": {
        "Vendor\\Namespace": "lib/"
    },
    "classmap": ["src/", "VeryOld.php"]
},
"include-path": ["src/", ""]
            

Composer builds an autoloader for you:

vendor/autoload.php
                

Use the generated autoloader:

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

use Silex\Application;
use Silex\Extension\TwigExtension;

use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Response;

$app = new Application();
// ...
                

Alternative Repositories

"repositories": [
    {
        "type": "composer",
        "url": "http://private.satis.example.org"
    },
    {
        "type": "vcs",
        "url": "git://example.org/MyRepo.git"
    },
    {
        "packagist": false
    }
]
            

See docs for more

Depending on packages without composer.json

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "vendor/package",
            "version": "1.0.0",
            "dist": {
                "url": "http://example.org/package.zip",
                "type": "zip"
            },
            "source": {
                "url": "git://example.org/package.git",
                "type": "git",
                "reference": "tag name, branch name or commit hash"
            }
        }
    }
],
"require": {
    "vendor/package": "1.0.0"
}
            

Note: repositories are only available to the root package

State of the Project

Adoption

Roadmap

Drupal and Composer

Drupal 8

Drush Composer

Drush wrapper to run Composer commands http://dgo.to/composer

$ drush dl composer
$ drush composer install
            

Drush Composer Hooks

Drush Composer processes composer.json when modules are installed

$ drush dl less
Project less (7.x-2.x-dev) downloaded to sites/all/modules/less.
Installing dependencies
  - Installing leafo/lessphp (v0.3.5)
    Downloading: 100%

Writing lock file
Generating autoload files
            

Drupal Composer Autoload

Module to integrate your module with Composer http://dgo.to/composer_autoload

Wishful Thinking

Look around.

Write small libs.

Share code.

Reuse things.

Reinvigorate PHP

Find Out More

Conversation!


@naderman

Pull Request for Package Cache: github.com/composer/composer/pull/915

Root composer.json proposal

"require": {
    "drupal/core": "8.0.0",
    "my/module1": "1.0.0",
    "my/module2": "2.1.3",
}
            

Drupal Update Process

"require": {
    "drupal/core": "8.0.1",
    "my/module1": "1.0.0",
    "my/module2": "2.1.3",
}
            
php composer.phar update
            

Install Drupal with Composer

php composer.phar create-project drupal/project
            

Result

composer.json

"require": {
    "drupal/core": "8.0.0",
}
            

Drupal Core and dependencies in vendor/

Bonus Slide: Aliases & Branches

Situation


Solution

"repositories": [
    {
        "type": "vcs",
        "url": "git://github.com/my/monolog.git"
    }
]
"require": {
    "symfony/monolog-bundle": "2.0.10",
    "monolog/monolog": "dev-my-feature-branch as 1.1.0"
}
            

Thank you.