Getting Started with Composer

Created by Sal Ferrarello / @salcode

(Skip to Resources)

Encyclopedia Advertistment

"I Want a Set of Encyclopedias"
- 10 yr old me

You want an Encyclopedia? Here are directions to the library.

What do you include
in your repo?

git add all the things

Our repo doesn't need all the things,
just the things that are unique to this project

do NOT git add all the things

Our repo doesn't need all the things,
just the things that are unique to this project

Composer is a Power Tool

Composer requires PHP 5.3

Things to do before using composer

  • Run WordPress locally
  • Use git
  • Use the command line

What We'll Cover Today

  • installing composer
  • composer with a plugin or theme
  • composer with a WordPress project

Installing Composer

On a Mac: copy-and-paste in Terminal


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

(detailed Linux / Unix / OS X instructions)

On Windows: use within VVV or see these instructions

Composer with a Plugin or Theme

Pluralize Title Plugin
Before 3rd Party Code

pluralize-title.php


$title .= 's';
					

Using Composer for
Third-Party Code

  • Define code to add (composer.json)
  • Install the code via composer
  • Load the code with composer's auto-loader
  • Use the class in the code

Define Code to Add

packagist.org is the default composer repository

I found the package doctrine/inflector when
searching for "pluralize"

composer.json


{
  "require": {
    "doctrine/inflector": "^1.0.0"
  }
}
						

Install the code via composer

From the Command Line


composer install
					
  • creates directory vendor/
  • downloads code from packagist.org

Composer's Auto-loader

Historically, use "require" for each file

Instead


$loader = require 'vendor/autoload.php';
						

Use the class in the code


$title =
Doctrine\Common\Inflector\Inflector::pluralize($title);
					

Summary Third-Party Code Plugin

Add composer third-party library

pluralize-title.php


$loader = require 'vendor/autoload.php';
$title =
Doctrine\Common\Inflector\Inflector::pluralize($title);
					

composer.json


{
  "require": {
    "doctrine/inflector": "^1.0.0"
  }
}
					

What haven't I cover

  • packagist.org isn't the only repository
  • changing your install path (e.g. wp-content/plugins)
  • lock versions within your team (composer.lock)
  • updating packages (composer update)
  • optimize auto-loader for deployment
    (composer install -o)
  • lots of other stuff

But I Build WordPress Projects

This Doesn't Apply to Me

This does not apply to me

Composer with a WordPress Project

  • Third-party plugins from .org
  • Private plugins
  • WordPress Core files

.gitignore

.gitignore all the things

ignore everything, whitelist what you need

.org Plugins with Composer


{
  "repositories":[
    {
      "type":"composer",
      "url":"http://wpackagist.org"
    }
  ],
  "require": {
    "wpackagist-plugin/wordpress-seo":">1.7.3"
  }
}
					

Resources

Iron Code Studio

Sal Ferrarello

ironco.de/composer

@salcode