Composer is a dependency management tool for PHP; perhaps comparable to using Maven for a Java project. I have used Maven for building both a Java project, as well as building a mobile application for Android and iOS using flex-mojos and Adobe AIR. So getting to learn about Composer has been a very enjoyable. I wanted to learn how to user Composer to use the following libraries:
Install Homebrew
To get started, we will be installed Homebrew. If you already have this installed, you can skip the next section on installing Composer. To install Homebrew is really easy. Open up a terminal window on your Mac and enter:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
Install Composer
Before installing Composer it is a good idea to run brew update
and brew doctor
to ensure that you have an updated installation of Homebrew, and that your Homebrew installation is in good working order.
I ran into some issues and had to untap the Composer installation and go back and perform these steps, so I suggest you do these first.
$ brew update
$ brew doctor
The next step is to ensure you have php55 installed via Homebrew:
$ brew install php55
Then, let’s add Composer:
$brew tap josegonzalez/homebrew-php
$ brew install composer
You should not have Composer installed. You can check by running the the following command, which should yield results similar to:
$ composer --version
Composer version 1.0.0-alpha7
Create the composer.json
File
The composer.json file instructs Composer with information about your project and what your dependencies are. Here is an outline of what the following composer.json features:
- The name element is a string that identifies your project’s name. I used my reverse domain name notation
- The repositories element describes repositories that the project requires. In our case, we define the CakePHP repository.
- The config element provides additional configuration parameters. Here we define the folder to put our dependencies in, called “Vendor”.
- The extra element includes the installer-paths element, which tells Composer a custom path to place our extra required dependencies. In this case, I want the three plugins that I am using to go into the app/Plugin directory so that they are available to the application after being built.
- The require element lists the library dependencies for the project, and the required version of each dependency.
{
"name": "com.brianflove",
"repositories": [
{
"type": "pear",
"url": "http://pear.cakephp.org"
}
],
"config": {
"vendor-dir": "Vendor/"
},
"extra": {
"installer-paths": {
"app/Plugin/DebugKit": ["cakephp/debug_kit"],
"app/Plugin/Localized": ["cakephp/localized"],
"app/Plugin/BoostCake": ["slywalker/boost_cake"]
}
},
"require": {
"php": ">=5.3",
"pear-pear.cakephp.org/CakePHP": ">=2.4.0",
"cakephp/debug_kit": "2.2.*",
"cakephp/localized": "2.1.*",
"slywalker/boost_cake": "*"
}
}
Run It
The next step is to install our dependencies into the vendor directory, via:
$ composer install
Lastly, we will bake our new cake project:
$ Vendor/bin/cake bake app
P.S. Composer will hard code your application’s core path, so you should update webroot/index.php to define CAKE_CORE_INCLUDE_PATH
as follows:
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'Vendor' . DS . 'pear-pear.cakephp.org' . DS . 'CakePHP' . DS . 'lib');
Note, this is slightly different than what the CakePHP Book indicates as the necessary path.