Composer Command explanation

Source: Internet
Author: User
Tags autoload autoloader

You've learned how to do something with the command-line interface. This chapter will introduce you to all the available commands.

In order to get help information from the command line, run composer or composer list command, and then combine --help the commands to get more help information.

  • Command line
  • Global parameters
    • Process exit code
    • Initialization init --parameters
    • Installation install --parameters
    • Update update --parameters
    • Affirm dependency require --parameters
    • Global executionglobal
    • Search search --parameters
    • Show show --parameters
    • Dependency detection depends --parameters
    • Validity detectionvalidate
    • Dependency Packet State detectionstatus
    • Self-updating self-update --parameters
    • Change Configuration- config -How to use--parameters--Modify package source
    • Create Project create-project --parameters
    • Print auto-load index dump-autoload --parameters
    • View License Agreementlicenses
    • Execute scriptrun-script
    • Diagnosisdiagnose
    • Archive archive --parameters
    • Get help informationhelp
    • Environment variables
      • COMPOSER
      • Composer_root_version
      • Composer_vendor_dir
      • Composer_bin_dir
      • Http_proxy or Http_proxy
      • No_proxy
      • Http_proxy_request_fulluri
      • Https_proxy_request_fulluri
      • Composer_home
        • Composer_home/config.json
      • Composer_cache_dir
      • Composer_process_timeout
      • Composer_discard_changes
      • Composer_no_interaction

Global parameters

The following parameters can be used in conjunction with each command:

    • --verbose (-V): Increases the verbosity of the feedback information.
      • -V indicates normal output.
      • The-VV represents a more verbose output.
      • The-VVV is for debug.
    • --help (-H): Displays help information.
    • --quiet (-Q): Suppresses the output of any information.
    • --no-interaction (-N): Do not ask for any interaction issues.
    • --working-dir (-D): Use the given directory as the working directory, if specified.
    • --profile: Displays time and memory usage information.
    • --ansi: Force ANSI output.
    • --no-ansi: Turn off ANSI output.
    • --version (-V): Displays version information for the current application.

Process exit code
    • 0: Normal
    • 1: Generic/Unknown error
    • 2: Dependency Handling Error

Initialization init

In the "library" chapter we saw how to create a composer.json file manually. There is actually another init command that makes it easier to do this.

When you run the command, it will interactively ask you to fill in some information while using some of the default values wisely.

php composer.phar init

Initialize-parameter
    • --name: The name of the package.
    • --description: Description of the package.
    • --author: The author of the package.
    • --homepage: The home page of the package.
    • --require: Other packages that need to be relied on must have a version constraint. And should follow foo/bar:1.0.0 such a format.
    • --require-dev: The development version of the dependency package, the content format is the same as--require.
    • --stability (-s): minimum-stability The value of the field.

Installation install

installThe command reads the file from the current directory composer.json , processes the dependency, and installs it into the vendor directory.

php composer.phar install

If a file exists under the current directory composer.lock , it reads the dependent version from this file, rather than relying on the composer.json file to obtain it. This ensures that each user of the library will be given the same dependent version.

If there is no composer.lock file, composer will create it after processing the dependency.

Installation-Parameters
  • --prefer-source: There are two ways to download packages: source and dist . For stable version composer will be used by default dist . Instead, the source version control source is represented. If --prefer-source It is enabled, composer will be source installed from (if any). This is useful if you want to use a bugfix to your project. Dependencies can be obtained directly from the local repository.
  • --prefer-dist: --prefer-source In contrast, composer will be as far as possible from the dist acquisition, which will dramatically accelerate the installation on build servers. This is also a way to avoid git problems if you don't know how to set them correctly.
  • --dry-run: If you just want to demonstrate instead of actually installing a package, you can run --dry-run the command, which will simulate the installation and show what will happen.
  • --dev: require-dev The package listed in the installation field (this is a default value).
  • --no-dev: Skips require-dev the packages listed in the field.
  • --no-scripts: Skips composer.json the script defined in the file.
  • --no-plugins: Turn off plugins.
  • --no-progress: Removes progress information, which avoids confusing display of terminals or scripts that do not handle line breaks.
  • --optimize-autoloader (-O): Convert PSR-0/4 autoloading to CLASSMAP for faster load support. This is recommended especially in production environments, but because it takes some time to run, it is not a default value.

