vscode-laravel-pint icon indicating copy to clipboard operation
vscode-laravel-pint copied to clipboard

Run Laravel Pint inside docker container

Open yungts97 opened this issue 1 year ago • 1 comments

I'm using WSL2 and not PHP installed in my WSL2. And I run my Laravel project with a custom PHP docker container. I'm not using Laravel Sail for that. Is there any way to make Laravel Pint run it on my PHP docker container?

yungts97 avatar Dec 01 '23 06:12 yungts97

This will be difficult as is already a challenge having a Laravel Sail setup (see #34)

Only difficult part here is determining in which container the code and PHP with the dependencies is

d8vjork avatar Dec 26 '23 17:12 d8vjork

https://github.com/m1guelpf/better-pest handles this through configuration.

{
  "better-pest.docker.enable": true,
  "better-pest.docker.command": "command to run", 
  "better-pest.docker.paths": {
    "${workspaceFolder}": "/var/www/html",
    "/some/other/path": "/whatever"
  },
}

The command can be:

  • docker exec some-container
  • docker compose exec some-container-within-compose
  • spin run php
  • relevant Sail command
  • any CLI wrapping Docker (like Sail or Spin)
  • etc.

The paths get used to transform paths from the host to the Docker container. With the assumption of the Docker container having configured a working directory and having 1-to-1 mapping with the host files, this config can be avoided. But, to cover all cases, it's better to have that configuration.

So, the final command becomes something like this:

$ docker exec some-container vendor/bin/pest
# or, for specific files:
$ docker exec some-container vendor/bin/pest tests/Unit/ExampleTest.php

This solution also eliminates the need for having special Laravel Sail logic.

yusufkandemir avatar Oct 26 '24 11:10 yusufkandemir