Php Composer

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

Php laravel docker composer-php docker-compose. Follow asked Jul 20 '18 at 13:24. 2,298 2 2 gold badges 13 13 silver badges 26 26 bronze badges. Today we have successfully implemented composer in our project, used composer to download a package and imported and the downloaded package in our “index.php” file. Finally, in the “composer.json” file we can also specify our own autoload information to guide the composer when our project is being used as a package in another. Composer actions for it are invoked from the Tools Composer main menu item. In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP Composer. Specify the location of the default composer.json. If you have a single Composer configuration file in your PhpStorm project, the Path to composer.json field is filled automatically. Composer is a dependency manager written in and for PHP.

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 the References node of your PHP project in the Solution Explorer, and select Install New Composer Packages...

Before opening the dialog packages, information has 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 the composer log in Output pane.

Once packages information is loaded, you can search for the package you are interested in and install it. For example, you can install phpunit as shown in 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.

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 installation, you can click on the link which will open the Composer Output Window.

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 the 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 the composer.lock file manually as well, this depends on the type of your project. Learn more about composer.lock

PhpPhp

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

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

Managing Installed Packages in Solution Explorer

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

This particular view corresponds to following composer.json file:

There are a few package status icons in the Solution explorer:

  • Installed and listed in the composer.json
  • Extraneous: Installed, but not listed in the composer.json
  • Missing: Listed in the composer.json, but not installed. PHP Tools also indicates when some of the required dependency packages are missing
  • Replaced: Listed in the composer.json, but replaced by another package. The package itself is not present, but its functionality is replaced by another package. More about this feature at composer documentation

Dependency type: All the required packages appear directly under the References node. Development packages (defined by require-dev in composer.json) appear 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 the 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).

You can inspect the properties of each package in the Properties pane

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 the selected the package(s)
  • Uninstall composer package(s): Uninstall the selected the package(s)

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 a working directory set to your project root directory. Then you can run composer commands the old-fashioned 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 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 drop-down, select Composer.

  3. You should see something similar to the following:

This is very useful to debug any composer related issues.

Composer package manager

Composer in MSBuild

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

How To Run Composer

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

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

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

Php Composer

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

If you are interested, you can find Composer the target file on this location $(MSBuildExtensionsPath32)DEVSENSEPHP ToolsVS$(VisualStudioVersion)Devsense.Composer.targets

Example

Composer is a dependency/package manager for PHP. It can be used to install, keep track of, and update your project dependencies. Composer also takes care of autoloading the dependencies that your application relies on, letting you easily use the dependency inside your project without worrying about including them at the top of any given file.

Dependencies for your project are listed within a composer.json file which is typically located in your project root. This file holds information about the required versions of packages for production and also development.

A full outline of the composer.json schema can be found on the Composer Website.

This file can be edited manually using any text-editor or automatically through the command line via commands such as composer require <package> or composer require-dev <package>.

To start using composer in your project, you will need to create the composer.json file. You can either create it manually or simply run composer init. After you run composer init in your terminal, it will ask you for some basic information about your project: Package name (vendor/package - e.g. laravel/laravel), Description - optional, Author and some other information like Minimum Stability, License and Required Packages.

The require key in your composer.json file specifies Composer which packages your project depends on. require takes an object that maps package names (e.g. monolog/monolog) to version constraints (e.g. 1.0.*).

Npm

To install the defined dependencies, you will need to run the composer install command and it will then find the defined packages that matches the supplied version constraint and download it into the vendor directory. It's a convention to put third party code into a directory named vendor.

Php Composer Install

You will notice the install command also created a composer.lock file.

A composer.lock file is automatically generated by Composer. This file is used to track the currently installed versions and state of your dependencies. Running composer install will install packages to exactly the state stored in the lock file.

Composer Require


Composer Install Windows