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-15 12:53:34 +00:00
|
|
|
The problem that Composer solves is this:
|
2012-03-14 03:07:22 +00:00
|
|
|
|
|
|
|
a) You have a project that depends on a number of libraries.
|
|
|
|
|
2013-07-01 00:52:10 +00:00
|
|
|
b) Some of those libraries depend on other libraries.
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2013-07-01 00:52:10 +00:00
|
|
|
c) You declare the things you depend on.
|
2012-03-14 03:07:22 +00:00
|
|
|
|
|
|
|
d) Composer finds out which versions of which packages need to be installed, and
|
2012-03-15 12:53:34 +00:00
|
|
|
installs 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.
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"require": {
|
|
|
|
"monolog/monolog": "1.2.*"
|
2012-02-18 12:34:07 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
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-10-15 11:12:30 +00:00
|
|
|
any version beginning with `1.2`.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2013-01-11 21:57:28 +00:00
|
|
|
## System Requirements
|
|
|
|
|
|
|
|
Composer requires PHP 5.3.2+ to run. A few sensitive php settings and compile
|
|
|
|
flags are also required, but the installer will warn you about any
|
|
|
|
incompatibilities.
|
|
|
|
|
|
|
|
To install packages from sources instead of simple zip archives, you will need
|
|
|
|
git, svn or hg depending on how the package is version-controlled.
|
|
|
|
|
|
|
|
Composer is multi-platform and we strive to make it run equally well on Windows,
|
|
|
|
Linux and OSX.
|
|
|
|
|
2012-08-23 16:09:20 +00:00
|
|
|
## Installation - *nix
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-20 11:50:23 +00:00
|
|
|
### Downloading the Composer Executable
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2012-06-13 16:30:51 +00:00
|
|
|
#### Locally
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
To actually get Composer, we need to do two things. The first one is installing
|
2012-10-09 02:54:54 +00:00
|
|
|
Composer (again, this means downloading it into your project):
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
curl -sS https://getcomposer.org/installer | php
|
|
|
|
```
|
|
|
|
|
2014-04-15 10:58:44 +00:00
|
|
|
> **Note:** If the above fails for some reason, you can download the installer
|
|
|
|
> with `php` instead:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
php -r "readfile('https://getcomposer.org/installer');" | php
|
|
|
|
```
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
This will just check a few PHP settings and then download `composer.phar` to
|
2012-03-15 12:53:34 +00:00
|
|
|
your working directory. This file is the Composer binary. It is a PHAR (PHP
|
2012-03-14 03:07:22 +00:00
|
|
|
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-15 12:53:34 +00:00
|
|
|
You can install Composer to a specific directory by using the `--install-dir`
|
2012-03-08 05:41:29 +00:00
|
|
|
option and providing a target directory (it can be an absolute or relative path):
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
|
|
|
|
```
|
2012-03-08 05:41:29 +00:00
|
|
|
|
2012-06-13 16:30:51 +00:00
|
|
|
#### Globally
|
|
|
|
|
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`.
|
|
|
|
|
2012-06-20 21:15:54 +00:00
|
|
|
You can run these commands to easily access `composer` from anywhere on your system:
|
2012-06-13 16:30:51 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
curl -sS https://getcomposer.org/installer | php
|
|
|
|
mv composer.phar /usr/local/bin/composer
|
|
|
|
```
|
2012-06-13 16:30:51 +00:00
|
|
|
|
2013-04-17 17:21:44 +00:00
|
|
|
> **Note:** If the above fails due to permissions, run the `mv` line
|
|
|
|
> again with sudo.
|
|
|
|
|
2012-10-25 13:17:35 +00:00
|
|
|
Then, just run `composer` in order to run Composer instead of `php composer.phar`.
|
2012-06-13 16:30:51 +00:00
|
|
|
|
2013-10-16 14:24:48 +00:00
|
|
|
#### Globally (on OSX via homebrew)
|
|
|
|
|
|
|
|
Composer is part of the homebrew-php project.
|
|
|
|
|
2014-04-01 16:20:11 +00:00
|
|
|
```sh
|
|
|
|
brew update
|
2014-06-22 08:52:33 +00:00
|
|
|
brew tap homebrew/homebrew-php
|
2014-04-01 16:46:16 +00:00
|
|
|
brew tap homebrew/versions
|
2014-04-01 20:07:50 +00:00
|
|
|
brew install php55-intl
|
2014-06-22 08:52:33 +00:00
|
|
|
brew install homebrew/php/composer
|
2014-04-01 16:20:11 +00:00
|
|
|
```
|
2013-10-20 01:54:36 +00:00
|
|
|
|
2012-08-23 16:09:20 +00:00
|
|
|
## Installation - Windows
|
|
|
|
|
2012-10-25 13:17:35 +00:00
|
|
|
### Using the Installer
|
|
|
|
|
|
|
|
This is the easiest way to get Composer set up on your machine.
|
|
|
|
|
2012-11-08 14:08:02 +00:00
|
|
|
Download and run [Composer-Setup.exe](https://getcomposer.org/Composer-Setup.exe),
|
2012-10-25 13:17:35 +00:00
|
|
|
it will install the latest Composer version and set up your PATH so that you can
|
|
|
|
just call `composer` from any directory in your command line.
|
|
|
|
|
|
|
|
### Manual Installation
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2012-10-25 22:50:39 +00:00
|
|
|
Change to a directory on your `PATH` and run the install snippet to download
|
|
|
|
composer.phar:
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
C:\Users\username>cd C:\bin
|
|
|
|
C:\bin>php -r "readfile('https://getcomposer.org/installer');" | php
|
|
|
|
```
|
2013-10-16 14:24:48 +00:00
|
|
|
|
2014-02-06 20:14:48 +00:00
|
|
|
> **Note:** If the above fails due to readfile, use the `http` url or enable php_openssl.dll in php.ini
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2013-07-01 00:52:10 +00:00
|
|
|
Create a new `composer.bat` file alongside `composer.phar`:
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
C:\bin>echo @php "%~dp0composer.phar" %*>composer.bat
|
|
|
|
```
|
2012-10-25 13:17:35 +00:00
|
|
|
|
2013-01-29 02:22:09 +00:00
|
|
|
Close your current terminal. Test usage with a new terminal:
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
C:\Users\username>composer -V
|
|
|
|
Composer version 27d8904
|
|
|
|
```
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2013-01-05 17:46:42 +00:00
|
|
|
## Using Composer
|
2013-01-03 23:05:08 +00:00
|
|
|
|
2013-01-05 17:46:42 +00:00
|
|
|
We will now use Composer to install the dependencies of the project. If you
|
|
|
|
don't have a `composer.json` file in the current directory please skip to the
|
|
|
|
[Basic Usage](01-basic-usage.md) chapter.
|
2012-10-25 13:20:16 +00:00
|
|
|
|
2013-01-05 17:46:42 +00:00
|
|
|
To resolve and download dependencies, run the `install` command:
|
2012-10-25 13:20:16 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
php composer.phar install
|
|
|
|
```
|
2012-10-25 13:20:16 +00:00
|
|
|
|
|
|
|
If you did a global install and do not have the phar in that directory
|
|
|
|
run this instead:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
composer install
|
|
|
|
```
|
2012-10-25 13:20:16 +00:00
|
|
|
|
2013-01-05 17:46:42 +00:00
|
|
|
Following the [example above](#declaring-dependencies), this will download
|
|
|
|
monolog into the `vendor/monolog/monolog` directory.
|
2012-10-25 13:20:16 +00:00
|
|
|
|
2012-02-18 12:34:07 +00:00
|
|
|
## Autoloading
|
|
|
|
|
2012-03-15 12:53:34 +00:00
|
|
|
Besides downloading the library, Composer also prepares an autoload file that's
|
2012-03-14 03:07:22 +00:00
|
|
|
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
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```php
|
|
|
|
require 'vendor/autoload.php';
|
|
|
|
```
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2013-01-05 17:46:42 +00:00
|
|
|
Woah! Now start using monolog! To keep learning more about Composer, keep
|
2012-03-14 03:07:22 +00:00
|
|
|
reading the "Basic Usage" chapter.
|
2012-03-07 16:35:53 +00:00
|
|
|
|
|
|
|
[Basic Usage](01-basic-usage.md) →
|