命令列介面/命令#

你已經學到如何使用命令列介面來做一些事情。 本章記錄所有可用的命令。

要從命令列取得幫助,簡單地呼叫 composercomposer list 來參閱完整的命令列表,然後 --help 與任意命令的組合可以給你更多資訊。

全域選項#

以下選項在每個命令都可用:

程序終止代碼#

init#

函式庫 章節我們看到了如何手動建立一個 composer.json。也有一個可用的 init 命令使得它更容易一些做到這一點。

當你執行該命令,它會互動式的詢問你來填入欄位, 同時使用一些聰明的預設值。

php composer.phar init

選項

install#

install 命令讀取目前目錄的 composer.json 檔案、解析依賴套件、並安裝他們到 vendor 中。

php composer.phar install

如果在目前目錄中有一個 composer.lock 檔案,它會使用檔案中的確切版本, 而非解析他們。 這確保了每個使用該函式庫的人會取得依賴套件的相同版本。

如果沒有 composer.lock 檔案,Composer 會在解析依賴之後建立一個。

選項

update#

為了取得依賴套件的最新版本,並且更新 composer.lock 檔案,你應該使用 update 命令。

php composer.phar update

這會解析該專案的所有依賴套件並寫入確切版本到 composer.lock 中。

如果你只想要更新幾個套件,而不是全部,你可以像這樣列出他們:

php composer.phar update vendor/package vendor/package2

你也可以使用萬用字元一次更新一堆套件:

