From 1362fc1a87f8ea8c824e426378a5d2221c89f00d Mon Sep 17 00:00:00 2001 From: Laurent Laville Date: Fri, 23 Aug 2024 13:08:21 +0000 Subject: [PATCH] introduces the new RestartHandler to solve issue explained on discussion https://github.com/llaville/box-manifest/discussions/12 --- src/Composer/RestartHandler.php | 99 +++++++++++++++++++++++++++++++++ src/Helper/BoxHelper.php | 11 +++- 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/Composer/RestartHandler.php diff --git a/src/Composer/RestartHandler.php b/src/Composer/RestartHandler.php new file mode 100644 index 0000000..369586f --- /dev/null +++ b/src/Composer/RestartHandler.php @@ -0,0 +1,99 @@ +=')) { + /** + * @var string[] $modes + * @link https://xdebug.org/docs/code_coverage#xdebug_info + */ + $modes = xdebug_info('mode'); + return !in_array('coverage', $modes, true); + } + + // See if xdebug.mode is supported in this version + $iniMode = ini_get('xdebug.mode'); + if ($iniMode === false) { + return false; + } + + // Environment value wins but cannot be empty + $envMode = (string) getenv('XDEBUG_MODE'); + if ($envMode !== '') { + $mode = $envMode; + } else { + $mode = $iniMode !== '' ? $iniMode : 'off'; + } + + // An empty comma-separated list is treated as mode 'off' + if (Preg::isMatch('/^,+$/', str_replace(' ', '', $mode))) { + $mode = 'off'; + } + + $modes = explode(',', str_replace(' ', '', $mode)); + return !in_array('coverage', $modes, true); + } + + return false; + } + + protected function restart(array $command): void + { + // uncomment last xdebug line + $regex = '/^;\s*(zend_extension\s*=.*xdebug.*)$/mi'; + $content = (string) file_get_contents((string) $this->tmpIni); + + if (Preg::isMatchAll($regex, $content, $matches)) { + $index = count($matches[1]) - 1; + $line = $matches[1][$index]; + $content .= $line . PHP_EOL; + } + + $content .= 'phar.readonly = 0' . PHP_EOL; + + file_put_contents((string) $this->tmpIni, $content); + Process::setEnv('XDEBUG_MODE', 'coverage'); + + parent::restart($command); + } +} diff --git a/src/Helper/BoxHelper.php b/src/Helper/BoxHelper.php index 617083f..7827955 100644 --- a/src/Helper/BoxHelper.php +++ b/src/Helper/BoxHelper.php @@ -7,13 +7,15 @@ */ namespace Bartlett\BoxManifest\Helper; +use Bartlett\BoxManifest\Composer\RestartHandler; + use Fidry\Console\IO; use KevinGH\Box\Configuration\Configuration; use KevinGH\Box\Console\Command\ConfigOption; -use KevinGH\Box\Console\Php\PhpSettingsHandler; use function KevinGH\Box\get_box_version; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Logger\ConsoleLogger; @@ -38,7 +40,12 @@ public function getBoxVersion(): string public function checkPhpSettings(IO $io): void { - (new PhpSettingsHandler(new ConsoleLogger($io->getOutput())))->check(); + $out = $io->getOutput(); + $out->setVerbosity(OutputInterface::VERBOSITY_DEBUG); + + $restart = (new RestartHandler('box')); + $restart->setLogger(new ConsoleLogger($out)); + $restart->check(); } /**