PhpStorm 2016.3 Help

Using Composer Dependency Manager

In this section:

Introduction

The Composer Dependency Manager is very helpful when you need to install PHP-related frameworks, for example PHPUnit or Behat test frameworks.

You can use Composer from PhpStorm in three modes:

  • Using the dedicated PhpStorm interface for most basic operations such as initializing a project with a composer.json stub created and adding dependencies.
  • To have all Composer commands at disposal, you need to configure Composer as a command line tool and use it in the command line mode from the dedicated Command Line Tools Console Tool Window or pop-up window. For more details, see Running Command Line Tool Commands.
  • You can have PhpStorm generate a project stub from an existing package using Composer. In this case, Composer will be initialized and one package will be installed during the project stub creation. After that you can use Composer from the command line or through the user interface.

Preparing to run Composer in the command line mode

You can use the full range of Composer commands from PhpStorm only in the command line mode. To use Composer in this mode, you need to configure it as a command line tool. For more details, see Using Command Line Tools.

  1. Download the Composer.phar definition file.
  2. Make sure the Command Line Tool Support plugin is enabled. The plugin is bundled with PhpStorm and activated by default. If the plugin is not activated, enable it on the Plugins page of the Settings / Preferences Dialog as described in Enabling and Disabling Plugins.
  3. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for OS X, and click Command Line Tool Support under Tools.
  4. On the Command Line Tool Support page, click the Add button. In the Choose Tool to Add dialog box that opens, choose Composer.
  5. In the Composer dialog box that opens, choose the way to launch Composer:
    • Composer.phar or php script: Choose this option to launch Composer through a PHP script or have PhpStorm detect and start the launcher in the composer.phar archive. In this mode, PhpStorm provides coding assistance and allows to execute scripts.

      In the Path to PHP executable field, specify the location of the PHP engine installation folder. Choose the PHP installation from the list, or type the path manually, or click the Browse button browseButton.png and choose the desired location in the dialog box that opens.

      In the Path to composer.phar or composer text box, specify the location of the composer.phar archive. Type the path manually or click the Browse button browseButton.png and choose the desired location in the dialog box that opens.

    • Composer executable: choose this option to launch Composer through the Composer executable file. In this mode, you do not get coding assistance and cannot execute scripts because not PHP engine is appointed for it.

      In the Path to executable text box, specify the location of the composer executable file. Type the path manually or click the Browse button browseButton.png and choose the desired location in the dialog box that opens.

    PhpStorm parses the contents of the specified .phar archive or executable file for Composer commands. When the file analyses is completed, PhpStorm returns to the Command Line Tools page of the Settings dialog bos where the specified file is added to the list of command line tools available in PhpStorm.

    You can have several instances of Composer configured and switch between them from one project to another by specifying the relevant one during Composer initialization.

  6. In the Alias text box, specify the alias to use in calls of tool commands when running in the command line mode. Accept the default alias c or edit it, if necessary.
  7. To activate the detected command set, select the Enable check box.
  8. Customize the command set, if necessary.
  9. In the Show console in area, specify where you want the Input pane for typing commands opened:
    • To have the Input pane opened in a pop-up window, choose the Pop-up option.
    • To have the Input pane opened as a text box at the bottom of the Command Line Tools Console tool window, choose the Tool window option.

For information about running the tool in the command line mode, see Running Command Line Tool Commands.

Using Composer via the dedicated user interface

PhpStorm provides a dedicated interface for such basic Composer operations as initializing a project and adding dependencies. In the UI mode, Composer is available at the project level. For information on using the tool from the command line, see Running Command Line Tool Commands.

Setting up Composer in an existing project

