1/27/2022»»Thursday

Phpstorm Composer

1/27/2022

Created at 2020-10-10Updated at 2020-10-11Category Docker Tag Resource / Docker / PHP / PhpStorm / PhpUnit / Linux

  1. Phpstorm Composer
  2. Phpstorm Composer
  3. Jetbrains Phpstorm
  4. Phpstorm Composer Crack
  5. Phpstorm Composer Download

I have recently configured my windows 10 laptop with an additional SSD, so I could experiment with Linux. I have already installed Pop!_OS Git, PhpStorm and Docker. I haven’t installed PHP or Composer locally. Next I want to learn how to use this new environment. This is what I have found out so far.

Alternatively, more complex projects will need specific PHP extensions to be installed, which are not included in the Composer Docker container. The following method could be used to install Composer, inside the container and install the dependencies. Access bash in the php-cli container: docker-compose run -rm php-cli /bin/bash. JetBrains PhpStorm is a commercial PHP IDE that you can configure to work with your Pantheon sites. This guide explains how to use the Composer integration of JetBrains PhpStorm to install Drupal 8. I have a problem with PhpStorm IDE. I am trying to setup a composer project. Here are listed things I made step-by-step: Installed preferred version of PHP in D:LIBRARIESphp74 Chosen php.ini.

Start with a Project

One of my favorite projects is the Gilded Rose Kata. I can clone that from github as follows:

Create docker-compose.yml

It is possible to run PHP cli without a docker-compose file, I have found it is easier to set up PhpStorm using this intermediate step.

PhpStorm has several preconfigured Docker containers, source:

They can be used as follows:

Php 7.3 CLI and XDebug 2.7

docker-compose.yml

Phpstorm Composer

The above will work for Linux, for Windows and MacOS the XDEBUG_CONFIG: will need the changed as follows:

Windows and MacOS

Windows and MacOS replace with XDEBUG_CONFIG:host.docker.internal, which will automatically resolve to the internal address of the host Docker is running on.

MacOS with local Homebrew php-fpm

If you use a local Homebrew php-fpm installation, port 9000 (which is the default debugging port) may become occupied. PhpStorm will remain silent on starting listening for incoming connections. If this is the case, in the Settings Languages & Frameworks PHP Debug, set the Debug port to 9001, and use the following configuration line instead.

Apache, PHP 7.3, XDebug 2.7 and MySQL

For information this is the LAMP version (based on the phpstorm-workshop).

docker-compose.yml

Install dependencies using composer

To keep things simple the composer Docker container can be used to install the dependencies.

This is the script recommended on the docker hub composer page to avoid filesystem permissions problems

By default, Composer runs as root inside the container. This can lead to permission issues on your host filesystem. You can work around this by running the container with a different user:

On windows change $PWD for the full path to the project (note: forward slash / as separator), remove the line end and run the command as one line:

Alternatively, more complex projects will need specific PHP extensions to be installed, which are not included in the Composer Docker container. The following method could be used to install Composer, inside the container and install the dependencies.

  1. Access bash in the php-cli container: docker-compose run --rm php-cli /bin/bash
  2. Install Composer, by following the download instructions for Linux
  3. Still, inside the container, install dependencies: php composer.phar install
  4. Exit the container exit

Note: In Linux, using the second method Composer will create the vendor folder as root!

The permissions can be changed using chown:

Further information

There is a detailed description about running Docker containers as current host user.

The official documentation on Docker run and docker-compose cli reference.

Configure PhpStorm

Now the project has been cloned from GitHub and the dependencies have been installed. PhpStorm can be setup to use Docker. Thanks to Gary Hockin’s excellent YouTube video Running PHPUnit Tests in PhpStorm with Docker, the setup process can be easily replicated.

There is a four stage process:

  1. Configure PhpStorm to use Docker
  2. Configure the remote interpreter
  3. Configure PhpUnit
  4. Create Test runner
Phpstorm composer

1. Configure PhpStorm to use Docker

  • Settings (Ctrl + Alt + S)
  • Search for Docker
    • Under Build, Execution, Deployment
  • Click + to add
  • Select Unix socket
    • Confirm connection was successful

