Composer

PHP Tools for Visual Studio has extensive package ecosystem support through integration with Composer. This allows you to install and manage packages for use in your PHP project. To get yourself familiar with basic composer functionality please check getcomposer.org to learn more. This text will assume you are familiar with it.

There are several ways you can work with Composer in PHP Tools for Visual Studio:

  1. Composer install package GUI - for browsing/installing packages from the packagist.org repository
  2. Solution Explorer - for viewing/managing installed packages
  3. Composer in the Command Prompt - for running composer the old fashioned way (in cmd.exe)
  4. Composer log in Output pane - for viewing the output of composer commands in your Visual Studio session
  5. Composer in MSBuild - for hooking composer into msbuild process

Browsing/Installing new composer packages in the GUI

PHP Tools for Visual Studio enables browsing in the package repository packagist.org directly within IDE. Right click on References node of your PHP project in the Solution Explorer, and select Install New Composer Packages...

Install New composer packages...

Before opening the dialog packages information have to be loaded from packagist.org , which is usually very fast. If your connection is very slow you can close the dialog and continue your work, the process will continue on the background. You can see the progress in composer log in Output pane.

Composer install gui loading

Once packages information are loaded you can search for the package you are interested in and install it. For example you can install phpunit as shown on the picture bellow. Select dependency type development (it will add the package to composer.json under require-dev) and version default, which will install the latest release.

Composer install package dialog

The command status indicator is located in the lower left hand corner. You can immediately see if something went wrong with the executed command. Or if you wish to see the details about the install, you can click on the link which will open Composer output Window.

Composer install command output

Composer will continue running in the background when you close the dialog (composer output will be printed to the composer log in the Output Window.) This applies to all composer commands executed within our IDE.

Note: If you didn't have composer.json file included in your project before this command will add it. Same applies to all the other composer commands. Please note that you might want to include composer.lock file manually as well, this depends on the type of your project. [Learn more about composer.lock](https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file )

Known Issue: Composer is using ZipArchive extension which has known issue when path to the package is longer than 256 characters, package won't install and outputs following message:

[ErrorException]
ZipArchive::extractTo(LONG_PATH_TO_FILE): failed to open stream: No such file or directory

Workaround is to install the package in the project with shorter path. Then you'll be able to install it also in this project as package will be installed from Composer cache.

Managing Installed Packages in Solution Explorer

You can view and manage composer packages under References node in the Solution Explorer. This reflects content of project's composer.json file and actuall install status of the packages.

Composer solution explorer

This particular view corresponds to following composer.json file:

{
    "require-dev": {
        "phpunit/phpunit": "~4.5"
    },
    "require": {
        "nette/mail": "~2.3",
        "lusitanian/oauth": "0.3.*"
    }
}

There are few package status icons in the Solution explorer:

  • Package Installed Icon Installed and listed in the composer.json
  • Package Extraneous Icon Extraneous: Installed, but not listed in the composer.json
  • Package Missing Icon Missing: Listed in the composer.json, but not installed. PHP Tools also indicate when some of the required dependency packages is missing
  • Package Replaced Icon Replaced: Listed in the composer.json, but replaced by another package. Package itself isn't present, but its funcionality is replaced by another package. More about this feature at composer documenation

Dependency type: All the required packages appears directly under References node. Development packages (defined by require-dev in composer.json) appears with (dev) after the name of the package.

You can expand the package node to browse its dependencies. You'll see all the required packages including missing ones and installed development packages (you won’t see those as missing if they are not installed, only the top development packages are shown as missing).

Expanded package node

You can inspect properties of the each package in Properties pane

Package properties

If you right click on a package node there are couple of options:

  • Open folder in File Explorer: Opens file explorer on the package directory
  • Update composer package(s): Updates selected the package(s)
  • Uninstall composer package(s): Uninstall selected the package(s)

    Package node commands

Composer in the command prompt

If you prefer to run commands in the command prompt yourself, you'd just need to start cmd.exe with working directory set to your project root directory. Then you can run composer commands old feshioned way assuming you have composer.phar in your project root or installed globally (absolute path to composer.phar has to be present in the PATH environmental variable).

To get yourself familiar with all the available composer commands please visit Command-line interface / Commands

All commands are immediately reflected to in the Solution Explorer

Note: Direct edit of composer.json will cause that composer.json and composer.lock will be out of sync. This means that Composer will display a warning when executing an install command and nothing gets installed. You can either delete composer.lock or call update command instead.

Composer log in Output pane

All composer commands executed in the Visual Studio outputs to the Composer Output pane.

  1. Open ViewOutput

  2. In the Show output from dropdown, select Composer.

  3. You should see something similar to the following:

    composer Log

This is very useful to debug any composer related issues.

Composer in MSBuild

Version 1.15 just supports MSBuild out-of-the-box for PHP Console and PHP Web Project for Microsoft Platforms

You can enable restore of the missing Composer packages on build by adding this element to your phpproj file:

<InstallMissingComposerPackages>true</InstallMissingComposerPackages>

There is also one DownloadComposerPhar property which is by default turned on true, indicating weather to download composer.phar file if it's missing from the project root.

You can easily test this by running Rebuild on your PHP Project.

Note PHP Console project phpproj file has to contain CoreCompile target definition.

<Target Name="CoreCompile" />

If you are interested you can find Composer target file on this location $(MSBuildExtensionsPath32)\DEVSENSE\PHP Tools\VS$(VisualStudioVersion)\Devsense.Composer.targets