Composer

These days, you’ll hear a lot of people talking about Composer in a conceited, condescending, and generally irritating way (you are using Composer, right?). It’s very annoying. But despite their best attempts their best efforts to put people off, Composer is actually a really useful tool.

It’s a dependency manager for PHP, allowing you to easily include libraries without having to bundle them in with your application – it’s essentially a package management system for PHP (kind of like PECL, but a lot more useful in many ways).

You can read about it here. Or here, in this blog post. What I should really say is, you can read more about it on the above website. Anyway…

Once you have it installed (and you can install it globally or just on a per project basis as an executable you can run), you can easily install all the depenencies in your project using:

composer install

This installs everything you need based on the project’s composer.json file, that you can save into your version controlled codebase. For example, here is the one I use for a Wing Commander project:

{
"minimum-stability": "dev",
"require": {
"xmeltrut/wing-commander": "*"
}
}

You can specify as many libraries as you need, and add version requirements for them too. There is also room for a “dev require” section – for example if you are using PHPUnit, you might want to install that library for your dev environment, but not your production clone.

Once you have everything installed, upgrading to the latest version of the libraries is easy:

composer update

Each time you download the libraries, it produces a composer.lock file that you can use to install your dependencies from – this records the version you are using, so you can generate your lock file on your dev environment, test it all works, then ensure your production environment has the same versions of all the libraries.

It also does dependency resolution. So if you try and install something with other dependencies – for example the Wing Commander library uses Flight and Mustache – it will automatically find this and download them too.

You can search for the packages you need on Packagist and while a lot of the libraries you will use are on Github, there is no coupling here – the dependency can specify any repository location and this is all handled transparently.

Metadata

This entry was posted on Friday, June 28th, 2013 at 11:38 am and is filed under Programming, Tips & Advice. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply