PHP Composer installation & usage
Release: 2016-07-29 09:14:59
Original
1260 people have browsed it
Basic usageBasic usage
- Installation
- display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json: Project installation
<ul> About Version (tilde operator)<li><codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>Stability<strong></strong>Install dependency packages</codecourier></li>
<li>display:inline-block<li>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer .lock</li> -
Lock file <li>Packagist<li>Auto-loading</ul></codecourier></li>
<li>
<li><codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>Installation</codecourier></li>
<li>Install Composer, you only need to download </li>display:inline-block<li>; line-height:18px; margin:0px 2px; background-color: rgb(249,242,244)">composer.phar</li> Executable file. </ul>
<precourier new font-size:14px padding:9.5px margin-top:0px margin-bottom:1.2em line-height:1.42857143 color:rgb word-break:break-all word-wrap:break-word border:none background-color:rgb><codeandale mono font-size:undefined padding:0px color:inherit direction:ltr word-spacing:normal word-break:normal background-color:transparent>curl -sS https://getcomposer.org/installer | php
</codeandale></precourier>
</li>Please see the Introduction chapter for details. <hrhelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>To check whether Composer is working properly, just pass <phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>display:inline-block; line-height:18px; margin :0px 2px; background-color:rgb(249,242,244)">php<h2helvetica neue grande sans gb yahei micro hei font-weight:normal line-height:1.35 color:rgb margin-top:1.2em margin-bottom:0.6em font-size:30px> to execute
PHAR: <precourier new font-size:14px padding:9.5px margin-top:0px margin-bottom:1.2em line-height:1.42857143 color:rgb word-break:break-all word-wrap:break-word border:none background-color:rgb><codeandale mono font-size:undefined padding:0px color:inherit direction:ltr word-spacing:normal word-break:normal background-color:transparent>php composer.phar
<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>This will return you a list of executable commands. <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>Note: <phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px> You can also just execute <codecourier new font-size:15.75px padding:0px color:inherit border:1px solid rgb>display:inline-block <phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">--check<codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb> option without downloading
Composer. For more information please use <codecourier new font-size:15.75px padding:0px color:inherit border:1px solid rgb>display:inline-block; line- height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">--help.<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px><precourier new font-size:14px padding:9.5px margin-top:0px margin-bottom:1.2em line-height:1.42857143 word-break:break-all word-wrap:break-word border:none background-color:rgb><codeandale mono font-size:undefined padding:0px color:inherit direction:ltr word-spacing:normal word-break:normal background-color:transparent>curl -sS https://getcomposer.org/installer | php -- --help
<blockquotehelvetica neue grande sans gb yahei micro hei><p><h2helvetica neue grande sans gb yahei micro hei font-weight:normal line-height:1.35 color:rgb margin-top:1.2em margin-bottom:0.6em font-size:30px><codecourier new font-size:27px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:33.75px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json:项目安装</codecourier></h2helvetica></p>
<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>要开始在你的项目中使用 Composer,你只需要一个 <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json 文件。该文件包含了项目的依赖和其它的一些元数据。<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>这个 JSON format 是很容易编写的。它允许你定义嵌套结构。<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px><h3helvetica neue grande sans gb yahei micro hei font-weight:normal line-height:1.35 color:rgb margin-top:1.2em margin-bottom:0.6em font-size:24px>关于 <codecourier new font-size:21.6000003814697px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:27px; margin:0px 2px; background-color:rgb(249,242,244)"><strong>require</strong> Key<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>第一件事情(并且往往只需要做这一件事),你需要在 <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json 文件中指定 <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)"><strong>require</strong> key
的值。你只需要简单的告诉 Composer 你的项目需要依赖哪些包。<precourier new font-size:14px padding:9.5px margin-top:0px margin-bottom:1.2em line-height:1.42857143 color:rgb word-break:break-all word-wrap:break-word border:none background-color:rgb><codeandale mono font-size:undefined padding:0px color:inherit direction:ltr word-spacing:normal word-break:normal background-color:transparent>{
"<strong>require</strong>": {
"monolog/monolog": "1.0.*"
}
}
<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>你可以看到, <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)"><strong>require</strong> 需要一个 <span>包名称</span> (例如 <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">monolog/monolog)
映射到 <span>包版本</span> (例如 <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0.*)
的<strong>对象</strong>。<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px><h3helvetica neue grande sans gb yahei micro hei font-weight:normal line-height:1.35 color:rgb margin-top:1.2em margin-bottom:0.6em font-size:24px>包名称<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>The package name consists of the vendor name and its project name. It is usually easy to produce the same project name, and the existence of the supplier name can solve the naming conflict problem well. It allows two different people to create libraries with the same name <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">json which will then be Named <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">igorw/json and <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">seldaek/json.<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>Here we need to introduce <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb (249,242,244)">monolog/monolog, the vendor name is the same as the name of the project, we recommend this for a project with a unique name. It also allows adding more related projects in the same namespace later. If you maintain a library, this will allow you to easily separate it into smaller parts. <phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px><h3helvetica neue grande sans gb yahei micro hei font-weight:normal line-height:1.35 color:rgb margin-top:1.2em margin-bottom:0.6em font-size:24px>Package version<phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>In the previous example, the monolog version we introduced was specified as <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)"> 1.0.*. This means any development branch starting from <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0 will match <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb> display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0.0、<codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0.2 or <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0.20 . <phelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px>Version constraints can be specified in several different ways.<tablehelvetica neue grande sans gb yahei micro hei font-size:16px line-height:28.7999992370605px><thead><tr>
<th>Name</th>
<th>Instance</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td>Exact version number</td>
<td><codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242 ,244) ">1.0.2</codecourier></td>
<td>You can specify the exact version of the package. </td>
</tr>
<tr>
<td>range</td>
<td><codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244) ">>=1.0 <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">>=1.0,<2.0display:inline- block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">>=1.0,<1.1|>=1.2</codecourier></codecourier></td>
<td>A valid version can be specified by using comparison operators scope. <br>Valid operators: <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">>, <codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height :18px; margin:0px 2px; background-color:rgb(249,242,244)">>=、<codecourier new font-size:14.3999996185303px padding:0px color:inherit border:1px solid rgb>display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244) "><, display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)"><=, display:inline-block</strong>; line- height:18px; margin:0px 2px; background-color:rgb(249,242,244)">!=.
You can define multiple ranges, separated by commas, which will be treated as a logical ANDprocess. A pipeline The symbol display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">| will be treated as logical OR.
AND has higher priority than OR. Wildcards | display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0.* | You can use wildcardsdisplay:inline -block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">* to specify a mode. display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">1.0.* and display:inline-block</strong>; line-height:18px; margin: 0px 2px; background-color:rgb(249,242,244)">>=1.0,<1.1 is equivalent. |
Assignment operator | display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 | This is for following semantic versioning project is very useful.display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 is equivalent to display:inline-block</strong>; line-height:18px; margin: 0px 2px; background-color:rgb(249,242,244)">>=1.2,<2.0. To learn more, read the next section. |
The next big version (tilde operator)display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">~ Best examples To explain: display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 is equivalent to display:inline-block</strong>; line-height:18px ; margin:0px 2px; background-color:rgb(249,242,244)">>=1.2,<2.0, while display:inline-block</strong>; line-height:18px; margin:0px 2px; background-color: rgb(249,242,244)">~1.2.3 is equivalent to display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">>=1.2.3,< ;1.3. As you can see this is most useful for projects that follow semantic versioning. A common usage is to mark the minimum version you depend on, like display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 (any version above 1.2 is allowed, but not 2.0). Since in theory there should be no backwards compatibility issues until 2.0, this works great. You'll also see another use of it, using display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">~ to specify a minimum version, but allow The last digit of the version number goes up. Note: Although display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">2.0-beta .1 Strictly speaking, it is earlier than display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">2.0, however, according to version constraints,
For example, display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 will not install this version. As mentioned before display: inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">~1.2 only means display:inline-block; line-height:19.6875px; margin: 0px 2px; background-color:rgb(249,242,244)">.2 part can be changed, but display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)" >1. part is fixed.
稳定性默认情况下只有稳定的发行版才会被考虑在内。如果你也想获得 RC、beta、alpha 或 dev 版本,你可以使用 稳定标志。你可以对所有的包做 最小稳定性 设置,而不是每个依赖逐一设置。安装依赖包获取定义的依赖到你的本地项目,只需要调用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.phar 运行 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install 命令。php composer.phar install
接着前面的例子,这将会找到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">monolog/monolog 的最新版本,并将它下载到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor 目录。
这是一个惯例把第三方的代码到一个指定的目录 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor。如果是
monolog 将会创建 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/monolog/monolog 目录。小技巧: 如果你正在使用Git来管理你的项目, 你可能要添加 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">vendor 到你的 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">.Gitignore 文件中。
你不会希望将所有的代码都添加到你的版本库中。
另一件事是 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install 命令将创建一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.lock 文件到你项目的根目录中。display:inline-block; line-height:33.75px; margin:0px 2px; background-color:rgb(249,242,244)">composer.lock -
Lock fileAfter installing dependencies, Composer will write the exact version number list during installation display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)"> composer.lock file. This will lock the specific version of your project. Please submit display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244 )">composer.lock (including display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json) to your repository This is very important because the display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install command will check whether the lock file exists, if exists, it will download the specified version (ignoring the definitions in the display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json file). This means that anyone building the project will download the exact same dependency as the specified version. Your continuous integration server, production environment, other developers on your team, everything and everyone will use the same dependency. Mitigate the impact of potential bugs on your deployment. Even if you develop the project alone, you can continue to work with confidence when you reinstall the project in six months, even if many new versions of your dependencies have been released since then If it does not exist display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.lock file, Composer
Will read display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json and create the lock file.这意味着如果你的依赖更新了新的版本,你将不会获得任何更新。此时要更新你的依赖版本请使用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">update 命令。这将获取最新匹配的版本(根据你的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json 文件)并将新版本更新进锁文件。php composer.phar update
如果只想安装或更新一个依赖,你可以白名单它们:php composer.phar update monolog/monolog [...]
注意: 对于库,并不一定建议提交锁文件 请参考:库的锁文件.
Packagistpackagist 是 Composer 的主要资源库。 一个 Composer 的库基本上是一个包的源:记录了可以得到包的地方。Packagist 的目标是成为大家使用库资源的中央存储平台。这意味着你可以 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">require 那里的任何包。当你访问 packagist website (packagist.org),你可以浏览和搜索资源包。任何支持 Composer 的开源项目应该发布自己的包在 packagist 上。虽然并不一定要发布在 packagist 上来使用 Composer,但它使我们的编程生活更加轻松。自动加载对于库的自动加载信息,Composer 生成了一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/autoload.php 文件。你可以简单的引入这个文件,你会得到一个免费的自动加载支持。require'vendor/autoload.php';这使得你可以很容易的使用第三方代码。例如:如果你的项目依赖 monolog,你就可以像这样开始使用这个类库,并且他们将被自动加载。$log=newMonolog\Logger('name');$log->pushHandler(newMonolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));$log->addWarning('Foo');你可以在 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">composer.json 的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">autoload 字段中增加自己的
autoloader。{
"autoload": {
"psr-4": {"Acme\\": "src/"}
}
}
Composer 将注册一个 PSR-4 autoloader 到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">Acme 命名空间。你可以定义一个从命名空间到目录的映射。此时 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">src 会在你项目的根目录,与 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor 文件夹同级。例如 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">src/Foo.php 文件应该包含 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">Acme\Foo 类。添加 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">autoload 字段后,你应该再次运行 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">install 命令来生成 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/autoload.php 文件。引用这个文件也将返回 autoloader 的实例,你可以将包含调用的返回值存储在变量中,并添加更多的命名空间。这对于在一个测试套件中自动加载类文件是非常有用的,例如。$loader=require'vendor/autoload.php';$loader->add('Acme\\Test\\',__DIR__);除了 PSR-4 自动加载,classmap 也是支持的。这允许类被自动加载,即使不符合 PSR-0 规范。详细请查看 自动加载-参考。Note: Composer provides its own autoloader. If you don't want to use it, you can just include the display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)">vendor/composer/autoload_*.php files , it returns an associative array through which you can configure your own
autoloader.
The above introduces the installation and use of PHP Composer, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31