2. Configure the default CLI interpreter

Phpstorm Composer

  • Settings (Ctrl + Alt + S)
  • Search for CLI interpreter
    • Under Language & Frameworks > PHP
  • Click the ellipse button next to CLI Interpreter
  • Click +
  • Select From Docker, Vagrant…
  • Choose Docker Compose
  • Choose the Service from the drop down list (e.g. php-cli)
  • Select OK
  • Change the name e.g. Docker PHP
  • Apply and OK
  • Check the mapping
    • e.g. for a web project <Project root>→/var/www/html
    • e.g. for an app project <Project root>→/app

3. Configure PhpUnit

  • Settings (Ctrl + Alt + S)
  • Search for Test Frameworks
    • Under Language & Frameworks > PHP
  • Click +
  • Select PhpUnit from remote interpreter
  • Choose the interpreter created above, e.g. Docker PHP
    • Confirm the path mappings, as above <Project root>→/app
    • Input the script path based on the mapping inside the container e.g. /app/vendor/autoload.php
    • Under Test runner, tick Default configuration script, type in the path, in the docker container. e.g. /app/phpunit.xml

4. Create the test runner

  • Click Edit Configuration (next to run test button)
  • Click + to add
  • Select PHPUnit
  • Under Test Runner choose Defined in the configuration file
  • Name - e.g. Docker PHPUnit
  • Click Play to run all the tests!

What about configuring xDebug?

Thanks to this setup, xDebug has been automatically configured! It will use the default PHP Interpreter, which was configured in step 2. A breakpoint can be set in the app or tests can be run with coverage :)

Enjoy the kata!

Edit: Added details on running commands on MacOS and Windows and small tweaks.

One project I’m working on has multiple separate parts in different git repositories that are brought into the main project using linked composer directories. I needed to get step debugging working in PhpStorm and this is the approach I took.

Directory layout

My project is laid out on disk like this:

Phpstorm
2
4
6
8
{
'url':'../plugin-one'
'symlink':true
},

Jetbrains Phpstorm

I can then set my require like this:

2
'client/plugin-one':'@dev',

The net result is that ./main-app/vendor/client/plugin-one is a symlink to ./plugin-one

Setting up PhpStorm

We create a PhpStorm project for ./main-app and then add plug-one to it as a “content root”:

Phpstorm Composer Crack

  • Go to Preferences -> Directories
  • Click “+ Add Content Root” add add {full path}/plugin-one
  • Click “Apply” and then “OK” to close the Preferences dialog

We now have the same source code in the project twice (once in ./plugin-one and once in ./main-app/vendor/client/plugin-one), so we need to excluded the vendor directory:

Phpstorm Composer Download

  • In the Project tree view, find the main-app/vendor/client/plugin-one folder and select it
  • Right click -> Mark Directory As -> Excluded

The folder will now be coloured orange, so we know it’s excluded.

Set-up for debugging

PhpStorm now knows about our source code, so next we need to map it to the server. I’m using Docker for this project, but it’s the for any remote-like project.

  • Go to Preferences -> Languages & Frameworks -> PHP -> Servers
  • Add a new entry:
    • Name: {dev-hostname-for-main-app}
    • Host: {dev-hostname-for-main-app}
    • Port: 8888 (or whatever)
    • Debugger: Xdebug (of course!)
    • [✓] Use path mappings
    • In the File/Directory list set up these mappings:
      • {full path on local disk}/main-app => /var/www/html/main-app
      • {full path on local disk}/plugin-one => /var/www/html/plugin-one

The key bit for this article is that path mapping section. The '/var/www/html' is whatever the correct path is on your Docker/Vagrant/whatever host.

Finally, click the menu item Run -> Start Listening for PHP Debug Connections. Note that if this is already selected, then the menu item does not exist and the menu item Stop Listening for PHP Debug Connections is in its place.

Start debugging

You can now set breakpoints in plugin-one/src/whatever.php and PhpStorm will stop on them.

If you are debugging a website using a browser then get the relevant Xdebug extension for Chrome or Firefox.

If it’s an API, then you’ll need to set a cookie to enable Xdebug for the request: