1
0
Fork 0
composer/doc/00-intro.md

140 lines
4.4 KiB
Markdown
Raw Normal View History

2012-02-18 12:34:07 +00:00
# Introduction
Composer is a tool for dependency management in PHP. It allows you to declare
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
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
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
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
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.
2012-02-29 14:56:53 +00:00
{
"require": {
"monolog/monolog": "1.2.*"
2012-02-29 14:56:53 +00:00
}
2012-02-18 12:34:07 +00:00
}
We are simply stating that our project requires some `monolog/monolog` package,
any version beginning with `1.2`.
2012-02-18 12:34:07 +00:00
## Installation - *nix
2012-02-18 12:34:07 +00:00
2012-03-20 11:50:23 +00:00
### Downloading the Composer Executable
#### Locally
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
$ curl -s 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
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
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 https://getcomposer.org/installer | php -- --install-dir=bin
#### Globally
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:
$ curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
2012-10-25 13:17:35 +00:00
Then, just run `composer` in order to run Composer instead of `php composer.phar`.
## 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.
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
Change to a directory on your `PATH` and run the install snippet to download
composer.phar:
C:\Users\username>cd C:\bin
C:\bin>php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
Create a new `.bat` file alongside composer:
C:\bin>notepad composer.bat
2012-10-25 13:17:35 +00:00
Paste the following in, it simply proxies all arguments to composer:
@ECHO OFF
SET composerScript=composer.phar
php "%~dp0%composerScript%" %*
2012-10-25 13:17:35 +00:00
Save the file. Close your current terminal. Test usage with a new terminal:
C:\Users\username>composer -V
Composer version 27d8904
2012-10-25 13:17:35 +00:00
C:\Users\username>
2012-10-25 13:20:16 +00:00
## Using Composer
Next, run the `install` command to resolve and download dependencies:
$ php composer.phar install
If you did a global install and do not have the phar in that directory
run this instead:
$ composer install
This will download monolog into the `vendor/monolog/monolog` directory.
2012-02-18 12:34:07 +00:00
## Autoloading
Besides downloading 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
require 'vendor/autoload.php';
2012-02-18 12:34:07 +00:00
Woh! Now start 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) →