Skip to content

Commit

Permalink
introduces the new RestartHandler to solve issue explained on discuss…
Browse files Browse the repository at this point in the history
…ion #12
  • Loading branch information
llaville committed Aug 23, 2024
1 parent 27e414f commit 1362fc1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
99 changes: 99 additions & 0 deletions src/Composer/RestartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php declare(strict_types=1);
/**
* This file is part of the BoxManifest package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\BoxManifest\Composer;

use Composer\Pcre\Preg;
use Composer\XdebugHandler\Process;
use Composer\XdebugHandler\XdebugHandler;

use function count;
use function explode;
use function file_put_contents;
use function getenv;
use function in_array;
use function ini_get;
use function phpversion;
use function str_replace;
use function version_compare;
use const PHP_EOL;

/**
* Restarts with xdebug loaded in coverage mode, by uncommenting xdebug in the
* temporary ini file and setting the XDEBUG_MODE environment variable,
* and with phar.read_only=0 (disabled).
*
* Credits to @link https://github.com/composer/xdebug-handler/blob/3.0.5/tests/App/README.md
* that help to make a compatible version between
* - https://github.com/composer/xdebug-handler/blob/3.0.5/tests/App/README.md#app-extend-mode
* - https://github.com/composer/xdebug-handler/blob/3.0.5/tests/App/README.md#app-extend-ini
*
* @since Release 4.0.0
*/
class RestartHandler extends XdebugHandler
{
/** @noinspection PhpVoidFunctionResultUsedInspection */
protected function requiresRestart(bool $default): bool
{
if ($default) {
$version = (string) phpversion('xdebug');

if (version_compare($version, '3.1', '>=')) {
/**
* @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);
}
}
11 changes: 9 additions & 2 deletions src/Helper/BoxHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit 1362fc1

Please sign in to comment.