Introduction to Composer
14, Dec 2013
Explaining the Need
Projects in today's world are not developed in isolation. There are so many incredibly intelligent people out there writing code to solve the common problems experienced in nearly every web application that some very impressive solutions have been developed. When you begin working on a new application there's no need for you to implement something that's already been done, so you bring that external code into your application to make use of it.
You've just introduced a dependency. Dependencies can be great assets, yet at the same time a source of confusion and frustration. Keeping track of dependencies and making them all play together nicely requires a lot of mental agility. Additionally if you're working as part of a team it can be very hard to ensure that all the team members have identical development environments with not just the same dependencies in the same places, but also of the same version!
Composer exists to help you out - a dependency manager to ease your mind. With Composer you simply declare your dependencies and Composer then downloads them (and whatever else they depend upon) and places them in one central location at your project's root. There are over 20,000 packages that Composer can use, available for your exploration at packagist.org. Just browsing through the popular packages gives gems such as logging and mailer packages, and the Doctrine and Symphony PHP frameworks.
Getting Started with Composer
It's incredibly simple to get moving with Composer. To install the phar - PHP Archive file - all one needs to do is download it from their site:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin
Those two commands will take care of things for you. Firstly downloading the phar and then running some checks on your local system to make sure that it's compatible with your copy of PHP. If all is good, you can then issue the second command to make Composer available system wide. If you choose not to do the second command, the composer.phar file is perfectly usable in it's current state; you'll just have to provide a path to it when ever you call it.
Composer uses git, SVN, or hg to obtain the packages that you're using in your application, so make sure you also have those installed as needed.
Installing a Dependency
Now that we're all setup with Composer, let's use it to grab our first dependency: Mustache. Mustache is a HTML templating language that has been ported into many different languages, including PHP.
The first step is to create a composer.json file. This JSON file will describe all of the dependencies that our project will be needing as well as their versions. At the moment we only have one: Mustache. In the root directory of your application create a file called composer.json with the following content:
{
"require": {
"mustache/mustache": "2.5"
}
}
The second (and final) step is to run Composer:
composer install
Issuing that command will have Composer go out and gather all the dependencies that are listed in the file, and organize them into your project. What exactly does that entail though? If you look in the directory after running the install command you'll see some newly created items:
- composer.lock - A JSON file that are an exact description of the dependencies installed. It supersedes composer.json; therefore if it is present when
composer installis run then it will be used to gather the dependencies instead of the composer.json file - vendor folder - All the dependencies installed go inside this folder inside folders of their own. Additionally there's a composer folder and an autoload.php file there as well. The autoload.php file calls the classes inside the composer folder which act as auto loaders for all the dependencies manged by Composer. This means that you only need to include that one autoload.php class and all of your other dependencies will be auto loaded into your project (aka, no require statements necessary)
Let's put that to the test then shall we?
Using Mustache with Composer
Let's create a very simple page using Mustache templates to try out our newly installed dependency. In the same web root as your composer.json file, make a new php file, and make add the following code to it:
<?php
require_once 'vendor/autoload.php';
$Mustache_Engine = new Mustache_Engine;
$template = 'Hello {{planet}}';
$context = Array('planet' => 'World!');
echo $Mustache_Engine->render($template, $context);
?>
This quick bit of code brings in the autoload file generated by Composer, and because of it allows us to auto load the Mustache_Engine class provided by the Mustache library. We can then use that class to process a simple template and echo it out. The double curlys around planet denote it as a named variable in the template to be replaced with the value of the corresponding key in the $context array.
That's all there is to it! I could talk more about Mustache and creating templates for it, but I'll leave that to another post.