2012-02-18 12:34:07 +00:00
|
|
|
# Introduction
|
|
|
|
|
|
|
|
Composer is a tool for dependency management in PHP. It allows you to declare
|
2012-03-14 03:07:22 +00:00
|
|
|
the dependent libraries your project needs and it will install them in your
|
|
|
|
project for you.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
## Dependency management
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
Composer is not a package manager. Yes, it deals with "packages" or libraries, but
|
|
|
|
it manages them on a per-project basis, installing them in a directory (e.g. `vendor`)
|
|
|
|
inside your project. By default it will never install anything globally. Thus,
|
|
|
|
it is a dependency manager.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
This idea is not new and Composer is strongly inspired by node's [npm](http://npmjs.org/)
|
|
|
|
and ruby's [bundler](http://gembundler.com/). But there has not been such a tool
|
|
|
|
for PHP.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
The problem that composer solves is this:
|
|
|
|
|
|
|
|
a) You have a project that depends on a number of libraries.
|
|
|
|
|
|
|
|
b) Some of those libraries depend on other libraries .
|
|
|
|
|
|
|
|
c) You declare the things you depend on
|
|
|
|
|
|
|
|
d) Composer finds out which versions of which packages need to be installed, and
|
|
|
|
install them (meaning it downloads them into your project).
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
## Declaring dependencies
|
|
|
|
|
|
|
|
Let's say you are creating a project, and you need a library that does logging.
|
|
|
|
You decide to use [monolog](https://github.com/Seldaek/monolog). In order to
|
|
|
|
add it to your project, all you need to do is create a `composer.json` file
|
|
|
|
which describes the project's dependencies.
|
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"require": {
|
|
|
|
"monolog/monolog": "1.0.*"
|
|
|
|
}
|
2012-02-18 12:34:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
We are simply stating that our project requires some `monolog/monolog` package,
|
2012-02-18 17:11:00 +00:00
|
|
|
any version beginning with `1.0`.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
### 1) Downloading the Composer Executable
|
|
|
|
|
|
|
|
To actually get Composer, we need to do two things. The first one is installing
|
|
|
|
composer (again, this mean downloading it into your project):
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
$ curl -s http://getcomposer.org/installer | php
|
|
|
|
|
|
|
|
This will just check a few PHP settings and then download `composer.phar` to
|
2012-03-14 03:07:22 +00:00
|
|
|
your working directory. This file is the composer binary. It is a PHAR (PHP
|
|
|
|
archive), which is an archive format for PHP which can be run on the command
|
|
|
|
line, amongst other things.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-08 05:41:29 +00:00
|
|
|
You can install composer to a specific directory by using the `--install-dir`
|
|
|
|
option and providing a target directory (it can be an absolute or relative path):
|
|
|
|
|
|
|
|
$ curl -s http://getcomposer.org/installer | php -- --install-dir=bin
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
You can place this file anywhere you wish. If you put it in your `PATH`,
|
|
|
|
you can access it globally. On unixy systems you can even make it
|
|
|
|
executable and invoke it without `php`.
|
|
|
|
|
|
|
|
### 2) Using Composer
|
|
|
|
|
|
|
|
Next, run the command the `install` command to calculate and download dependencies:
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
$ php composer.phar install
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
This will download monolog into the `vendor/monolog/monolog` directory.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
## Autoloading
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
Besides download the library, Composer also prepares an autoload file that's
|
|
|
|
capable of autoloading all of the classes in any of the libraries that it
|
|
|
|
downloads. To use it, just add the following line to your code's bootstrap
|
|
|
|
process:
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
require 'vendor/.composer/autoload.php';
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
Woh! Now starting using monolog! To keep learning more about Composer, keep
|
|
|
|
reading the "Basic Usage" chapter.
|
2012-03-07 16:35:53 +00:00
|
|
|
|
|
|
|
[Basic Usage](01-basic-usage.md) →
|