Blog. Just a few musings.

Using Composer with WordPress

This is a simple guide to getting Composer to work with WordPress.

You will need to ensure your web server (development and production) are both running PHP 5.3.* minimum and will require the cURL extension to be enabled.

I develop and run all of my sites on a *nix environment (CentOS / Apache2 / PHP 5.4.*), so further instructions will only be valid for this platform. If you run on a Windows environment, please follow the instructions on Composer’s site.

Installing Composer

Open up a terminal window (SSH/directly on the machine) and run the following in your home directory.
cd ~
curl -s https://getcomposer.org/installer | php

We want to run composer globally, so next we will move it into your environment’s path.

sudo mv composer.phar /usr/local/bin/composer

Setting up Composer for WordPress

First navigate to your document root for your WordPress install.

Add a new file to the document root called composer.json.

touch composer.json

Open the file in your favourite editor (in this case, vi)

vi composer.json

In this article, I’ll provide an example of dependencies I have added to one of my recent projects which uses Doctrine2, Silex, Twig and a number of other libraries.

{
"require": {
"monolog/monolog": "1.2.*",
"doctrine/common": "2.3.*",
"doctrine/orm": "2.3.*",
"doctrine/dbal": "2.3.*",
"twig/twig": "1.*",
"sybio/image-workshop": "dev-master",
"silex/silex": "1.0.*",
"taluu/doctrine-orm-provider": "1.0.*",
"symfony/form": "2.1.*",
"symfony/validator": "2.1.*",
"symfony/config": "2.1.*",
"symfony/translation": "2.1.*",
"symfony/twig-bridge": "2.1.*",
"symfony/doctrine-bridge":"2.1.*",
"doctrine/data-fixtures":"1.0.*"
},
"minimum-stability": "dev"
}

Save the file, and now we’re ready to download our dependencies. Run the following:
<Esc> :wq <Enter>
composer install

This process will download the libraries/framework to a new folder called ‘vendor’ in your document root. In addition to this, Composer will generate an autoloader for you to automatically include the files when they are called. The file is PSR-0 compliant and will load files by their namespace and class name.

To use the generated autoloader, open up your wp-config.php file in an editor and add the following:

Before:
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Add:
/** Composer Autoloader */
require_once(ABSPATH.'vendor/autoload.php');

Save the config file.

Now you should be able to use any of the libraries with a simple call to the class name in your plugins, themes, etc, eg.
$silex = new \Silex\Application();

Awesome PHP Libraries →

Awesome PHP Libraries

Let me just start this article off by saying … damn, I love the open source movement!

The PHP community has come to the for in the last 2 years with a number of new libraries and frameworks being released. Out of the frameworks/libraries in the link attached, the following have made many a PHP developer very excited.

Composer

One thing that was lacking in the world of PHP was a decent method to pull together 3rd-party libraries for a project, as well as keep them up to date. Languages such as Ruby, have had this dependency management functionality for quite some time and it was envisaged that such functionality would be extremely useful for the PHP community as well.

Composer is as simple as downloading the composer.phar script, and then working out the packages you will need to add to the vendors folder of a project. Note, the vendors folder is framework agnostic so it doesn’t matter if you are using WordPress, Drupal, Symfony2, etc, they just have different methods of being included/namespaced into each application. Once the packages have been decided on, a composer.json file needs to generated. Again this can be done via the command line, or manually. The composer.json file lists out the required packages and the version numbers to watch for, and then when the composer update command is run, the libraries will then be checked for their latest versions.

Get Composer »

Symfony

One of my all time favourite frameworks has progressed to version 2.1, and is now an entirely new beast, and I mean that in a good way.

Symfony2 is made up of a number of base components, as well as the Doctrine ORM, Swiftmailer, Assetic, Monolog, and a powerful Web Profiler to assist with debugging.  The various components that make up Symfony2 can also be used independently.

Of note, Drupal 8 is currently being developed with a Symfony2 core.

Just launched!

I’ve finally built and launched an online presence after many years of saying I’d get one up. I will be posting articles, and various things of interest in future, so stay tuned!