An Introduction to PHP Dependency Management With Composer

Post on 10-May-2015

3.277 views 2 download

Tags:

description

Bradley Jacob's talk from WordCamp Boston 2013

Transcript of An Introduction to PHP Dependency Management With Composer

ComposerDependency Management in PHP

Presentation by: Bradley Jacobs / @crazyjaco

@crazyjaco

Some assumptionsWordPress directory structureJSONWorking with multiple WordPress sites

Talking PointsGoalsWhat is Dependency ManagementIntroduction to Composer

The composer.json fileThe composer.lock fileAuto-loading

Package RepositoriesUsing Composer with WordPressWrap up Demo

Getting to a better workflow through Composer

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

quickly and consistently

TLDR;

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

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

Dependency/PackageManagement

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.

FrameworksLibrariesScripts

Dependency Management vsPackage Management

Introducing Composerhttp://getcomposer.org

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

Download and run the Composer-Setup.exe

Installation

Mac/Linux

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

Windows:

File acts as both a consumer and provider

composer.json

Schema Documentation

{ "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": {

Composer.lock

This one line gives you access to all of the classes in yourdependencies.

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

require 'vendor/autoload.php';

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

Packagist.orgPackagist.org

custom installers, WPackagist.org

Using Composer w/WordPress

WPackagist.org

-

Custom InstallersIt will be necessary for additional actions on occasion, like

installing something outside the /vendor folder.

http://composer.github.io/installers/ An installer for packagesthat 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 2Using 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":"*" }}

Goal: Generate the WordPress Skeleton

Final Demo - WordPress Endgame

./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" }, {

Where do we go from hereIntegration of Source Control (SVN, Git, Hg)Pre/Post install hooksWP-CLI commands

Wrapping UpGoalsWhat is Dependency ManagementIntroduction to Composer

The composer.json fileThe composer.lock fileAuto-loading

Package RepositoriesUsing Composer with WordPressWrap up Demo

Resources:

Composer Documentationhttp://composer.rarst.net/Trac ticket #23912 - Add composer support to WordPress

|

Thank You!@crazyjaco github.com/crazyjaco