Add xdebug and allow for disabling xdebug

This commit is contained in:
Daniel Winning 2025-04-13 23:33:55 +01:00
parent 06ba3f7ae0
commit 2246c597f1
7 changed files with 84 additions and 7 deletions

View file

@ -63,4 +63,38 @@ Creates a new PHP development environment and mounts your project files.
> **Required?** > **Required?**
> >
> Does not install a webserver (so no Nginx). Useful if you just need a PHP container to run > Does not install a webserver (so no Nginx). Useful if you just need a PHP container to run
> unit tests or something. > unit tests or something.
> #### Option: --disable-xdebug
>
> **Required?**
>
> Do not install XDebug in your environment.
## XDebug Setup
These instructions are for PHPStorm. I don't know about working with other IDE's, although they
should be fairly universal.
### Server Settings
- `File` -> `Settings` (`Preferences` on MacOS)
- `PHP` -> `Servers` -> `+`
- Give your "server" a name and use the values shown below.
| Setting | Value |
|-------------------------|----------------------------------------------------------------------------------------------------------------|
| Host | 127.0.0.1 |
| Port | **Local** port **PHP** container is running on. i.e. if your docker container shows 52033:9003, use **52033**. |
| Debugger | Xdebug |
| Use path mappings? | ✅ |
| File/Directory | Select **your project root** |
| Absolute path on server | /var/www/html |
### Remote Debugger
- `Run` -> `Edit Configurations`
- `+` -> `PHP Remote Debug`
- Give it a name
- Use the server you created before
- Use **SPINNER** as IDE key

View file

@ -1,6 +1,10 @@
RUN pecl install xdebug redis \ #RUN pecl install xdebug redis \
&& docker-php-ext-enable redis # && docker-php-ext-enable redis
#
#COPY ./xdebug.ini.tmp "${PHP_INI_DIR}/conf.d/xdebug.ini"
#RUN docker-php-ext-install opcache > /dev/null
#COPY ./opcache.ini "${PHP_INI_DIR}/conf.d"
COPY ./xdebug.ini.tmp "${PHP_INI_DIR}/conf.d/xdebug.ini" RUN pecl install xdebug
RUN docker-php-ext-install opcache > /dev/null
COPY ./opcache.ini "${PHP_INI_DIR}/conf.d" COPY ./xdebug.ini "${PHP_INI_DIR}/conf.d/xdebug.ini"

View file

@ -0,0 +1,8 @@
[xdebug]
zend_extension=xdebug.so
xdebug.mode=debug,develop,coverage
xdebug.start_with_request=yes
xdebug.client_host=host.docker.internal
xdebug.client_port=9003
xdebug.idekey=SPINNER
xdebug.log=/tmp/xdebug.log

View file

@ -2,6 +2,7 @@ options:
environment: environment:
php: php:
version: 8.4 version: 8.4
xdebug: true
node: node:
enabled: true enabled: true
version: 23 version: 23

View file

@ -71,6 +71,18 @@ class Config
return $this->getEnvironmentOption('server', 'enabled'); return $this->getEnvironmentOption('server', 'enabled');
} }
/**
* @throws \Exception
*/
public function isXDebugEnabled(InputInterface $input): bool
{
if ($input->getOption('disable-xdebug')) {
return false;
}
return $this->getEnvironmentOption('php', 'xdebug');
}
/** /**
* @throws \Exception * @throws \Exception
*/ */
@ -126,6 +138,8 @@ class Config
DataPathInterface::CONFIG_PHP_FPM_DOCKERFILE => new SpinnerFilePath(DataPathInterface::CONFIG_PHP_FPM_DOCKERFILE), DataPathInterface::CONFIG_PHP_FPM_DOCKERFILE => new SpinnerFilePath(DataPathInterface::CONFIG_PHP_FPM_DOCKERFILE),
DataPathInterface::CONFIG_NGINX_DOCKERFILE => new SpinnerFilePath(DataPathInterface::CONFIG_NGINX_DOCKERFILE), DataPathInterface::CONFIG_NGINX_DOCKERFILE => new SpinnerFilePath(DataPathInterface::CONFIG_NGINX_DOCKERFILE),
'nodeDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/Node.Dockerfile'), 'nodeDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/Node.Dockerfile'),
'xdebugIniTemplate' => new SpinnerFilePath('config/php-fpm/xdebug.ini'),
'xdebugDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/XDebug.Dockerfile'),
]); ]);
} }
} }

View file

@ -34,10 +34,20 @@ class PHPDockerFileBuilder extends AbstractFileBuilder
$this->content = str_replace('${PHP_VERSION}', (string) $this->config->getPhpVersion($input), $this->content); $this->content = str_replace('${PHP_VERSION}', (string) $this->config->getPhpVersion($input), $this->content);
if ($this->config->isNodeEnabled($input)) { if ($this->config->isNodeEnabled($input)) {
$this->content .= "\r\n\r\n" . file_get_contents($this->config->getFilePaths()->get('nodeDockerfileTemplate')->getAbsolutePath()); $this->addNewLine();
$this->content .= file_get_contents($this->config->getFilePaths()->get('nodeDockerfileTemplate')->getAbsolutePath());
$this->content = str_replace('${NODE_VERSION}', (string) $this->config->getNodeVersion($input), $this->content); $this->content = str_replace('${NODE_VERSION}', (string) $this->config->getNodeVersion($input), $this->content);
} }
if ($this->config->isXdebugEnabled($input)) {
$this->addNewLine();
$this->content .= file_get_contents($this->config->getFilePaths()->get('xdebugDockerfileTemplate')->getAbsolutePath());
file_put_contents(
(new SpinnerFilePath(sprintf('data/environments/%s/php-fpm/xdebug.ini', $input->getArgument('name'))))->getProvidedPath(),
file_get_contents($this->config->getFilePaths()->get('xdebugIniTemplate')->getAbsolutePath())
);
}
return $this; return $this;
} }

View file

@ -59,6 +59,12 @@ class SpinCommand extends AbstractSpinnerCommand
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
'Set this flag to disable Node.js for your environment.' 'Set this flag to disable Node.js for your environment.'
) )
->addOption(
'disable-xdebug',
null,
InputOption::VALUE_NONE,
'Set this flag to disable XDebug for your environment.'
)
->addOption('node', null, InputOption::VALUE_OPTIONAL, 'The Node.js version to use (e.g. 20).'); ->addOption('node', null, InputOption::VALUE_OPTIONAL, 'The Node.js version to use (e.g. 20).');
} }