2012-02-18 19:03:24 +00:00
|
|
|
# Libraries
|
|
|
|
|
|
|
|
This chapter will tell you how to make your library installable through composer.
|
|
|
|
|
|
|
|
## Every project is a package
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
As soon as you have a `composer.json` in a directory, that directory is a
|
|
|
|
package. When you add a `require` to a project, you are making a package that
|
|
|
|
depends on other packages. The only difference between your project and
|
|
|
|
libraries is that your project is a package without a name.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
In order to make that package installable you need to give it a name. You do
|
|
|
|
this by adding a `name` to `composer.json`:
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"name": "acme/hello-world",
|
|
|
|
"require": {
|
|
|
|
"monolog/monolog": "1.0.*"
|
|
|
|
}
|
2012-02-18 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
In this case the project name is `acme/hello-world`, where `acme` is the
|
|
|
|
vendor name. Supplying a vendor name is mandatory.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
> **Note:** If you don't know what to use as a vendor name, your GitHub
|
|
|
|
username is usually a good bet. While package names are case insensitive, the
|
|
|
|
convention is all lowercase and dashes for word separation.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
## Specifying the version
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
You need to specify the version some way. Depending on the type of repository
|
|
|
|
you are using, it might be possible to omit it from `composer.json`, because
|
|
|
|
the repository is able to infer the version from elsewhere.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
If you do want to specify it explicitly, you can just add a `version` field:
|
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"version": "1.0.0"
|
|
|
|
}
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
However if you are using git, svn or hg, you don't have to specify it.
|
|
|
|
Composer will detect versions as follows:
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
### Tags
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
For every tag that looks like a version, a package version of that tag will be
|
|
|
|
created. It should match 'X.Y.Z' or 'vX.Y.Z', with an optional suffix for RC,
|
|
|
|
beta, alpha or patch.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
Here are a few examples of valid tag names:
|
|
|
|
|
|
|
|
1.0.0
|
|
|
|
v1.0.0
|
|
|
|
1.10.5-RC1
|
|
|
|
v4.4.4beta2
|
|
|
|
v2.0.0-alpha
|
|
|
|
v2.0.4-p1
|
|
|
|
|
2012-02-19 21:58:36 +00:00
|
|
|
> **Note:** If you specify an explicit version in `composer.json`, the tag name must match the specified version.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
### Branches
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
For every branch, a package development version will be created. If the branch
|
|
|
|
name looks like a version, the version will be `{branchname}-dev`. For example
|
|
|
|
a branch `2.0` will get a version `2.0-dev`. If the branch does not look like
|
|
|
|
a version, it will be `dev-{branchname}`. `master` results in a `dev-master`
|
|
|
|
version.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
Here are some examples of version branch names:
|
|
|
|
|
|
|
|
1.0
|
|
|
|
1.*
|
|
|
|
1.1.x
|
|
|
|
1.1.*
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
> **Note:** When you install a dev version, it will install it from source.
|
2012-03-07 23:18:03 +00:00
|
|
|
See [Repositories](05-repositories.md) for more information.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-19 12:12:34 +00:00
|
|
|
## Lock file
|
|
|
|
|
2012-03-27 21:18:34 +00:00
|
|
|
For your library you may commit the `composer.lock` file if you want to. This
|
|
|
|
can help your team to always test against the same dependency versions.
|
|
|
|
However, this lock file will not have any effect on other projects that depend
|
|
|
|
on it. It only has an effect on the main project.
|
2012-02-19 12:12:34 +00:00
|
|
|
|
2012-03-27 21:18:34 +00:00
|
|
|
If you do not want to commit the lock file and you are using git, add it to
|
|
|
|
the `.gitignore`.
|
2012-02-19 12:12:34 +00:00
|
|
|
|
2012-02-18 22:43:23 +00:00
|
|
|
## Publishing to a VCS
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
Once you have a vcs repository (version control system, e.g. git) containing a
|
|
|
|
`composer.json` file, your library is already composer-installable. In this
|
|
|
|
example we will publish the `acme/hello-world` library on GitHub under
|
|
|
|
`github.com/composer/hello-world`.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
Now, To test installing the `acme/hello-world` package, we create a new
|
2012-03-17 00:14:53 +00:00
|
|
|
project locally. We will call it `acme/blog`. This blog will depend on
|
|
|
|
`acme/hello-world`, which in turn depends on `monolog/monolog`. We can
|
|
|
|
accomplish this by creating a new `blog` directory somewhere, containing a
|
2012-02-20 10:08:18 +00:00
|
|
|
`composer.json`:
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"name": "acme/blog",
|
|
|
|
"require": {
|
|
|
|
"acme/hello-world": "dev-master"
|
|
|
|
}
|
2012-02-18 22:43:23 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
The name is not needed in this case, since we don't want to publish the blog
|
|
|
|
as a library. It is added here to clarify which `composer.json` is being
|
|
|
|
described.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
Now we need to tell the blog app where to find the `hello-world` dependency.
|
|
|
|
We do this by adding a package repository specification to the blog's
|
|
|
|
`composer.json`:
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"name": "acme/blog",
|
2012-03-08 00:08:13 +00:00
|
|
|
"repositories": [
|
|
|
|
{
|
|
|
|
"type": "vcs",
|
|
|
|
"url": "https://github.com/composer/hello-world"
|
2012-02-29 14:56:53 +00:00
|
|
|
}
|
2012-03-08 00:23:39 +00:00
|
|
|
],
|
2012-02-29 14:56:53 +00:00
|
|
|
"require": {
|
|
|
|
"acme/hello-world": "dev-master"
|
2012-02-18 22:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
For more details on how package repositories work and what other types are
|
2012-03-07 23:18:03 +00:00
|
|
|
available, see [Repositories](05-repositories.md).
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
That's all. You can now install the dependencies by running composer's
|
|
|
|
`install` command!
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
**Recap:** Any git/svn/hg repository containing a `composer.json` can be added
|
|
|
|
to your project by specifying the package repository and declaring the
|
|
|
|
dependency in the `require` field.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
|
|
|
## Publishing to packagist
|
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
Alright, so now you can publish packages. But specifying the vcs repository
|
|
|
|
every time is cumbersome. You don't want to force all your users to do that.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
The other thing that you may have noticed is that we did not specify a package
|
|
|
|
repository for `monolog/monolog`. How did that work? The answer is packagist.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 15:18:31 +00:00
|
|
|
[Packagist](http://packagist.org/) is the main package repository for
|
|
|
|
composer, and it is enabled by default. Anything that is published on
|
|
|
|
packagist is available automatically through composer. Since monolog
|
|
|
|
[is on packagist](http://packagist.org/packages/monolog/monolog), we can depend
|
|
|
|
on it without having to specify any additional repositories.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
Assuming we want to share `hello-world` with the world, we would want to
|
|
|
|
publish it on packagist as well. And this is really easy.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-02-20 10:08:18 +00:00
|
|
|
You simply hit the big "Submit Package" button and sign up. Then you submit
|
2012-02-20 15:18:31 +00:00
|
|
|
the URL to your VCS repository, at which point packagist will start crawling
|
|
|
|
it. Once it is done, your package will be available to anyone.
|
2012-03-07 16:35:53 +00:00
|
|
|
|
|
|
|
← [Basic usage](01-basic-usage.md) | [Command-line interface](03-cli.md) →
|