Using Composer within PhpStorm

This post is a follow-up to Discovering Composer for PHP Dependencies. Absent a knowledge of Composer, readers are recommended to read the previous post for background before continuing with this one.

PhpStorm, made by JetBrains, is a popular editor focused on editing and tooling support for PHP projects. A feature of the version 6 release was built-in support for Composer within the IDE, without requiring a separate command line window.

This post will demonstrate how to add Composer support to an existing project, then how to select dependencies for consumption.

Leveraging Composer in PhpStorm

PhpStorm makes starting a project with Composer dead easy. In fact, there’s a Composer Project option:

PhpStorm creating new project
PhpStorm creating new project

For this introduction, a blank project was created to show how Composer support can be added to an existing project. The Composer Project is a good option for new projects.

With the blank project created, navigate the main menu to Tools > Composer > Init Composer.

PhpStorm initialize Composer
PhpStorm initialize Composer

A small dialog comes up. It prompts for the location of the PHP executable and a local composer.phar.

Composer settings in PhpStorm
Composer settings in PhpStorm

Clicking the link allows downloading of composer.phar to the project directory. Strange that PhpStorm couldn’t use the already-installed Composer.

PhpStorm downloading Composer
PhpStorm downloading Composer

When the Composer Settings dialog is completed, a basic composer.json file is generated and opened:

Initial Composer file in PhpStorm
Initial Composer file in PhpStorm

The fields can be tabbed through and filled out one by one. They needn’t be filled out now though. Note that the initial fields provide enough structure to define a consumable package as described previously. Also note the absence of a require block – no dependencies have been declared yet. That’s next up.

Dependencies of course can be added manually into the composer.json file, but PhpStorm provides a facility to manage dependencies, and will update composer.json accordingly. To access the tool, one can use the main menu to navigate to Tools > Composer > Add dependency. Alternately, one can right-click any item in the project outline pane and choose Composer > Add dependency, as shown below.

PhpStorm using Composer to add a dependency
PhpStorm using Composer to add a dependency

A dialog pops up with the ability to search packages on Packagist. When a selection is made on the left, the area on the right shows the package description. The adjacent dropdown allows choosing which version to install. The collapsible settings section contains the path to the PHP executable (which was set earlier) and a field for command-line flags.

Composer dependency selector
Composer dependency selector

Clicking Install will cause PhpStorm to do its work, as shown below. It uses the local composer.phar to do the work.

Composer in PhpStorm adding a dependency
Composer in PhpStorm adding a dependency

Once the process completes, the project outline pane is updated, shown below. Note the addition of the vendor folder, and the yiisoft/yii subfolders. Also notice the updated composer.json file. Lastly, Composer generated the composer.lock file. Dependency versions can be updated through this dialog also, by changing the selected version in the dropdown.

The Composer file in PhpStorm after a dependency is added
The Composer file in PhpStorm after a dependency is added

The primary dependency is in place. There’s still a need for some supporting tools, including but not limited to:

There are other packages that could be useful, but for now these will do. A return to the dependency manager dialog makes adding these easy. Only one dependency can be added at a time, but the same dialog can be used to add another dependency once the previous has been installed – no need to close and reopen the dialog. The resulting composer.json looks like the following:

{
    "name": "grantpalin/composertest",
    "description": "A test implementation of Composer",
    "minimum-stability": "dev",
    "license": "proprietary",
    "authors": [
        {
            "name": "Grant Palin",
            "email": "email@example.com"
        }
    ],
    "require": {
        "yiisoft/yii": "1.1.16",
        "squizlabs/php_codesniffer": "2.1.0",
        "phpunit/phpunit": "4.4.1",
        "phpdocumentor/phpdocumentor": "2.8.1"
    }
}

Conclusion

Composer is turning out to be a useful tool for managing PHP-based project dependencies. PhpStorm having built-in support for interacting with it both makes Composer more accessible and adds yet more utility to an already excellent editor.