默认情况下执行 composer 各种命令是去国外的 composer 官方镜像源获取需要安装的具体软件信息,在正常情况下国内访问国外服务器的速度相对比较慢

Composer 加速方法

一、修改镜像源为国内源

以阿里巴巴提供的 Composer 全量镜像为例:

镜像地址: https://mirrors.aliyun.com/composer/

配置只在当前项目生效

composer config repo.packagist composer https://mirrors.aliyun.com/composer/

取消当前项目配置

composer config --unset repos.packagist

配置全局生效

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

取消全局配置

composer config -g --unset repos.packagist

执行之后,composer 会利用默认值(也就是官方源)重置源地址。

二、通过第三方工具快速切换源CRM

CRM - Composer源管理工具

Composer Registry Manager 默认带了一个镜像列表,可以帮助你轻松、快速地切换到另外一个镜像。

GitHub地址: https://github.com/slince/composer-registry-manager

安装 crm

composer global require slince/composer-registry-manager

使用方法

列出所有可使用的镜像

$ composer repo:ls

--- ------------- ------------------------------------------------ ------------------------------
      composer      https://packagist.org                            Europe, Canada and Singapore
      aliyun        https://mirrors.aliyun.com/composer              China
      tencent       https://mirrors.cloud.tencent.com/composer       China
  *   huawei        https://mirrors.huaweicloud.com/repository/php   China
      cnpkg         https://php.cnpkg.org                            China
      sjtug         https://packagist.mirrors.sjtug.sjtu.edu.cn      China
      phpcomposer   https://packagist.phpcomposer.com                China
      kkame         https://packagist.kr                             South Korea
      hiraku        https://packagist.jp                             Japan
      webysther     https://packagist.com.br                         Brazil
      solidworx     https://packagist.co.za                          South Africa
      indra         https://packagist.phpindonesia.id                Indonesia
      varun         https://packagist.in                             India
 --- ------------- ------------------------------------------------ ------------------------------

标“*”表示当前正在使用的源;

你可以使用 --location xx 按地区过滤

$ composer repo:ls --location China

切换镜像

$ composer repo:use

Please select your favorite repository (defaults to composer) [composer]:
  [0 ] composer
  [1 ] aliyun
  [2 ] tencent
  [3 ] huawei
  [4 ] cnpkg
  [5 ] sjtug
  [6 ] phpcomposer
  [7 ] kkame
  [8 ] hiraku
  [9 ] webysther
  [10] solidworx
  [11] indra
  [12] varun
>

输入上面显示的源的前置数字回车即切换为选定的源。

你也可以直接追加镜像名称来跳过选择

$ composer repo:use aliyun

添加选项 --current/-c 为当前项目切换源,默认是修改全局的源。

重置命令

如果你想丢弃所有自定义的镜像源,你可以使用下面命令:

$ composer repo:reset

所有命令

执行下面命令查看

$ composer repo

  _____   _____        ___  ___
 /  ___| |  _  \      /   |/   |
 | |     | |_| |     / /|   /| |
 | |     |  _  /    / / |__/ | |
 | |___  | | \ \   / /       | |
 \_____| |_|  \_\ /_/        |_|
 
 Composer Repository Manager version 2.0.0
 
 Usage:
   command [options] [arguments]
 
 Options:
   -h, --help            Display this help message
   -q, --quiet           Do not output any message
   -V, --version         Display this application version
       --ansi            Force ANSI output
       --no-ansi         Disable ANSI output
   -n, --no-interaction  Do not ask any interactive question
   -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 
 Available commands for the "repo" namespace:
   repo:add     Creates a repository
   repo:ls      List all available repositories
   repo:remove  Remove a repository
   repo:use     Change current repository