Update update

In order to get the latest version of the dependency and to upgrade the composer.lock file, you should use the update command.

php composer.phar update

This will resolve all dependencies on the project and write the exact version number composer.lock .

If you just want to update several packages, you can list them separately like this:

php composer.phar update vendor/package vendor/package2

You can also use wildcard characters for batch updates:

php composer.phar update vendor/*

Update-parameters
    • --prefer-source: When there are available packages, from the source installation.
    • --prefer-dist: When there are available packages, from the dist installation.
    • --dry-run: Analog command, and did not do the actual operation.
    • --dev: require-dev The package listed in the installation field (this is a default value).
    • --no-dev: Skips require-dev the packages listed in the field.
    • --no-scripts: Skips composer.json the script defined in the file.
    • --no-plugins: Turn off plugins.
    • --no-progress: Removes progress information, which avoids confusing display of terminals or scripts that do not handle line breaks.
    • --optimize-autoloader (-O): Convert PSR-0/4 autoloading to CLASSMAP for faster load support. This is recommended especially in production environments, but because it takes some time to run, it is not a default value.
    • --lock: Only the hash of the lock file is updated, and the warning about the lock file's obsolescence is canceled.
    • --with-dependencies also updates the dependencies of the whitelist package, which will be updated recursively.

Affirm Reliance require

requireThe command adds a new dependency package to the current directory composer.json in the file.

php composer.phar require

When you add or change dependencies, the modified dependencies are installed or updated.

If you do not want to specify a dependent package through interaction, you can indicate the dependency package directly in this order.

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

Affirm dependency-parameters
    • --prefer-source: When there are available packages, from the source installation.
    • --prefer-dist: When there are available packages, from the dist installation.
    • --dev: Installs require-dev the packages that are listed in the field.
    • --no-update: Disables automatic updating of dependencies.
    • --no-progress: Removes progress information, which avoids confusing display of terminals or scripts that do not handle line breaks.
    • --update-with-dependencies to update the new package's dependencies.

Global execution global

globalcommand allows you to execute other commands, such as, or, in the Composer_home directory install require update .

And if you add the $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

You php-cs-fixer can now use the global scope (assuming you have set your PATH). If later you want to update it, you just need to run global update :

php composer.phar global update

Search search

searchThe command allows you to search for a dependent package for the current project, usually it searches 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.

Search-Parameters
    • --only-name (-N): Search only for the specified name (exact match).

Show show

List all available packages and you can use the show commands.

php composer.phar show

If you want to see more information about a package, you can enter a package name.

php composer.phar show monolog/monologname     : monolog/monologversions : master-dev, 1.0.2, 1.0.1, 1.0.0, 1.0.0-RC1type     : librarynames    : monolog/monologsource   : [git] http://github.com/Seldaek/monolog.git 3d4e60d0cbc4b888fe5ad223d77964428b1978dadist     : [zip] http://github.com/Seldaek/monolog/zipball/3d4e60d0cbc4b888fe5ad223d77964428b1978da 3d4e60d0cbc4b888fe5ad223d77964428b1978dalicense  : MITautoloadpsr-0Monolog : src/requiresphp >=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

Show-parameters
    • --installed (-i): Lists the installed dependency packages.
    • --platform (-P): lists only the platform packages (PHP and its extensions).
    • --self (-s): Lists only the current project information.

Dependency detection depends

dependscommand to find out if a package has been installed in your project, is being relied upon by other packages, and lists them.

php composer.phar depends --link-type=require monolog/monolognrk/monolog-fluentpoc/pocpropel/propelsymfony/monolog-bridgesymfony/symfony

Dependency Detection-parameters
    • --link-type: Type of Detection, default require or Yes require-dev .

Validity detection validate

composer.jsonyou should always run the command before committing the file, and before creating the tag validate . It will detect if your composer.json files are valid.

php composer.phar validate
Validity test parameters
    • --no-check-all:composer whether to perform a full checksum.

Dependency Packet State detection status

If you frequently modify the code in the dependent packages and they are installed from source (custom sources), then the status command allows you to check if you have any local changes that will give you a hint.

php composer.phar status

You can use --verbose the series parameters (-V|VV|VVV) to get more detailed details:

php composer.phar status -vYou have changes in the following dependencies:vendor/seld/jsonlint:    M README.mdown

Self-renewal self-update

To upgrade the Composer itself to the latest version, you only need to run the self-update command. It will replace your file with the composer.phar latest version.

php composer.phar self-update

If you want to upgrade to a specific version, you can specify it in such a simple way:

php composer.phar self-update 1.0.0-alpha7

If you have installed Composer for the entire system (see Global installation), you may need to root run it under Permissions:

sudo composer self-update

Self-updating-parameters
    • --rollback (-R): Roll Back to the last version you have installed.
    • --clean-backups: Deletes the old backup during the update process, which makes the current version of the update the only available backup.

Change Configuration config

configThe command allows you to edit some of the basic settings of the Composer, whether local composer.json or global config.json .

php composer.phar config --list

Change configuration-How to use

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

setting-keyis the name of a configuration option and setting-value1 is a configured value. You can use an array as a configured value (like github-protocols ), and more than one setting-value is allowed.

For a valid configuration option, see Config in the "Schema" section.

Change Configuration-parameters
    • --global (-G): The operation is located in $COMPOSER_HOME/config.json the global configuration file. If you do not specify this parameter, this command affects the current project's Composer.json file, or --file the file that the parameter points to.
    • --editor (-E): Open the Composer.json file with a text editor. By default, the file for the current project is always open. When --global a parameter is present, the global Composer.json file is opened.
    • --unset: Removes setting-key the configuration option by the specified name.
    • --list (-L): Displays a list of current configuration options. When a --global parameter is present, a list of global configuration options is displayed.
    • --file= "..." (-F): Operates on a specified file, not Composer.json. Note: cannot be --global used with parameters.

Modify package source

In addition to modifying configuration options, the config command also supports modifying source information in the following ways:

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

Create a project create-project

You can use Composer to create a new project from an existing package. This is equivalent to executing one git clone or svn checkout command after installing the dependency of this package into its own vendor directory.

There are several common uses for this command:

    1. You can quickly deploy your app.
    2. You can check out any resource bundles and develop patches for it.
    3. Multi-person development project, which can be used to speed up the initialization of the application.

To create a new project based on Composer, you can use the "create-project" command. Pass in a package name and it will create a directory for your project. You can also specify a version number in the third parameter, or you will get the latest version.

If the directory does not currently exist, it will be created automatically during the installation process.

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

In addition, you can start the project with an existing file instead of using this command composer.json .

By default, this command looks for the package you specified on packagist.org.

Create Project-parameters
    • --repository-url: Provides a custom repository to search for packages, which will be used instead of packagist.org. Can be an composer HTTP URL that points to a repository, or a packages.json local path to a file.
    • --stability (-s): the lowest stable version of the resource bundle, by default stable .
    • --prefer-source: When there are available packages, from the source installation.
    • --prefer-dist: When there are available packages, from the dist installation.
    • --dev: Installs require-dev the packages that are listed in the field.
    • --no-install: Prohibit installation of package dependencies.
    • --no-plugins: Disables plugins.
    • --no-scripts: Disables script execution defined in the root resource bundle.
    • --no-progress: Removes progress information, which avoids confusing display of terminals or scripts that do not handle line breaks.
    • --keep-vcs: Skipping the missing VCs while creating. This is useful if you are running the Create command in non-interactive mode.

Print auto-load index dump-autoload

In some cases you need to update autoloader, such as adding a new class to your package. You can use it dump-autoload to complete without having to execute install or update command.

In addition, it can print an optimized index of classes that conform to the PSR-0/4 specification, which is also due to performance considerations. There will be many classes of files in a large application, and Autoloader will take up a large portion of each request, and using classmaps may not be easy to develop, but it can still be facilitated by the PSR-0/4 specification in the context of guaranteed performance.

Print auto-load index-parameters
    • --optimize (-O): Convert PSR-0/4 autoloading to Classmap for faster load times. This is especially true for production environments, but it may take some time to run, so it is not currently the default setting.
    • --no-dev: Disables the Autoload-dev rule.

View License Agreement licenses

Lists the name, version, and license Agreement for each package that you have installed. You can use --format=json parameters to get the output in JSON format.

Execute script run-script

You can run this command to execute the script manually, just specify the name of the script, and optional --no-dev parameters allow you to disable developer mode.

Diagnosis diagnose

If you find a bug or program behavior to be weird, you may need to run a diagnose command to help you detect some common problems.

php composer.phar diagnose

Archive archive

This command is used to Zip/tar archive the specified version of the specified package. It can also be used to archive your entire project, not including excluded/ignored (exclude/ignore) files.

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

Archive-parameters
    • --format (-F): Specifies the archive format: tar or zip (default is tar).
    • --dir: Specifies the directory in which the archive resides (default is the current directory).

Get help information help

Use help to get help information for the specified command.

php composer.phar help install

Environment variables

You can set some environment variables to override the default configuration. It is recommended that you composer.json set these values in the fields as much as possible, rather config than setting environment variables from the command line. It is important to note that the value in the environment variable will always take precedence over the composer.json value specified in.

COMPOSER

An environment variable COMPOSER can composer.json specify a different file name for the file.

For example:

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

Composer_root_version

By setting this environment variable, you can specify the version of the root package, if the program cannot guess the publication number from the VCS and is not declared in the composer.json file.

Composer_vendor_dir

By setting this environment variable, you can specify that composer will depend on vendor the installation in a different directory.

Composer_bin_dir

By setting this environment variable, you can specify a bin vendor/bin directory other than the (Vendor Binaries) directory.

Http_proxy or Http_proxy

If you are using Composer through an HTTP proxy, you can use http_proxy or HTTP_PROXY environment variables. Simply set it as the URL of the proxy server. Many operating systems have set this variable for your service.

It is recommended http_proxy to use (lowercase) or both to define. Because some tools, like git or curl, will use http_proxy lowercase versions. In addition, you can also use git config --global http.proxy <proxy url> the agent to set up git separately.

No_proxy

If you are using a proxy server and you want to disable the proxy for some domain names, you can use no_proxy environment variables. You only need to enter a comma-separated list of domain name exclusions .

This environment variable accepts domain names, IP, and CIDR address blocks. You can limit it to one port (for example: :80 ). You can also set it to * Ignore all HTTP proxy requests.

Http_proxy_request_fulluri

If you use an HTTP proxy, but it does not support request_fulluri tags, then you should set this environment variable to false or 0 , to prevent composer from reading the request_fulluri configuration.

Https_proxy_request_fulluri

If you use an HTTPS proxy, but it does not support request_fulluri tags, then you should set this environment variable to false or 0 , to prevent composer from reading the request_fulluri configuration.

Composer_home

COMPOSER_HOMEEnvironment variables allow you to change Composer's home directory. This is a hidden, all-Project Shared global catalog (available to all users of this machine).

Its default values on each system are:

    • *nix /home/<user>/.composer .
    • OSX /Users/<user>/.composer .
    • Windows C:\Users\<user>\AppData\Roaming\Composer .

Composer_home/config.json

You can COMPOSER_HOME place a file in the directory config.json . When you execute install and update command, Composer will merge it with the files in your project composer.json .

This file allows you to set configuration information and resource libraries for your users ' projects.

If the global and the project have the same configuration items, the files in the project composer.json have a higher priority.

Composer_cache_dir

COMPOSER_CACHE_DIREnvironment variables allow you to set the cache directory for Composer, which can also be cache-dir configured.

Its default values on each system are:

    • *nix and OSX $COMPOSER_HOME/cache .
    • Windows C:\Users\<user>\AppData\Local\Composer or %LOCALAPPDATA%/Composer .

Composer_process_timeout

This environment variable controls the wait time for the Composer to execute the command (for example, the git command). The default value is 300 seconds (5 minutes).

Composer_discard_changes

This environment variable controls the discard-changes config option.

Composer_no_interaction

If set to 1, this environment variable will cause Composer to discard the interaction when executing each command, which is equivalent to using all commands --no-interaction . This can be set up when building a virtual machine/Continuous Integration Server .

Composer Command explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.