The dependencies added to a project through Composer are listed in the project-specific composer.json file. Therefore setting up the tool in a project starts with creating a project-specific composer.json file. This operation is also referred to as Composer initialization.

  1. Open the project to use Composer.
  2. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for OS X. Expand the Languages&Frameworks node, and then click Composer under PHP.
  3. On the Composer page that opens, specify the following:
    1. In the Path to PHP executable field, specify the location of the PHP engine installation folder. Choose the PHP installation from the list, or type the path manually, or click the Browse button browseButton.png and choose the desired location in the dialog box that opens.
    2. In the Path to composer.phar text box, specify the location of the composer.phar archive. Type the path manually or click the Browse button browseButton.png and choose the desired location in the dialog box that opens.
    3. If you have not downloaded the composer.phar file yet, click the Download link to have PhpStorm download composer.phar from the official storage and specify the folder to store the archive in. This instance of Composer will be available in the current project only. To use it in the command line mode, configure it as a command line tool.
    4. Specify how you want PhpStorm to treat Composer packages:
      • To have PhpStorm mark Composer packages added to the current project as libraries., select the Add packages as libraries check box.

        In the context of PhpStorm, any file marked as library is automatically write-protected. Library files are indexed for the only purpose which is enhancing coding assistance (i.e. code completion, syntax highlighting, navigation, and documentation lookup). Library files are not to be edited in the scope of the project where they are declared as library.

        By default, the Add packages as libraries check box is selected, which automatically blocks the possibility to edit them in the current project.

      • If you want to use Composer packages in your development and at the same time update them yourself within the scope of the same project, clear the Add packages as libraries check box.
  4. Click OK to save the Composer settings and leave the Settings dialog box.
  5. On the main menu, choose Tools | Composer | Init Composer. PhpStorm creates a stub of the composer.json file and opens it in the editor.
  6. Complete the code or accept the default values. For more details, see Composer.json: Project Setup.

Quick Composer setup

Alternatively, you can specify the location of the composer.phar archive and the PHP engine right during the Composer initialization:

  1. On the main menu, choose Tools | Composer | Init Composer.
  2. In the Composer Settings dialog that opens, specify the location of the composer.phar archive and the PHP engine to use. If you have not downloaded the composer.phar file yet, click the Download link to have PhpStorm download it now and specify the folder to store the file in.
  3. Click OK. The dialog box is closed whereupon PhpStorm creates a stub of the composer.json file and opens it in the editor.
  4. Complete the code or accept the default values. For more details, see Composer.json: Project Setup.

Adding Composer dependencies

When you add a dependency to the project using Composer, a new subfolder is created under the Vendor folder, the added package is stored in this subfolder, and the added package is added to the list in the require section of Composer.json. For information on adding dependencies manually in the command line mode, see Running Command Line Tool Commands. Below are instructions for adding dependencies using the interface provided by PhpStorm.

  1. Open the project where Composer is set up and choose Tools | Composer | Add Dependency on the main menu.
  2. In the Add Composer Dependency dialog that opens, select the packageToAdd from the Available Packages list, possibly using the search field. The list shows all the available packages, however, the packages that have been already installed are marked with a tick.

    Choose the relevant version from the Version to install drop-down list.

  3. If necessary, expand the Settings hidden area and specify the advanced installation options. In the Command line parameters text box, type the additional command line parameters to adjust the add dependency procedure. For example, to have a dependency added to the require-dev section instead of the default require section type -dev. For more information about Composer command line options during installation, see https://getcomposer.org/doc/03-cli.md.
  4. Click Install.

When you click Create, the create-project command is invoked with the selected package. This results in creating a Composer project whose configuration and structure depends on the selected package, see https://getcomposer.org/doc/03-cli.md#create-project for details. After that a PhpStorm project opens.

Generating a Composer project stub

  1. Choose File | New Project on the main menu or click the New Project button on the Welcome screen. The New Project Dialog dialog box opens.
  2. In the left-hand pane, choose Composer Project.
  3. In the right-hand pane, in the Location field, specify the folder where the project will be created and provide the following Composer-specific information:
    1. Appoint the Composer instance to use.
      • To use commands from a previously downloaded composer.phar, choose Use existing composer.phar: and specify it location in the text box.
      • To have a new instance of Composer downloaded, choose Download composer.phar from getcomposer.org:. The composer.phar file will be saved under the project root folder specified in the Location text box of the New Project Dialog dialog.
    2. In the Package area, specify the package to install during the project creation. Select the package to add from the Available Packages list, possibly using the search field, and choose the relevant version from the Version to install drop-down list.
    3. Expand the Settings area and specify the path to the PHP interpreter to use (see Configuring Remote PHP Interpreters and Configuring Local PHP Interpreters for details). In the Command line parameters text box, type the additional command line parameters to adjust the add dependency procedure. For example, to have a package added to the require-dev section instead of the default require section type -dev. For more information about Composer command line options during installation, see https://getcomposer.org/doc/03-cli.md.

When you click Create, the create-project command is invoked with the selected package. This results in creating a Composer project whose configuration and structure depends on the selected package, see https://getcomposer.org/doc/03-cli.md#create-project for details. After that a PhpStorm project opens.

See Also

Last modified: 23 March 2017