php composer.phar update vendor/*

選項

require#

require 命令從目前目錄添加新套件到 composer.json 檔案。 如果檔案不存在,會在執行時建立。

php composer.phar require

在添加/變更需求之後, 修改的需求會被安裝或更新。

如果你不想以互動方式選擇需求, 你可以把他們傳遞給命令。

php composer.phar require vendor/package:2.* vendor/package2:dev-master

選項

remove#

remove 命令從目前目錄的 composer.json 檔案移除套件。

php composer.phar remove vendor/package vendor/package2

在移除需求之後, 修改的需求會被移除。

選項

global#

global 命令允許你執行其他命令像 installrequireupdate 如同你在 COMPOSER_HOME 目錄執行他們一樣。

This is merely a helper to manage a project stored in a central location that can hold CLI tools or Composer plugins that you want to have available everywhere.

This can be used to install CLI utilities globally. Here is an example:

php composer.phar global require fabpot/php-cs-fixer

Now the php-cs-fixer binary is available globally. Just make sure your global vendor binaries directory is in your $PATH environment variable, you can get its location with the following command :

php composer.phar global config bin-dir --absolute

If you wish to update the binary later on you can just run a global update:

php composer.phar global update

search 命令允許你透過目前專案的套件儲存庫搜尋。 通常這只會是 packagist。 你只需傳遞給它你想搜尋的條件。

php composer.phar search monolog

你也可以一次搜尋更多透過傳遞多個參數。

選項

show#

要列出所有可用的套件,你可以使用 show 命令。

php composer.phar show

To filter the list you can pass a package mask using wildcards.

php composer.phar show monolog/*

monolog/monolog 1.19.0 Sends your logs to files, sockets, inboxes, databases and various web services

如果你想看某個套件的詳情, 你可以傳遞套件名稱。

php composer.phar show monolog/monolog

name     : monolog/monolog
versions : master-dev, 1.0.2, 1.0.1, 1.0.0, 1.0.0-RC1
type     : library
names    : monolog/monolog
source   : [git] https://github.com/Seldaek/monolog.git 3d4e60d0cbc4b888fe5ad223d77964428b1978da
dist     : [zip] https://github.com/Seldaek/monolog/zipball/3d4e60d0cbc4b888fe5ad223d77964428b1978da 3d4e60d0cbc4b888fe5ad223d77964428b1978da
license  : MIT

autoload
psr-0
Monolog : src/

requires
php >=5.3.0

你甚至可以傳遞套件版本, 它會告訴你該指定版本的詳情。

php composer.phar show monolog/monolog 1.0.2

選項

outdated#

The outdated command shows a list of installed packages that have updates available, including their current and latest versions. This is basically an alias for composer show -lo.

The color coding is as such:

選項

browse / home#

browse(別名為 home)在你的瀏覽器中開啟套件的儲存庫 URL 或首頁。

選項

suggests#

Lists all packages suggested by currently installed set of packages. You can optionally pass one or multiple package names in the format of vendor/package to limit output to suggestions made by those packages only.

Use the --by-package or --by-suggestion flags to group the output by the package offering the suggestions or the suggested packages respectively.

Use the --verbose (-v) flag to display the suggesting package and the suggestion reason. This implies --by-package --by-suggestion, showing both lists.

選項

depends#

depends 命令告訴你某個套件被其他套件依賴。 As with installation require-dev relationships are only considered for the root package.

php composer.phar depends doctrine/lexer
 doctrine/annotations v1.2.7 requires doctrine/lexer (1.*)
 doctrine/common      v2.6.1 requires doctrine/lexer (1.*)

You can optionally specify a version constraint after the package to limit the search.

+Add the --tree or -t flag to show a recursive tree of why the package is +depended upon, for example:

php composer.phar depends psr/log -t
psr/log 1.0.0 Common interface for logging libraries
|- aboutyou/app-sdk 2.6.11 (requires psr/log 1.0.*)
|  `- __root__ (requires aboutyou/app-sdk ^2.6)
|- monolog/monolog 1.17.2 (requires psr/log ~1.0)
|  `- laravel/framework v5.2.16 (requires monolog/monolog ~1.11)
|     `- __root__ (requires laravel/framework ^5.2)
`- symfony/symfony v3.0.2 (requires psr/log ~1.0)
   `- __root__ (requires symfony/symfony ^3.0)

選項

prohibits#

The prohibits command tells you which packages are blocking a given package from being installed. Specify a version constraint to verify whether upgrades can be performed in your project, and if not why not. See the following example:

php composer.phar prohibits symfony/symfony 3.1
 laravel/framework v5.2.16 requires symfony/var-dumper (2.8.*|3.0.*)

Note that you can also specify platform requirements, for example to check whether you can upgrade your server to PHP 8.0:

php composer.phar prohibits php:8
 doctrine/cache        v1.6.0 requires php (~5.5|~7.0)
 doctrine/common       v2.6.1 requires php (~5.5|~7.0)
 doctrine/instantiator 1.0.5  requires php (>=5.3,<8.0-DEV)

As with depends you can request a recursive lookup, which will list all packages depending on the packages that cause the conflict.

選項

validate#

你始終應該在你 commit 你的 composer.json 檔案以及標記一個發行之前執行 validate 命令。它會檢查你的 composer.json 是否有效。

php composer.phar validate

選項

status#

如果你經常需要修改你依賴套件的程式碼,而他們是從來源安裝, 如果你在其中有本地變更, status 命令允許你檢查。

php composer.phar status

--verbose 選項你可以取得更多關於變更的資訊:

php composer.phar status -v

You have changes in the following dependencies:
vendor/seld/jsonlint:
    M README.mdown

self-update#

要更新 Composer 自身到最新版本,只要執行 self-update 命令。它會用最新版本取代你的 composer.phar

php composer.phar self-update

如果你只想更新而不是一個特定發行版本,只需指定它:

php composer.phar self-update 1.0.0-alpha7

如果你已經為你的整個系統安裝了 Composer(參閱 全域安裝), 你可能必須使用 root 特權執行命令

sudo -H composer self-update

選項

config#

The config command allows you to edit composer config settings and repositories in either the local composer.json file or the global config.json file.

Additionally it lets you edit most properties in the local composer.json.

php composer.phar config --list

使用#

config [options] [setting-key] [setting-value1] ... [setting-valueN]

setting-key 是一個配置選項名稱,而 setting-value1 是一個配置值。For settings that can take an array of values (like github-protocols), more than one setting-value arguments are allowed.

You can also edit the values of the following properties:

description, homepage, keywords, license, minimum-stability, name, prefer-stable, type and version.

有效的配置選項,參閱 配置

選項

修改儲存庫#

In addition to modifying the config section, the config command also supports making changes to the repositories section by using it the following way:

php composer.phar config repositories.foo vcs https://github.com/foo/bar

If your repository requires more configuration options, you can instead pass its JSON representation :

php composer.phar config repositories.foo '{"type": "vcs", "url": "http://svn.example.org/my-project/", "trunk-path": "master"}'

Modifying Extra Values#

In addition to modifying the config section, the config command also supports making changes to the extra section by using it the following way:

php composer.phar config extra.foo.bar value

The dots indicate array nesting, a max depth of 3 levels is allowed though. The above would set "extra": { "foo": { "bar": "value" } }.

create-project#

You can use Composer to create new projects from an existing package. This is the equivalent of doing a git clone/svn checkout followed by a "composer install" of the vendors.

There are several applications for this:

  1. You can deploy application packages.
  2. You can check out any package and start developing on patches for example.
  3. Projects with multiple developers can use this feature to bootstrap the initial application for development.

To create a new project using Composer you can use the "create-project" command. Pass it a package name, and the directory to create the project in. You can also provide a version as third argument, otherwise the latest version is used.

如果該目錄目前不存在,它會在安裝期間被建立。

php composer.phar create-project doctrine/orm path 2.2.*

It is also possible to run the command without params in a directory with an existing composer.json file to bootstrap a project.

預設情況下,該命令在 packagist.org 檢查套件。

選項

dump-autoload#

If you need to update the autoloader because of new classes in a classmap package for example, 你可以使用 "dump-autoload",而不須透過一個 install 或 update 達成。

此外,它可以倒出一個出於效能理由而轉換 PSR-0/4 套件為 classmap 的最佳化自動載入器。在大型應用程式中有很多類別, 自動載入器會佔用每個請求時間的主要部份。 Using classmaps for everything is less convenient in development, but using this option you can still use PSR-0/4 for convenience and classmaps for performance.

選項

clear-cache#

刪除 Composer 快取目錄中的所有內容。

licenses#

列出每個安裝套件的名稱、版本和授權許可。使用 --format=json 來取得機器可閱讀的輸出。

選項

run-script#

選項

要手動執行指令碼,你可以使用此命令, 只要給它指令碼名稱和任何需要的參數。

exec#

Executes a vendored binary/script. You can execute any command and this will ensure that the Composer bin-dir is pushed on your PATH before the command runs.

選項

diagnose#

如果你認為你發現了一個錯誤,或一些奇怪的行為,你可能想要運行 diagnose 命令來執行自動化檢查許多常見問題。

php composer.phar diagnose

archive#

這個命令是用來為所給套件的所給版本產生一個 zip/tar 封存。 它也可以被用來封存你的整個專案, 不包括排除/忽略的檔案。

php composer.phar archive vendor/package 2.0.21 --format=zip

選項

help#

要取得更多關於某些命令的資訊,只要使用 help

php composer.phar help install

Command-line completion#

Command-line completion can be enabled by following instructions on this page.

環境變數#

You can set a number of environment variables that override certain settings. Whenever possible it is recommended to specify these settings in the config section of composer.json instead. It is worth noting that the env vars will always take precedence over the values specified in composer.json.

COMPOSER#

透過設定 COMPOSER 環境變數來設定 composer.json 成其他的檔案名稱是可行的。

例如:

COMPOSER=composer-other.json php composer.phar install

The generated lock file will use the same name: composer-other.lock in this example.

COMPOSER_ROOT_VERSION#

透過設定此變數,你可以指定 root 套件的版本,如果它不能從 VCS 資訊被猜測而且不存在 composer.json 中。

COMPOSER_VENDOR_DIR#

透過設定此變數,你可以讓 Composer 安裝依賴套件到 vendor 以外的目錄中。

COMPOSER_BIN_DIR#

透過設定此選項,你可以變更 binVendor 二進制文件) 到 vendor/bin 以外的目錄。

http_proxy 或 HTTP_PROXY#

如果你正透過一個 HTTP 代理使用 Composer,你可以使用標準 http_proxyHTTP_PROXY 環境變數。簡單地設定它指向你的代理 URL。 許多操作系統已經為你設定了此變數。

使用 http_proxy(小寫) 或甚至兩個都定義會比較好,因為一些工具像 git 或 curl 只使用小寫的 http_proxy 版本。 另一個選擇,你也可以使用 git config --global http.proxy <proxy url> 來定義 git 代理。

If you are using Composer in a non-CLI context (i.e. integration into a CMS or similar use case), and need to support proxies, please provide the CGI_HTTP_PROXY environment variable instead. See httpoxy.org for further details.

no_proxy#

如果你在一個代理之後,並想要在某些網域下停用它, 你可以使用 no_proxy 環境變數。 只需設定一個以逗號分隔,不應該 使用代理的網域列表,

環境變數接受網域、IP 地址、和 CIDR 表示的 IP 地址區塊。 你可以限制過濾器到一個特定連接埠(例如 :80)。 你也可以設定它為 * 來對所有 HTTP 請求忽略代理。

HTTP_PROXY_REQUEST_FULLURI#

如果你使用一個代理但它不支援 request_fulluri 旗標, 那麼你應該設定此環境變數為 false0 來防止 Composer 設定 request_fulluri 選項。

HTTPS_PROXY_REQUEST_FULLURI#

如果你使用一個代理但它不對 HTTPS 請求支援 request_fulluri 旗標, 那麼你應該設定此環境變數為 false0 來防止 Composer 設定 request_fulluri 選項。

COMPOSER_HOME#

COMPOSER_HOME 變數允許你變更 Composer 家目錄。 這是一個隱藏的、全域(機器上的每個使用者)目錄, 它在所有專案之間共享。

By default it points to C:\Users\<user>\AppData\Roaming\Composer on Windows and /Users/<user>/.composer on OSX. On nix systems that follow the XDG Base Directory Specifications, it points to $XDG_CONFIG_HOME/composer. On other nix systems, it points to /home/<user>/.composer.

COMPOSER_HOME/config.json#

You may put a config.json file into the location which COMPOSER_HOME points to. Composer will merge this configuration with your project's composer.json when you run the install and update commands.

這個檔案允許你為使用者的專案設定 儲存庫配置

在全域與 本地 配置匹配的情況下, 在專案 composer.json 中的 本地 配置始終會勝出。

COMPOSER_CACHE_DIR#

COMPOSER_CACHE_DIR 變數允許你變更 Composer 快取目錄, 它也可以經由 cache-dir 選項來配置。

預設情況下,在 *nix 和 OSX 它指向 $COMPOSER_HOME/cache,在 Windows 是 C:\Users\<user>\AppData\Local\Composer(或%LOCALAPPDATA%/Composer)。

COMPOSER_PROCESS_TIMEOUT#

這個環境變數控制 Composer 等待命令執行完成的時間(例如 git 命令)。 預設值是 300 秒(5 分鐘)。

COMPOSER_CAFILE#

By setting this environmental value, you can set a path to a certificate bundle file to be used during SSL/TLS peer verification.

COMPOSER_AUTH#

The COMPOSER_AUTH var allows you to set up authentication as an environment variable. The contents of the variable should be a JSON formatted object containing http-basic, github-oauth, bitbucket-oauth, ... objects as needed, and following the spec from the config.

COMPOSER_DISCARD_CHANGES#

這個環境變數控制 discard-changes config 選項。

COMPOSER_NO_INTERACTION#

如果設為 1,此環境變數會使 Composer 在每個命令的行為如同你傳遞了 --no-interaction 旗標。這可以被設置在建立虛擬機/持續整合(boxes/CI)。

COMPOSER_ALLOW_SUPERUSER#

If set to 1, this env disables the warning about running commands as root/super user. It also disables automatic clearing of sudo sessions, so you should really only set this if you use Composer as super user at all times like in docker containers.

COMPOSER_MIRROR_PATH_REPOS#

If set to 1, this env changes the default path repository strategy to mirror instead of symlink. As it is the default strategy being set it can still be overwritten by repository options.

COMPOSER_HTACCESS_PROTECT#

Defaults to 1. If set to 0, Composer will not create .htaccess files in the composer home, cache, and data directories.

函式庫 | 綱要

發現錯字?在這個文件中有些錯誤?只要 fork 並編輯 它!