2012-02-18 17:11:00 +00:00
|
|
|
# Basic usage
|
|
|
|
|
2015-06-22 14:24:59 +00:00
|
|
|
## Introduction
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2015-06-22 20:01:20 +00:00
|
|
|
For our basic usage introduction, we will be installing `monolog/monolog`,
|
2015-07-21 13:46:38 +00:00
|
|
|
a logging library. If you have not yet installed Composer, refer to the
|
2015-06-22 20:01:20 +00:00
|
|
|
[Intro](00-intro.md) chapter.
|
|
|
|
|
|
|
|
> **Note:** for the sake of simplicity, this introduction will assume you
|
|
|
|
> have performed a [local](00-intro.md#locally) install of Composer.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
## `composer.json`: Project Setup
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2012-03-15 12:53:34 +00:00
|
|
|
To start using Composer in your project, all you need is a `composer.json`
|
2012-02-20 10:08:18 +00:00
|
|
|
file. This file describes the dependencies of your project and may contain
|
2012-02-18 17:11:00 +00:00
|
|
|
other metadata as well.
|
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
### The `require` Key
|
|
|
|
|
2012-02-18 17:11:00 +00:00
|
|
|
The first (and often only) thing you specify in `composer.json` is the
|
2015-07-21 13:46:38 +00:00
|
|
|
[`require`](04-schema.md#require) key. You're simply telling Composer which
|
2015-06-23 06:47:30 +00:00
|
|
|
packages your project depends on.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"require": {
|
|
|
|
"monolog/monolog": "1.0.*"
|
2012-02-18 17:11:00 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2015-07-21 13:46:38 +00:00
|
|
|
As you can see, [`require`](04-schema.md#require) takes an object that maps
|
|
|
|
**package names** (e.g. `monolog/monolog`) to **version constraints** (e.g.
|
2015-06-23 06:47:30 +00:00
|
|
|
`1.0.*`).
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-03-08 09:15:27 +00:00
|
|
|
Composer uses this information to search for the right set of files in package
|
2017-01-20 01:04:24 +00:00
|
|
|
"repositories" that you register using the [`repositories`](04-schema.md#repositories)
|
|
|
|
key, or in Packagist, the default package respository. In the above example,
|
2017-01-20 17:49:29 +00:00
|
|
|
since no other repository has been registered in the `composer.json` file, it is
|
|
|
|
assumed that the `monolog/monolog` package is registered on Packagist. (See more
|
|
|
|
about Packagist [below](#packagist), or read more about repositories
|
|
|
|
[here](05-repositories.md).
|
2017-01-20 01:04:24 +00:00
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
### Package Names
|
2012-02-18 17:11:00 +00:00
|
|
|
|
|
|
|
The package name consists of a vendor name and the project's name. Often these
|
2017-01-20 01:04:24 +00:00
|
|
|
will be identical - the vendor name just exists to prevent naming clashes. For
|
|
|
|
example, it would allow two different people to create a library named `json`.
|
|
|
|
One might be named `igorw/json` while the other might be `seldaek/json`.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
Read more about publishing packages and package naming [here](02-libraries.md).
|
|
|
|
(Note that you can also specify "platform packages" as dependencies, allowing
|
|
|
|
you to require certain versions of server software. See
|
|
|
|
[platform packages](#platform-packages) below.)
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
### Package Version Constraints
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
In our example, we're requesting the Monolog package with the version constraint
|
|
|
|
[`1.0.*`](http://semver.mwl.be/#?package=monolog%2Fmonolog&version=1.0.*).
|
|
|
|
This means any version in the `1.0` development branch, or any version that is
|
|
|
|
greater than or equal to 1.0 and less than 1.1 (`>=1.0 <1.1`).
|
2015-06-22 14:24:59 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
(What the term "version" actually means can be a little confusing in Composer.
|
|
|
|
Please read [versions](articles/versions.md) for more in-depth information.)
|
2014-09-25 13:27:10 +00:00
|
|
|
|
2017-01-20 01:04:24 +00:00
|
|
|
> **How does Composer download the right files?** When you specify a dependency in
|
|
|
|
> `composer.json`, Composer, first takes the name of the package that you've requested
|
|
|
|
> and searches for it in any repositories that you've registered using the
|
|
|
|
> [`repositories`](04-schema.md#repositories) key. If you haven't registered
|
|
|
|
> any extra repositories, or it doesn't find a package with that name in the
|
|
|
|
> repositories you've specified, it falls back to Packagist (more [below](#packagist)).
|
|
|
|
>
|
|
|
|
> When it finds the right package, either in Packagist or in a repo you've specified,
|
2017-01-20 17:49:29 +00:00
|
|
|
> it then uses the versioning features of the package's VCS (i.e., branches and tags)
|
2017-01-23 19:06:51 +00:00
|
|
|
> to attempt to find the best match for the version constraint you've specified. Be sure to read
|
2017-01-20 17:49:29 +00:00
|
|
|
> about versions and package resolution in the [versions article](articles/versions.md).
|
2017-01-20 01:04:24 +00:00
|
|
|
|
|
|
|
> **Note:** If you're trying to require a package but Composer throws an error
|
2017-01-20 17:49:29 +00:00
|
|
|
> regarding package stability, the version you've specified may not meet your
|
|
|
|
> default minimum stability requirements. By default only stable releases are taken
|
|
|
|
> into consideration when searching for valid package versions in your VCS.
|
2017-01-20 01:04:24 +00:00
|
|
|
>
|
|
|
|
> You might run into this if you're trying to require dev, alpha, beta, or RC
|
|
|
|
> versions of a package. Read more about stability flags and the `minimum-stability`
|
|
|
|
> key on the [schema page](04-schema.md).
|
2012-12-08 19:50:48 +00:00
|
|
|
|
2012-03-14 03:07:22 +00:00
|
|
|
## Installing Dependencies
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2015-06-22 14:24:59 +00:00
|
|
|
To install the defined dependencies for your project, just run the
|
2015-06-23 06:47:30 +00:00
|
|
|
[`install`](03-cli.md#install) command.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
php composer.phar install
|
|
|
|
```
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
When you run this command, one of two things may happen:
|
|
|
|
|
|
|
|
### Installing Without `composer.lock`
|
|
|
|
|
|
|
|
If you've never run the command before and there is also no `composer.lock` file present,
|
|
|
|
Composer simply resolves all dependencies listed in your `composer.json` file and downloads
|
|
|
|
the latest version of their files into the `vendor` directory in your project. (The `vendor`
|
|
|
|
directory is the conventional location for all third-party code in a project). In our
|
|
|
|
example from above, you would end up with the Monolog source files in
|
|
|
|
`vendor/monolog/monolog/`. If Monolog listed any dependencies, those would also be in
|
|
|
|
folders under `vendor/`.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2012-02-26 11:40:35 +00:00
|
|
|
> **Tip:** If you are using git for your project, you probably want to add
|
2015-06-22 14:24:59 +00:00
|
|
|
> `vendor` in your `.gitignore`. You really don't want to add all of that
|
2017-01-20 17:49:29 +00:00
|
|
|
> third-party code to your versioned repository.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
When Composer has finished installing, it writes all of the packages and the exact versions
|
|
|
|
of them that it downloaded to the `composer.lock` file, locking the project to those specific
|
2017-01-23 19:06:51 +00:00
|
|
|
versions. You should commit the `composer.lock` file to your project repo so that all people
|
|
|
|
working on the project are locked to the same versions of dependencies (more below).
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
### Installing With `composer.lock`
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
This brings us to the second scenario. If there's already a `composer.lock` file as well as a
|
|
|
|
`composer.json` file when you run `composer install`, it means that either you've run the
|
|
|
|
`install` command before, or someone else on the project has run the `install` command and
|
|
|
|
committed the `composer.lock` file to the project (which is good).
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
Either way, running `install` when a `composer.lock` file is present simply resolves and installs
|
|
|
|
all dependencies that you've listed in `composer.json`, but it uses the version constraints
|
|
|
|
that it finds in `composer.lock` to ensure that the package versions are consistent for everyone
|
|
|
|
working on your project. The result is that you have all dependencies requested by your
|
|
|
|
`composer.json` file, but that they may not all be at the very latest available versions (since
|
2017-01-23 19:06:51 +00:00
|
|
|
some of the dependencies listed in the `composer.lock` file may have released newer versions since
|
2017-01-20 17:49:29 +00:00
|
|
|
the file was created). This is by design, as it ensures that your project never breaks because of
|
|
|
|
unexpected changes in dependencies.
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
### Commit Your `composer.lock` File to Version Control
|
2013-04-08 16:15:08 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
Committing this file to VC is important because it will cause anyone who sets
|
|
|
|
up the project to use the exact same
|
2017-01-20 01:04:24 +00:00
|
|
|
versions of the dependencies that you're using. Your CI server, production
|
|
|
|
machines, other developers in your team, everything and everyone runs on the
|
|
|
|
same dependencies, which mitigates the potential for bugs affecting only some
|
|
|
|
parts of the deployments. Even if you develop alone, in six months when
|
|
|
|
reinstalling the project you can feel confident the dependencies installed are
|
|
|
|
still working even if your dependencies released many new versions since then.
|
|
|
|
(See note below about using the `update` command.)
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
## Updating Dependencies to their Latest Versions
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2017-01-20 17:49:29 +00:00
|
|
|
As mentioned above, the `composer.lock` file prevents you from automatically getting
|
|
|
|
the latest versions of your dependencies. To update to the latest versions, use the
|
2015-07-21 13:46:38 +00:00
|
|
|
[`update`](03-cli.md#update) command. This will fetch the latest matching
|
|
|
|
versions (according to your `composer.json` file) and also update the lock file
|
2017-01-20 17:49:29 +00:00
|
|
|
with the new version. (This is equivalent to simply deleting the `composer.lock` file.)
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
php composer.phar update
|
|
|
|
```
|
2015-06-22 14:24:59 +00:00
|
|
|
> **Note:** Composer will display a Warning when executing an `install` command
|
|
|
|
> if `composer.lock` and `composer.json` are not synchronized.
|
|
|
|
|
2013-04-08 16:15:08 +00:00
|
|
|
If you only want to install or update one dependency, you can whitelist them:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
php composer.phar update monolog/monolog [...]
|
|
|
|
```
|
2013-04-08 16:15:08 +00:00
|
|
|
|
2015-06-23 06:47:30 +00:00
|
|
|
> **Note:** For libraries it is not necessary to commit the lock
|
2015-06-22 14:24:59 +00:00
|
|
|
> file, see also: [Libraries - Lock file](02-libraries.md#lock-file).
|
2012-03-27 20:49:35 +00:00
|
|
|
|
2012-02-18 17:15:57 +00:00
|
|
|
## Packagist
|
|
|
|
|
2012-11-08 14:08:02 +00:00
|
|
|
[Packagist](https://packagist.org/) is the main Composer repository. A Composer
|
2012-03-14 03:07:22 +00:00
|
|
|
repository is basically a package source: a place where you can get packages
|
2012-02-20 10:08:18 +00:00
|
|
|
from. Packagist aims to be the central repository that everybody uses. This
|
2017-01-20 01:04:24 +00:00
|
|
|
means that you can automatically `require` any package that is available there,
|
|
|
|
without further specifying where Composer should look for the package.
|
2012-02-18 17:15:57 +00:00
|
|
|
|
2015-06-23 06:47:30 +00:00
|
|
|
If you go to the [Packagist website](https://packagist.org/) (packagist.org),
|
2012-02-20 10:08:18 +00:00
|
|
|
you can browse and search for packages.
|
2012-02-18 17:15:57 +00:00
|
|
|
|
2015-07-21 13:46:38 +00:00
|
|
|
Any open source project using Composer is recommended to publish their packages
|
|
|
|
on Packagist. A library doesn't need to be on Packagist to be used by Composer,
|
2015-06-23 06:47:30 +00:00
|
|
|
but it enables discovery and adoption by other developers more quickly.
|
2012-02-18 17:15:57 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
## Platform packages
|
|
|
|
|
|
|
|
Composer has platform packages, which are virtual packages for things that are
|
|
|
|
installed on the system but are not actually installable by Composer. This
|
|
|
|
includes PHP itself, PHP extensions and some system libraries.
|
|
|
|
|
|
|
|
* `php` represents the PHP version of the user, allowing you to apply
|
|
|
|
constraints, e.g. `>=5.4.0`. To require a 64bit version of php, you can
|
|
|
|
require the `php-64bit` package.
|
|
|
|
|
|
|
|
* `hhvm` represents the version of the HHVM runtime (aka HipHop Virtual
|
|
|
|
Machine) and allows you to apply a constraint, e.g., '>=2.3.3'.
|
|
|
|
|
|
|
|
* `ext-<name>` allows you to require PHP extensions (includes core
|
|
|
|
extensions). Versioning can be quite inconsistent here, so it's often
|
|
|
|
a good idea to just set the constraint to `*`. An example of an extension
|
|
|
|
package name is `ext-gd`.
|
|
|
|
|
|
|
|
* `lib-<name>` allows constraints to be made on versions of libraries used by
|
|
|
|
PHP. The following are available: `curl`, `iconv`, `icu`, `libxml`,
|
|
|
|
`openssl`, `pcre`, `uuid`, `xsl`.
|
|
|
|
|
|
|
|
You can use [`show --platform`](03-cli.md#show) to get a list of your locally
|
|
|
|
available platform packages.
|
|
|
|
|
2012-02-18 17:11:00 +00:00
|
|
|
## Autoloading
|
|
|
|
|
2012-04-13 10:06:53 +00:00
|
|
|
For libraries that specify autoload information, Composer generates a
|
2017-01-20 17:49:29 +00:00
|
|
|
`vendor/autoload.php` file. You can simply include this file and start
|
|
|
|
using the classes that those libraries provide without any extra work:
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```php
|
2015-07-21 13:46:38 +00:00
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
$log = new Monolog\Logger('name');
|
|
|
|
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
|
|
|
|
$log->addWarning('Foo');
|
|
|
|
```
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2015-07-21 13:46:38 +00:00
|
|
|
You can even add your own code to the autoloader by adding an
|
2015-06-23 06:47:30 +00:00
|
|
|
[`autoload`](04-schema.md#autoload) field to `composer.json`.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"autoload": {
|
|
|
|
"psr-4": {"Acme\\": "src/"}
|
2012-02-18 17:11:00 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-01-03 15:46:56 +00:00
|
|
|
Composer will register a [PSR-4](http://www.php-fig.org/psr/psr-4/) autoloader
|
|
|
|
for the `Acme` namespace.
|
2012-04-13 10:06:53 +00:00
|
|
|
|
|
|
|
You define a mapping from namespaces to directories. The `src` directory would
|
2012-07-17 13:13:13 +00:00
|
|
|
be in your project root, on the same level as `vendor` directory is. An example
|
2014-01-03 15:46:56 +00:00
|
|
|
filename would be `src/Foo.php` containing an `Acme\Foo` class.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2015-07-21 13:46:38 +00:00
|
|
|
After adding the [`autoload`](04-schema.md#autoload) field, you have to re-run
|
|
|
|
[`dump-autoload`](03-cli.md#dump-autoload) to re-generate the
|
2015-06-23 06:47:30 +00:00
|
|
|
`vendor/autoload.php` file.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2012-02-26 11:40:35 +00:00
|
|
|
Including that file will also return the autoloader instance, so you can store
|
|
|
|
the return value of the include call in a variable and add more namespaces.
|
|
|
|
This can be useful for autoloading classes in a test suite, for example.
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```php
|
2015-07-21 13:46:38 +00:00
|
|
|
$loader = require __DIR__ . '/vendor/autoload.php';
|
2014-05-19 10:17:07 +00:00
|
|
|
$loader->add('Acme\\Test\\', __DIR__);
|
|
|
|
```
|
2012-02-18 17:11:00 +00:00
|
|
|
|
2015-07-21 13:46:38 +00:00
|
|
|
In addition to PSR-4 autoloading, Composer also supports PSR-0, classmap and
|
|
|
|
files autoloading. See the [`autoload`](04-schema.md#autoload) reference for
|
2015-06-23 06:47:30 +00:00
|
|
|
more information.
|
2012-04-13 10:06:53 +00:00
|
|
|
|
2015-06-22 14:24:59 +00:00
|
|
|
> **Note:** Composer provides its own autoloader. If you don't want to use that
|
|
|
|
> one, you can just include `vendor/composer/autoload_*.php` files, which return
|
|
|
|
> associative arrays allowing you to configure your own autoloader.
|
2012-03-07 16:35:53 +00:00
|
|
|
|
2012-03-08 05:41:29 +00:00
|
|
|
← [Intro](00-intro.md) | [Libraries](02-libraries.md) →
|