PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/doc/01-basic-usage.md

http://github.com/composer/composer
Markdown | 273 lines | 207 code | 66 blank | 0 comment | 0 complexity | fd448a9be3a318632aaceebfcd8ea18a MD5 | raw file
  1. # Basic usage
  2. ## Introduction
  3. For our basic usage introduction, we will be installing `monolog/monolog`,
  4. a logging library. If you have not yet installed Composer, refer to the
  5. [Intro](00-intro.md) chapter.
  6. > **Note:** for the sake of simplicity, this introduction will assume you
  7. > have performed a [local](00-intro.md#locally) install of Composer.
  8. ## `composer.json`: Project setup
  9. To start using Composer in your project, all you need is a `composer.json`
  10. file. This file describes the dependencies of your project and may contain
  11. other metadata as well.
  12. ### The `require` key
  13. The first (and often only) thing you specify in `composer.json` is the
  14. [`require`](04-schema.md#require) key. You are simply telling Composer which
  15. packages your project depends on.
  16. ```json
  17. {
  18. "require": {
  19. "monolog/monolog": "1.0.*"
  20. }
  21. }
  22. ```
  23. As you can see, [`require`](04-schema.md#require) takes an object that maps
  24. **package names** (e.g. `monolog/monolog`) to **version constraints** (e.g.
  25. `1.0.*`).
  26. Composer uses this information to search for the right set of files in package
  27. "repositories" that you register using the [`repositories`](04-schema.md#repositories)
  28. key, or in Packagist, the default package repository. In the above example,
  29. since no other repository has been registered in the `composer.json` file, it is
  30. assumed that the `monolog/monolog` package is registered on Packagist. (See more
  31. about Packagist [below](#packagist), or read more about repositories
  32. [here](05-repositories.md)).
  33. ### Package names
  34. The package name consists of a vendor name and the project's name. Often these
  35. will be identical - the vendor name only exists to prevent naming clashes. For
  36. example, it would allow two different people to create a library named `json`.
  37. One might be named `igorw/json` while the other might be `seldaek/json`.
  38. Read more about publishing packages and package naming [here](02-libraries.md).
  39. (Note that you can also specify "platform packages" as dependencies, allowing
  40. you to require certain versions of server software. See
  41. [platform packages](#platform-packages) below.)
  42. ### Package version constraints
  43. In our example, we are requesting the Monolog package with the version constraint
  44. [`1.0.*`](https://semver.mwl.be/#?package=monolog%2Fmonolog&version=1.0.*).
  45. This means any version in the `1.0` development branch, or any version that is
  46. greater than or equal to 1.0 and less than 1.1 (`>=1.0 <1.1`).
  47. Please read [versions](articles/versions.md) for more in-depth information on
  48. versions, how versions relate to each other, and on version constraints.
  49. > **How does Composer download the right files?** When you specify a dependency in
  50. > `composer.json`, Composer first takes the name of the package that you have requested
  51. > and searches for it in any repositories that you have registered using the
  52. > [`repositories`](04-schema.md#repositories) key. If you have not registered
  53. > any extra repositories, or it does not find a package with that name in the
  54. > repositories you have specified, it falls back to Packagist (more [below](#packagist)).
  55. >
  56. > When Composer finds the right package, either in Packagist or in a repo you have specified,
  57. > it then uses the versioning features of the package's VCS (i.e., branches and tags)
  58. > to attempt to find the best match for the version constraint you have specified. Be sure to read
  59. > about versions and package resolution in the [versions article](articles/versions.md).
  60. > **Note:** If you are trying to require a package but Composer throws an error
  61. > regarding package stability, the version you have specified may not meet your
  62. > default minimum stability requirements. By default only stable releases are taken
  63. > into consideration when searching for valid package versions in your VCS.
  64. >
  65. > You might run into this if you are trying to require dev, alpha, beta, or RC
  66. > versions of a package. Read more about stability flags and the `minimum-stability`
  67. > key on the [schema page](04-schema.md).
  68. ## Installing dependencies
  69. To install the defined dependencies for your project, run the
  70. [`install`](03-cli.md#install) command.
  71. ```sh
  72. php composer.phar install
  73. ```
  74. When you run this command, one of two things may happen:
  75. ### Installing without `composer.lock`
  76. If you have never run the command before and there is also no `composer.lock` file present,
  77. Composer simply resolves all dependencies listed in your `composer.json` file and downloads
  78. the latest version of their files into the `vendor` directory in your project. (The `vendor`
  79. directory is the conventional location for all third-party code in a project). In our
  80. example from above, you would end up with the Monolog source files in
  81. `vendor/monolog/monolog/`. If Monolog listed any dependencies, those would also be in
  82. folders under `vendor/`.
  83. > **Tip:** If you are using git for your project, you probably want to add
  84. > `vendor` in your `.gitignore`. You really don't want to add all of that
  85. > third-party code to your versioned repository.
  86. When Composer has finished installing, it writes all of the packages and the exact versions
  87. of them that it downloaded to the `composer.lock` file, locking the project to those specific
  88. versions. You should commit the `composer.lock` file to your project repo so that all people
  89. working on the project are locked to the same versions of dependencies (more below).
  90. ### Installing with `composer.lock`
  91. This brings us to the second scenario. If there is already a `composer.lock` file as well as a
  92. `composer.json` file when you run `composer install`, it means either you ran the
  93. `install` command before, or someone else on the project ran the `install` command and
  94. committed the `composer.lock` file to the project (which is good).
  95. Either way, running `install` when a `composer.lock` file is present resolves and installs
  96. all dependencies that you listed in `composer.json`, but Composer uses the exact versions listed
  97. in `composer.lock` to ensure that the package versions are consistent for everyone
  98. working on your project. As a result you will have all dependencies requested by your
  99. `composer.json` file, but they may not all be at the very latest available versions
  100. (some of the dependencies listed in the `composer.lock` file may have released newer versions since
  101. the file was created). This is by design, it ensures that your project does not break because of
  102. unexpected changes in dependencies.
  103. ### Commit your `composer.lock` file to version control
  104. Committing this file to VC is important because it will cause anyone who sets
  105. up the project to use the exact same
  106. versions of the dependencies that you are using. Your CI server, production
  107. machines, other developers in your team, everything and everyone runs on the
  108. same dependencies, which mitigates the potential for bugs affecting only some
  109. parts of the deployments. Even if you develop alone, in six months when
  110. reinstalling the project you can feel confident the dependencies installed are
  111. still working even if your dependencies released many new versions since then.
  112. (See note below about using the `update` command.)
  113. ## Updating dependencies to their latest versions
  114. As mentioned above, the `composer.lock` file prevents you from automatically getting
  115. the latest versions of your dependencies. To update to the latest versions, use the
  116. [`update`](03-cli.md#update) command. This will fetch the latest matching
  117. versions (according to your `composer.json` file) and update the lock file
  118. with the new versions. (This is equivalent to deleting the `composer.lock` file
  119. and running `install` again.)
  120. ```sh
  121. php composer.phar update
  122. ```
  123. > **Note:** Composer will display a Warning when executing an `install` command
  124. > if the `composer.lock` has not been updated since changes were made to the
  125. > `composer.json` that might affect dependency resolution.
  126. If you only want to install, upgrade or remove one dependency, you can explicitly list it as an argument:
  127. ```sh
  128. php composer.phar update monolog/monolog [...]
  129. ```
  130. > **Note:** For libraries it is not necessary to commit the lock
  131. > file, see also: [Libraries - Lock file](02-libraries.md#lock-file).
  132. ## Packagist
  133. [Packagist](https://packagist.org/) is the main Composer repository. A Composer
  134. repository is basically a package source: a place where you can get packages
  135. from. Packagist aims to be the central repository that everybody uses. This
  136. means that you can automatically `require` any package that is available there,
  137. without further specifying where Composer should look for the package.
  138. If you go to the [Packagist website](https://packagist.org/) (packagist.org),
  139. you can browse and search for packages.
  140. Any open source project using Composer is recommended to publish their packages
  141. on Packagist. A library does not need to be on Packagist to be used by Composer,
  142. but it enables discovery and adoption by other developers more quickly.
  143. ## Platform packages
  144. Composer has platform packages, which are virtual packages for things that are
  145. installed on the system but are not actually installable by Composer. This
  146. includes PHP itself, PHP extensions and some system libraries.
  147. * `php` represents the PHP version of the user, allowing you to apply
  148. constraints, e.g. `^7.1`. To require a 64bit version of php, you can
  149. require the `php-64bit` package.
  150. * `hhvm` represents the version of the HHVM runtime and allows you to apply
  151. a constraint, e.g., `^2.3`.
  152. * `ext-<name>` allows you to require PHP extensions (includes core
  153. extensions). Versioning can be quite inconsistent here, so it's often
  154. a good idea to set the constraint to `*`. An example of an extension
  155. package name is `ext-gd`.
  156. * `lib-<name>` allows constraints to be made on versions of libraries used by
  157. PHP. The following are available: `curl`, `iconv`, `icu`, `libxml`,
  158. `openssl`, `pcre`, `uuid`, `xsl`.
  159. You can use [`show --platform`](03-cli.md#show) to get a list of your locally
  160. available platform packages.
  161. ## Autoloading
  162. For libraries that specify autoload information, Composer generates a
  163. `vendor/autoload.php` file. You can simply include this file and start
  164. using the classes that those libraries provide without any extra work:
  165. ```php
  166. require __DIR__ . '/vendor/autoload.php';
  167. $log = new Monolog\Logger('name');
  168. $log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
  169. $log->addWarning('Foo');
  170. ```
  171. You can even add your own code to the autoloader by adding an
  172. [`autoload`](04-schema.md#autoload) field to `composer.json`.
  173. ```json
  174. {
  175. "autoload": {
  176. "psr-4": {"Acme\\": "src/"}
  177. }
  178. }
  179. ```
  180. Composer will register a [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloader
  181. for the `Acme` namespace.
  182. You define a mapping from namespaces to directories. The `src` directory would
  183. be in your project root, on the same level as `vendor` directory is. An example
  184. filename would be `src/Foo.php` containing an `Acme\Foo` class.
  185. After adding the [`autoload`](04-schema.md#autoload) field, you have to re-run
  186. this command:
  187. ```sh
  188. php composer.phar dump-autoload
  189. ```
  190. This command will re-generate the `vendor/autoload.php` file.
  191. See the [`dump-autoload`](03-cli.md#dump-autoload-dumpautoload-) section for
  192. more information.
  193. Including that file will also return the autoloader instance, so you can store
  194. the return value of the include call in a variable and add more namespaces.
  195. This can be useful for autoloading classes in a test suite, for example.
  196. ```php
  197. $loader = require __DIR__ . '/vendor/autoload.php';
  198. $loader->addPsr4('Acme\\Test\\', __DIR__);
  199. ```
  200. In addition to PSR-4 autoloading, Composer also supports PSR-0, classmap and
  201. files autoloading. See the [`autoload`](04-schema.md#autoload) reference for
  202. more information.
  203. See also the docs on [optimizing the autoloader](articles/autoloader-optimization.md).
  204. > **Note:** Composer provides its own autoloader. If you don't want to use that
  205. > one, you can include `vendor/composer/autoload_*.php` files, which return
  206. > associative arrays allowing you to configure your own autoloader.
  207. &larr; [Intro](00-intro.md) | [Libraries](02-libraries.md) &rarr;