Composer 常用命令总结(三)

init(初始化)

该命令用于创建 composer.json 文件,并进行基础信息配置:

$ composer init

可以配置Package name、Description、Author、Minimum、Package Type、License、dependencies 及 dev dependencies 信息。

完成后配置文件内容如下:

{
    "name": "test/test",
    "description": "test init",
    "type": "library",
    "license": "License Description",
    "authors": [
        {
            "name": "mayanlong",
            "email": "json_vip@163.com"
        }
    ],
    "require": {}
}

search(搜索)

根据名称搜索相关的包,成功后会列出符合的相关包的信息,本处以搜索 monolog 为例:

$ composer search monolog 
monolog/monolog Sends your logs to files, sockets, inboxes, databases and various web services
kdyby/monolog Integration of Monolog into Nette Framework

show(详情)

根据包的名称,列出包的相关信息,本处以查看 monolog/monolog 为例:

$ composer show -all monolog/monolog
name     : monolog/monolog
descrip. : Sends your logs to files, sockets, inboxes, databases and various web services
keywords : log, logging, psr-3
versions : dev-master, 2.0.x-dev, 1.x-dev, 1.21.0, 1.20.0, 1.19.0, 1.18.2, 1.18.1, 1.18.0, 1.17.2, 1.17.1, 1.17.0, 1.16.0, 1.15.0, 1.14.0, 1.13.1, 1.13.0, 1.12.0, 1.11.0, 1.10.0, 1.9.1, 1.9.0, 1.8.0, 1.7.0, 1.6.0, 1.5.0, 1.4.1, 1.4.0, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.2, 1.0.1, 1.0.0, 1.0.0-RC1

想查看更多信息,就亲自将该命令复制到命令行执行吧。

install (安装)

我们先在 composer.json 配置中添加一个 monolog/monolog 依赖库,如下:

{
    "name": "test/test",
    "description": "test init",
    "type": "library",
    "license": "License Description",
    "authors": [
        {
            "name": "mayanlong",
            "email": "json_vip@163.com"
        }
    ],
    "require": {
        "monolog/monolog": "1.21.*",
    }
}

然后通过如下命令进行安装依赖

$ composer install

update (更新)

如果我们新增或者删除了某个依赖,可以通过如下命令进行更新

$ composer update    

如果只想安装或更新一个依赖,可以使用如下命令:

$ composer update monolog/monolog [...]

require (申明依赖)

我们也可以用命令直接添加依赖,可以根据自己的需要选择拉取的版本,执行该命令后将自动下载,命令如下:

$ composer require symfony/http-foundation
$ composer require "symfony/http-foundation @dev"
$ composer require "symfony/http-foundation 4.0"
$ composer require "symfony/http-foundation ~4.0"
$ composer require "symfony/http-foundation ^4.0"

符号含义不清楚的可以查看 Composer 官网:https://getcomposer.org/doc/articles/versions.md#next-significant-release-operators

clear-chache (清除本地缓存)

安装一个包后过,就会缓存该包到本地,再次下载会优先从本地获取,如果不需要可以清掉这些本地缓存,命令如下:

$ composer clear-cache
Cache directory does not exist (cache-vcs-dir):
Clearing cache (cache-repo-dir): /root/.composer/cache/repo
Clearing cache (cache-files-dir): /root/.composer/cache/files
Clearing cache (cache-dir): /root/.composer/cache
All caches cleared.

--version(查看版本)

该命令用于用于查看 Composer 版本信息,命令如下:

$ composer --version
Composer version 1.5.2 2017-09-11 16:59:25

self-update (更新版本)

Composer 用了一段时间后,发现使用后会提示更新版本,命令如下:

$ composer self-update
Updating to version 1.5.6 (stable channel).
   Downloading (100%)
Use composer self-update --rollback to return to version 1.5.2

如果该版本有问题或者想用之前版本,可以回退到之前的版本,命令如下:

$ composer self-update --rollback 1.5.2
Rolling back to version 2017-09-11_16-59-25-1.5.2.

本文首发于马燕龙个人博客,欢迎分享,转载请标明出处。
马燕龙个人博客:https://www.mayanlong.com
马燕龙个人微博:http://weibo.com/imayanlong
马燕龙Github主页:https://github.com/yanlongma

标签: composer

不错,不错,对我有帮助! 我要打赏他!GO ->

已有 3 条评论

  1. threebeltone threebeltone

    fix clear-chache to clear-cache

  2. I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

添加新评论