May 25, 2021 Composer
11.. Dependency detection depends
12.. Validity detection validate
13.. Depends on the package state to detect the status
16.. Create a project create-project
17.. Print the autoload index dump-autoload
You've learned how to do something with the command-line interface. This chapter will introduce you to all available commands.
To get help from the command line, run the
composer
or
composer list
and then combine the
--help
command to get more help.
The following parameters can be used in conjunction with each command:
In
that chapter
of the library we saw how to manually create a
composer.json
file.
In fact, there is
init
command that makes it easier to do this.
When you run the command, it interactively asks you to fill in some information, while using some default values wisely.
php composer.phar init
foo/bar:1.0.0
minimum-stability
field.
install
command reads the
composer.json
the current directory, handles dependencies, and installs it into the
vendor
directory.
php composer.phar install
If a composer.lock file
composer.lock
it reads the dependent version from
composer.json
file.
This ensures that each consumer of the library gets the same dependent version.
If
composer.lock
file, composer creates the dependency after it has been processed.
source
and
dist
T
he dist method will be used by default for
dist
S
ource,
source
other, represents the versioning source. I
f
--prefer-source
enabled, composer will install
source
if any. T
his is useful if you want to use a bugfix into your project.
Dependencies can also be obtained directly from the local repository.
--prefer-source
composer will get as
dist
as possible, which will greatly speed up the installation on build servers.
This is also a way to avoid git questions if you don't know how to set it up correctly.
--dry-run
which simulates the installation and shows what will happen.
require-dev
field (this is a default).
require-dev
field.
composer.json
file.
In order to get the latest version of the dependency
composer.lock
file, you should use
update
command.
php composer.phar update
This resolves all dependencies on the project and writes the exact version number
composer.lock
If you just want to update a few packages, you can list them separately like this:
php composer.phar update vendor/package vendor/package2
You can also use wildcards for bulk updates:
php composer.phar update vendor/*
source
dist
require-dev
field (this is a default).
require-dev
field.
composer.json
file.
require
command adds a new dependency package to the
composer.json
directory.
php composer.phar require
When you add or change a dependency, the modified dependency is installed or updated.
If you don't want to specify a dependency package through interaction, you can specify the dependent package directly in this order.
php composer.phar require vendor/package:2.* vendor/package2:dev-master
source
dist
require-dev
field.
global
command allows you to COMPOSER_HOME commands, such as
install
or update, in the
require
of
update
And if you
$COMPOSER_HOME/vendor/bin
$PATH
environment variable, you can use it to install the global app on the command line, here's an example:
php composer.phar global require fabpot/php-cs-fixer:dev-master
The
php-cs-fixer
worldwide (assuming you've set up your PATH).
If you want to update it later, you just need to
global update
php composer.phar global update
search
command allows you to search for dependency packages for the current item, usually searching only for packages on packagist.org, and you can simply enter your search criteria.
php composer.phar search monolog
You can also perform a multi-conditional search by passing multiple parameters.
List all available packages and you can use
show
command.
php composer.phar show
If you want to see the details of a package, you can enter a package name.
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] http://github.com/Seldaek/monolog.git 3d4e60d0cbc4b888fe5ad223d77964428b1978da
dist : [zip] http://github.com/Seldaek/monolog/zipball/3d4e60d0cbc4b888fe5ad223d77964428b1978da 3d4e60d0cbc4b888fe5ad223d77964428b1978da
license : MIT
autoload
psr-0
Monolog : src/
requires
php >=5.3.0
You can even enter the version number of a package to display the details of that version.
php composer.phar show monolog/monolog 1.0.2
depends
command finds out if a package has been installed in your project, is being relied on by other packages, and lists them.
php composer.phar depends --link-type=require monolog/monolog
nrk/monolog-fluent
poc/poc
propel/propel
symfony/monolog-bridge
symfony/symfony
require
or
require-dev
You should always run the validate command before submitting the
composer.json
file and
validate
tag.
It will detect if
composer.json
file is valid
php composer.phar validate
If you frequently modify the code in the dependency package, and they are installed from source
status
allows you to check, and it will prompt you if you have any local changes.
php composer.phar status
You can
--verbose
series parameters (-v|vvvvv| for more detail:
php composer.phar status -v
You have changes in the following dependencies:
vendor/seld/jsonlint:
M README.mdown
To upgrade Composer itself to the latest version, you only need to
self-update
command.
It will replace your
composer.phar
to the latest version.
php composer.phar self-update
If you want to upgrade to a specific version, you can specify it as simple as this:
php composer.phar self-update 1.0.0-alpha7
If you already have Composer installed for the entire system
(see Global Installation),
you may need
root
it under root permission:
sudo composer self-update
config
config
command allows you to edit some of composer's basic settings, whether
composer.json
or
config.json
file.
php composer.phar config --list
config [options] [setting-key] [setting-value1] ... [setting-valueN]
setting-key
is the name of a configuration option,
setting-value1
is the value of a configuration.
You can use arrays as configured values
github-protocols
and
setting-value
are allowed.
For valid configuration options, check out the config section of The Architecture.
$COMPOSER_HOME/config.json
If you do not specify this parameter, this command affects the composer.json file for the current project, or
--file
parameter that points to the file.
--global
exists, the global composer.json file opens.
setting-key
the name by set-key.
--global
exists, a list of global configuration options is displayed.
--global
parameter.
In addition to modifying configuration
config
the config command also supports modifying source information by:
php composer.phar config repositories.foo vcs http://github.com/foo/bar
You can use Composer to create a new project from an existing package.
This is equivalent to executing a
git clone
svn checkout
and installing the package's dependencies into its own vendor directory.
This command has several common uses:
To create a new project based on Composer, you can use the create-project command. P ass a package name that creates a catalog of items for you. You can also specify the version number in the third parameter, otherwise you will get the latest version.
If the directory does not currently exist, it is automatically created during installation.
php composer.phar create-project doctrine/orm path 2.2.*
You can also start this project with an existing
composer.json
of using this command.
By default, this command looks packagist.org package on the page.
composer
the composer repository, or a local path to a
packages.json
file.
stable
source
dist
require-dev
field.
In some cases you need to update autoloader, such as adding a new class to your package.
You can
dump-autoload
without having to execute
install
or
update
In addition, it can print an index of an optimized class that conforms to the PSR-0/4 specification, which is also due to performance considerations. There will be many class files in large applications, and autoloader takes up a large portion of the time per request, and using classmaps may not be convenient to develop, but it can still be facilitated by the PSR-0/4 specification while guaranteeing performance.
Lists the name, version, and license agreement of each package that has been installed.
You
--format=json
parameter to get the output of the JSON format.
You can run this command to execute the script
manually,
simply specifying the name of the
--no-dev
allows you to disable developer mode.
If you think you've found a bug or the program is acting strangely, you might want to run
diagnose
command to help you detect some common problems.
php composer.phar diagnose
This command is used to archive the specified version of the specified package in zip/tar. It can also be used to archive your entire project, excluding excluded/ignored (excluded/ignored) files.
php composer.phar archive vendor/package 2.0.21 --format=zip
Use
help
get help information for the specified command.
php composer.phar help install
You can set some environment variables to override the default configuration. I
t is recommended that you
composer.json
config
field as much as possible, rather than setting environment variables from the command line.
It is important to note that the value in the environment variable will always
composer.json
Environment variable
COMPOSER
specify additional file names
composer.json
files.
For example:
COMPOSER=composer-other.json php composer.phar install
By setting this environment variable, you can specify the version of the root package if the program cannot guess the version number from the VCS and does not
composer.json
file.
By setting this environment variable, you can specify that composer will depend on other directories installed outside of
vendor
By setting this environment variable, you can
bin
Binaries)
directory to a directory other than
vendor/bin
If you are using Composer through an HTTP proxy,
http_proxy
or HTTP_PROXY
HTTP_PROXY
variables. J
ust simply set it to the URL of the proxy server.
Many operating systems have set this variable for your service.
We recommend
http_proxy
(small case) or both. B
ecause some tools, like git or curl, will use
http_proxy
version.
In addition,
git config --global http.proxy <proxy url>
set up git's proxy separately.
If you are using a proxy server and want to disable proxy for some domain names, you
no_proxy
variables.
All you need to do is enter a list of domain names with
commas separated.
This environment variable accepts domain names, IP, and CIDR address blocks. Y
ou can limit it to one port (ex:
:80
You can also set
*
to ignore all HTTP proxy requests.
If you use an HTTP proxy,
request_fulluri
tags, you should set this environment variable to
false
0
to prevent composer from reading the configuration
request_fulluri
the file.
If you use an HTTPS proxy,
request_fulluri
tags, you should set this environment variable to
false
0
to prevent composer from reading the configuration from
request_fulluri
COMPOSER_HOME
allows you to change Composer's home directory.
This is a hidden global directory that all items share (available to all users of the machine).
Its default values on each system are:
/home/<user>/.composer
。
/Users/<user>/.composer
。
C:\Users\<user>\AppData\Roaming\Composer
。
You can
COMPOSER_HOME
config.json
When you
install
update
commands, Composer merges it with the
composer.json
in your project.
This file allows you to configure information and libraries for the user's project settings.
If
global and
project
have
the same configuration item, the
composer.json
has a higher priority.
COMPOSER_CACHE_DIR
variable allows you to set composer's cache directory, which can also
be configured through cache-dir.
Its default values on each system are:
$COMPOSER_HOME/cache
。
C:\Users\<user>\AppData\Local\Composer
%LOCALAPPDATA%/Composer
This environment variable controls the wait time for Composer to execute the command (ex: git command). The default is 300 seconds (5 minutes).
This environment variable controls the discard-changes config option.
If set to 1, this environment variable causes Composer to discard interaction when executing each command, equivalent to using
--no-interaction
This can be
done when building virtual machines/continuous
integration servers.