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

修改镜像源

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

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

配置只在当前项目生效

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

取消当前项目配置

  1. composer config --unset repos.packagist

配置全局生效

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

取消全局配置

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

  1. composer config -g --unset repos.packagist

第三方工具快速切换源CRM

CRM - Composer源管理工具

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

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

安装 crm

  1. composer global require slince/composer-registry-manager

使用方法

列出所有可使用的镜像

  1. $ composer repo:ls
  2. --- ------------- ------------------------------------------------ ------------------------------
  3. composer https://packagist.org Europe, Canada and Singapore
  4. aliyun https://mirrors.aliyun.com/composer China
  5. tencent https://mirrors.cloud.tencent.com/composer China
  6. * huawei https://mirrors.huaweicloud.com/repository/php China
  7. cnpkg https://php.cnpkg.org China
  8. sjtug https://packagist.mirrors.sjtug.sjtu.edu.cn China
  9. phpcomposer https://packagist.phpcomposer.com China
  10. kkame https://packagist.kr South Korea
  11. hiraku https://packagist.jp Japan
  12. webysther https://packagist.com.br Brazil
  13. solidworx https://packagist.co.za South Africa
  14. indra https://packagist.phpindonesia.id Indonesia
  15. varun https://packagist.in India
  16. --- ------------- ------------------------------------------------ ------------------------------

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

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

  1. $ composer repo:ls --location China

切换镜像

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

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

  1. $ composer repo:use aliyun

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

重置命令

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

  1. $ composer repo:reset

所有命令

执行下面命令查看

  1. $ composer repo
  2. _____ _____ ___ ___
  3. / ___| | _ \ / |/ |
  4. | | | |_| | / /| /| |
  5. | | | _ / / / |__/ | |
  6. | |___ | | \ \ / / | |
  7. \_____| |_| \_\ /_/ |_|
  8. Composer Repository Manager version 2.0.0
  9. Usage:
  10. command [options] [arguments]
  11. Options:
  12. -h, --help Display this help message
  13. -q, --quiet Do not output any message
  14. -V, --version Display this application version
  15. --ansi Force ANSI output
  16. --no-ansi Disable ANSI output
  17. -n, --no-interaction Do not ask any interactive question
  18. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  19. Available commands for the "repo" namespace:
  20. repo:add Creates a repository
  21. repo:ls List all available repositories
  22. repo:remove Remove a repository
  23. repo:use Change current repository