2012-02-18 19:03:24 +00:00
|
|
|
# Libraries
|
|
|
|
|
2015-06-23 06:47:30 +00:00
|
|
|
This chapter will tell you how to make your library installable through
|
|
|
|
Composer.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
|
|
|
## 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
|
2015-06-23 06:47:30 +00:00
|
|
|
package. When you add a [`require`](04-schema.md#require) to a project, you are
|
|
|
|
making a package that depends on other packages. The only difference between
|
2017-01-23 19:06:51 +00:00
|
|
|
your project and a library 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
|
2015-06-23 06:47:30 +00:00
|
|
|
this by adding the [`name`](04-schema.md#name) property in `composer.json`:
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"name": "acme/hello-world",
|
|
|
|
"require": {
|
|
|
|
"monolog/monolog": "1.0.*"
|
2012-02-18 19:03:24 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2015-06-23 06:47:30 +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
|
2021-05-17 20:18:06 +00:00
|
|
|
> username is usually a good bet. Package names must be lowercase, and the
|
|
|
|
> convention is to use dashes for word separation.
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
## Library Versioning
|
2012-11-07 12:04:10 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
In the vast majority of cases, you will be maintaining your library using some
|
|
|
|
sort of version control system like git, svn, hg or fossil. In these cases,
|
2020-06-30 15:39:56 +00:00
|
|
|
Composer infers versions from your VCS, and you **should not** specify a version
|
2017-01-23 19:06:51 +00:00
|
|
|
in your `composer.json` file. (See the [Versions article](articles/versions.md)
|
|
|
|
to learn about how Composer uses VCS branches and tags to resolve version
|
|
|
|
constraints.)
|
2012-11-07 12:04:10 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
If you are maintaining packages by hand (i.e., without a VCS), you'll need to
|
|
|
|
specify the version explicitly by adding a `version` value in your `composer.json`
|
|
|
|
file:
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"version": "1.0.0"
|
|
|
|
}
|
|
|
|
```
|
2012-02-18 19:03:24 +00:00
|
|
|
|
2017-03-08 09:58:55 +00:00
|
|
|
> **Note:** When you add a hardcoded version to a VCS, the version will conflict
|
|
|
|
> with tag names. Composer will not be able to determine the version number.
|
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
### VCS Versioning
|
2012-04-26 16:40:00 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
Composer uses your VCS's branch and tag features to resolve the version
|
2020-02-16 22:02:38 +00:00
|
|
|
constraints you specify in your [`require`](04-schema.md#require) field to specific sets of files.
|
2017-01-23 19:06:51 +00:00
|
|
|
When determining valid available versions, Composer looks at all of your tags
|
|
|
|
and branches and translates their names into an internal list of options that
|
2017-03-08 09:58:55 +00:00
|
|
|
it then matches against the version constraint you provided.
|
2012-04-26 16:40:00 +00:00
|
|
|
|
2017-01-23 19:06:51 +00:00
|
|
|
For more on how Composer treats tags and branches and how it resolves package
|
|
|
|
version constraints, read the [versions](articles/versions.md) article.
|
2012-04-26 16:40:00 +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
|
|
|
|
2020-06-30 15:39:56 +00:00
|
|
|
If you do not want to commit the lock file, and you are using git, add it to
|
2012-03-27 21:18:34 +00:00
|
|
|
the `.gitignore`.
|
2012-02-19 12:12:34 +00:00
|
|
|
|
2012-02-18 22:43:23 +00:00
|
|
|
## Publishing to a VCS
|
|
|
|
|
2015-06-23 06:47:30 +00:00
|
|
|
Once you have a VCS repository (version control system, e.g. git) containing a
|
2012-02-20 10:08:18 +00:00
|
|
|
`composer.json` file, your library is already composer-installable. In this
|
|
|
|
example we will publish the `acme/hello-world` library on GitHub under
|
2013-04-24 08:49:19 +00:00
|
|
|
`github.com/username/hello-world`.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2013-04-24 04:13:45 +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
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"name": "acme/blog",
|
|
|
|
"require": {
|
|
|
|
"acme/hello-world": "dev-master"
|
2012-02-18 22:43:23 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
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
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"name": "acme/blog",
|
|
|
|
"repositories": [
|
|
|
|
{
|
|
|
|
"type": "vcs",
|
|
|
|
"url": "https://github.com/username/hello-world"
|
2012-02-18 22:43:23 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
],
|
|
|
|
"require": {
|
|
|
|
"acme/hello-world": "dev-master"
|
2012-02-18 22:43:23 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
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
|
|
|
|
2013-07-01 00:52:10 +00:00
|
|
|
That's all. You can now install the dependencies by running Composer's
|
2015-06-23 06:47:30 +00:00
|
|
|
[`install`](03-cli.md#install) command!
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2016-06-22 05:19:29 +00:00
|
|
|
**Recap:** Any git/svn/hg/fossil repository containing a `composer.json` can be
|
|
|
|
added to your project by specifying the package repository and declaring the
|
2015-06-23 06:47:30 +00:00
|
|
|
dependency in the [`require`](04-schema.md#require) field.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
|
|
|
## Publishing to packagist
|
|
|
|
|
2015-06-22 12:10:54 +00:00
|
|
|
Alright, so now you can publish packages. But specifying the VCS repository
|
2012-02-20 10:08:18 +00:00
|
|
|
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
|
2015-06-22 12:10:54 +00:00
|
|
|
repository for `monolog/monolog`. How did that work? The answer is Packagist.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2012-11-08 14:08:02 +00:00
|
|
|
[Packagist](https://packagist.org/) is the main package repository for
|
2013-07-01 00:52:10 +00:00
|
|
|
Composer, and it is enabled by default. Anything that is published on
|
2015-06-23 06:47:30 +00:00
|
|
|
Packagist is available automatically through Composer. Since
|
|
|
|
[Monolog is on Packagist](https://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-04-13 12:35:13 +00:00
|
|
|
If we wanted to share `hello-world` with the world, we would publish it on
|
2020-10-23 19:52:05 +00:00
|
|
|
Packagist as well.
|
2012-02-18 22:43:23 +00:00
|
|
|
|
2020-10-23 19:52:05 +00:00
|
|
|
You visit [Packagist](https://packagist.org) and hit the "Submit"
|
2016-05-19 18:23:38 +00:00
|
|
|
button. This will prompt you to sign up if you haven't already, and then
|
|
|
|
allows you to submit 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
|
|
|
|
2024-03-19 14:23:35 +00:00
|
|
|
## Light-weight distribution packages
|
|
|
|
|
|
|
|
Some useless information like the `.github` directory, or large examples, test
|
|
|
|
data, etc. should typically not be included in distributed packages.
|
|
|
|
|
|
|
|
The `.gitattributes` file is a git specific file like `.gitignore` also living
|
|
|
|
at the root directory of your library. It overrides local and global
|
|
|
|
configuration (`.git/config` and `~/.gitconfig` respectively) when present and
|
|
|
|
tracked by git.
|
|
|
|
|
|
|
|
Use `.gitattributes` to prevent unwanted files from bloating the zip
|
|
|
|
distribution packages.
|
|
|
|
|
|
|
|
```text
|
|
|
|
// .gitattributes
|
|
|
|
/demo export-ignore
|
|
|
|
phpunit.xml.dist export-ignore
|
|
|
|
/.github/ export-ignore
|
|
|
|
```
|
|
|
|
|
|
|
|
Test it by inspecting the zip file generated manually:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
git archive branchName --format zip -o file.zip
|
|
|
|
```
|
|
|
|
|
|
|
|
> **Note:** Files would be still tracked by git just not included in the
|
|
|
|
> zip distribution. This only works for packages installed from
|
|
|
|
> dist (i.e. tagged releases) coming from GitHub, GitLab or Bitbucket.
|
|
|
|
|
2012-04-13 12:35:13 +00:00
|
|
|
← [Basic usage](01-basic-usage.md) | [Command-line interface](03-cli.md) →
|