@crazyjaco

Composer

Dependency Management in PHP

Presentation by: Bradley Jacobs / @crazyjaco

Some assumptions

  • WordPress directory structure
  • JSON
  • Working with multiple WordPress sites

Talking Points

  • Goals
  • What is Dependency Management
  • Introduction to Composer
    • The composer.json file
    • The composer.lock file
    • Auto-loading
  • Package Repositories
  • Using Composer with WordPress
  • Wrap up Demo

TLDR;

Getting to a better workflow through Composer

Goal:
Rapidly create a WordPress server setup that can be replicated quickly and consistently

WordPress Skeleton

https://github.com/markjaquith/WordPress-Skeleton

./wp/
   ./wp/wp-admin/
   ./wp/wp-content/
   ./wp/wp-includes/
./wp-content/
./index.html
./wp-config.php

Dependency/Package Management



What is a package?


A package is just a bunch of files in a folder

Dependencies are a type of package.

Why Package/Dependency Management?


Modern day web pages are rarely self-contained.

  • Frameworks
  • Libraries
  • Scripts

Dependency Management vs Package Management

Introducing Composer

http://getcomposer.org

Demo 1 - PHPUnit


{
    "name": "crazyjaco/phpunit-test",
    "description": "A test of using composer with phpunit",
    "license": "proprietary",
    "require": {
        "phpunit/phpunit":"3.7.*"
    }
}

Installation

Mac/Linux


$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
						

Windows:

Download and run the Composer-Setup.exe

composer.json



Schema Documentation


File acts as both a consumer and provider

{
    "name": "bradley/mycoolproject",
    "description": "This project does cool stuff",
    "keywords": ["wordcamp", "boston", "cool"],
    "homepage": "http://github.com/crazyjaco/mycoolproject",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Bradley Jacobs",
            "email": "bjacobs@oomphinc.com",
            "homepage": "http://oomphinc.com"
        }
    ],
    "repositories":{
            "type": "composer",
            "url": "http://custom-repo.org"
    },
    "require": {
        "oomph/framework": "3.2",
        "bigcompany/api-library": ">4.0"
    },
    "require-dev": {
        "custom-repo/unit-tester": "2.3.*",
        "logger/logger": "1.7.*",
        "debug/tools": "~2.0"
    },
    "suggest": {
        "coolcompany/extension": "Allow extension to the core program"
    }
}	

{
    "name": "bradley/mycoolproject",
    "description": "This project does cool stuff",
    "keywords": ["wordcamp", "boston", "cool"],
    "homepage": "http://github.com/crazyjaco/mycoolproject",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Bradley Jacobs",
            "email": "bjacobs@oomphinc.com",
            "homepage": "http://oomphinc.com"
        }
    ],
    "repositories":{
            "type": "composer",
            "url": "http://custom-repo.org"
    },
    "require": {
        "oomph/framework": "3.2",
        "bigcompany/api-library": ">4.0"
    },
    "require-dev": {
        "custom-repo/unit-tester": "2.3.*",
        "logger/logger": "1.7.*",
        "debug/tools": "~2.0"
    },
    "suggest": {
        "coolcompany/extension": "Allow extension to the core program"
    }
}	

Composer.lock

Autoloading

*** All dependencies are loaded into the '/vendor' folder ***



require 'vendor/autoload.php';
This one line gives you access to all of the classes in your dependencies.

Package Repositories

Where do we find and get the packages for composer to install?

Packagist.org

Packagist.org

Using Composer w/ WordPress

custom installers, WPackagist.org

WPackagist.org


Custom Installers

It will be necessary for additional actions on occasion, like installing something outside the /vendor folder.



http://composer.github.io/installers/ - An installer for packages that need special installation based on package type

Hosted Plugin


{
    "name": "oomphinc/coolplugin",
    "type": "wordpress-plugin",
    "require": {
        "composer/installers": "*"
    }
}
							

Your Project

{
    "name": "crazyjaco/wordpress-composer-test",
    "description": "A test of using composer with WordPress",
    "license": "proprietary",
    "repositories": [
        {
            "type": "composer",
            "url": "http://wpackagist.org"
        }
    ],
    "require": {
        "wpackagist/yet-another-related-posts-plugin":"*",
        "wpackagist/wp-super-cache":"*",
        "wpackagist/jetpack":"*"
    },
    "require-dev": {
        "wpackagist/developer":"*"
    }
}

Demo 2

Using the Custom Installer for WordPress Plugins

{
    "name": "crazyjaco/wordpress-composer-test",
    "description": "A test of using composer with WordPress",
    "license": "proprietary",
    "repositories": [
        {
            "type": "composer",
            "url": "http://wpackagist.org"
        }
    ],
    "require": {
        "wpackagist/yet-another-related-posts-plugin":"*",
        "wpackagist/wp-super-cache":"*",
        "wpackagist/jetpack":"*"
    },
    "require-dev": {
        "wpackagist/developer":"*"
    }
}

Final Demo - WordPress Endgame

Goal: Generate the WordPress Skeleton


./wp/
   ./wp/wp-admin/
   ./wp/wp-content/
   ./wp/wp-includes/
./wp-content/
./index.html
./wp-config.php

{
    "name": "crazyjaco/wp-full-demo",
    "description": "Full WordPress Install Demo",
    "keywords": ["WordPress", "theme", "plugin", "Oomph"],
    "homepage": "http://oomphinc.com/",
    "type": "project",
    "license": "proprietary",
    "authors": [
        {
            "name": "Bradley Jacobs - Oomph",
            "email": "bjacobs@oomphinc.com",
            "homepage": "http://oomphinc.com"
        }
    ],
    "repositories": [
        {
            "type": "composer",
            "url": "http://wpackagist.org"
        },
        {
            "type"   : "package",
            "package": {
                "name"   : "crazyjaco/wordpress",
                "type"   : "wordpress-core",
                "version": "3.6",
                "dist"   : {
                    "url" : "http://wordpress.org/wordpress-3.6.zip",
                    "type": "zip"
                },
                "source" : {
                    "url"  : "https://github.com/WordPress/WordPress",
                    "type" : "git",
                    "reference": "3.6"
                },
		        "require": {
                    "johnpbloch/wordpress-core-installer": "0.2"
                }
            }
        }		
    ],
    "require": {
        "crazyjaco/wordpress": "3.6",
        "wpackagist/yet-another-related-posts-plugin":"4.0.8",
        "wpackagist/wp-super-cache":"1.3.2",
        "wpackagist/jetpack":"2.5"
    },
    "require-dev": {
        "wpackagist/developer":"1.2.5"
    },
    "config": {
        "vendor-dir": "wp-content/vendor"
    },    
    "extra": {
        "wordpress-install-dir": "wp"
    }	
}

Where do we go from here

  • Integration of Source Control (SVN, Git, Hg)
  • Pre/Post install hooks
  • WP-CLI commands

Wrapping Up

  • Goals
  • What is Dependency Management
  • Introduction to Composer
    • The composer.json file
    • The composer.lock file
    • Auto-loading
  • Package Repositories
  • Using Composer with WordPress
  • Wrap up Demo

Resources:

Thank You!

@crazyjaco | github.com/crazyjaco