Skip to content

Using Composer

Andrey Savchenko edited this page Apr 17, 2015 · 7 revisions

Introduction

Composer is a dependency manager for PHP projects. It is a command line utility, easy to install and use.

WordPress SEO uses it to include and load libraries required (License Manager and i18n module at the moment), while keeping their development standalone.

WordPress SEO comes with composer.json configuration and is available through the central Packagist repository under yoast/wordpress-seo.

Prerequisites

Composer needs to be installed in build environment, following installation instructions.

PHP 5.3+ is required to run Composer itself, but resulting WordPress SEO project can still be run in PHP 5.2 environment.

Procedures

Creating a basic WordPress SEO project

The following command will run a full process to:

  1. retrieve WordPress SEO code (at latest stable version)
  2. retrieve library dependencies
  3. build a working plugin instance
composer create-project yoast/wordpress-seo

Setting up development copy

The following command will set up development branch of the project, with copies of components checked out from version control.

composer create-project yoast/wordpress-seo:dev-trunk --prefer-source --keep-vcs

Installing dependencies in Git checkout

The following command will retrieve dependencies and build a working plugin at the state, captured in working copy of checked out version control.

git clone https://github.com/Yoast/wordpress-seo
cd wordpress-seo
composer install

If working copy is switched to a different branch composer install can be used again to switch state of dependencies accordingly.

Updating state of dependencies

Composer tracks the state of the project in composer.lock file. Install will always reproduce that state, ensuring developers have consistent shared access to it.

When dependencies need to be updated the following command will bring them to the latest versions, allowed by composer.json configuration.

composer update

After that the updated composer.lock file needs to be committed into version control to capture the new state.

Using Composer in Travis tests

Travis provides Composer natively in its PHP environment.

Since some of the WordPress SEO tests run in PHP 5.2 environment, the PHP is temporarily switched to later version for the purposes of Composer build process.

phpenv local 5.6
composer install --no-interaction
phpenv local --unset

Building WordPress SEO for distribution

The following command will build the plugin, as its meant to be distributed to end users. It will:

  • omit development–only dependencies
  • prefer to skip version control sources
  • produce autoload, optimized for production environment
composer create-project yoast/wordpress-seo --no-dev --prefer-dist
cd wordpress-seo
composer dump-autoload --optimize

archive command can be further used to produce a compressed package:

cd wordpress-seo
composer archive --format zip

Git integration

The following script can be used to automate composer install command, whenever composer.lock of project's Git checkout changes (source — comment at this gist).

#!/bin/bash

# Put this file at: .git/hooks/post-checkout
# and make it executable
# You can install it system wide too, see http://stackoverflow.com/a/2293578/685587

PREV_COMMIT=$1
POST_COMMIT=$2

GIT_DIR=$(git rev-parse --git-dir)
GIT_DIR_MERGE="$GIT_DIR"/rebase-merge
GIT_DIR_APPLY="$GIT_DIR"/rebase-apply

GIT_MERGE_REBASE=false
[[ (-d "$GIT_DIR_MERGE" && -f "$GIT_DIR_MERGE/interactive") || -d "$GIT_DIR_APPLY" ]] && GIT_MERGE_REBASE=true

NOCOLOR='\e[0m'
REDCOLOR='\e[37;41m'

function composer.lock {
    echo -e "$REDCOLOR composer.lock has changed: running composer install $NOCOLOR"

    COMPOSER=
    if [ -f composer.phar ]; then
        COMPOSER="php composer.phar"
    fi

    which composer > /dev/null 2>&1
    if [ $? ]; then
        COMPOSER="composer"
    fi

    if [[ $GIT_MERGE_REBASE = false && -n "$COMPOSER" ]]; then
        $COMPOSER install
    fi
}

function package.json {
    echo -e "$REDCOLOR package.json has changed: running npm install $NOCOLOR"

    which npm > /dev/null 2>&1
    if [[ $GIT_MERGE_REBASE = false && $? ]]; then
        npm install
    fi
}

FUNCS=$(declare -F -p | cut -d " " -f 3)
for FUNC in $FUNCS
do
    DIFF=$(git diff --shortstat $PREV_COMMIT..$POST_COMMIT $FUNC 2>/dev/null)
    if [[ $DIFF != "" ]]; then
        $FUNC
    fi
done