2018-07-17 20:08:34 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Fork;
|
2018-07-17 20:08:34 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Composer\XdebugHandler\XdebugHandler;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_filter;
|
|
|
|
use function extension_loaded;
|
|
|
|
use function file_get_contents;
|
|
|
|
use function file_put_contents;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function implode;
|
|
|
|
use function preg_replace;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2021-12-03 20:11:20 +01:00
|
|
|
class PsalmRestarter extends XdebugHandler
|
2018-07-17 20:08:34 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $required = false;
|
|
|
|
|
|
|
|
/**
|
2018-07-17 22:40:03 +02:00
|
|
|
* @var string[]
|
2018-07-17 20:08:34 +02:00
|
|
|
*/
|
2018-07-17 22:40:03 +02:00
|
|
|
private $disabledExtensions = [];
|
2018-07-17 20:08:34 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function disableExtension(string $disabledExtension): void
|
2018-07-17 20:08:34 +02:00
|
|
|
{
|
2018-07-17 22:40:03 +02:00
|
|
|
$this->disabledExtensions[] = $disabledExtension;
|
2018-07-17 20:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-26 12:52:01 +01:00
|
|
|
* No type hint to allow xdebug-handler v1 and v2 usage
|
2021-05-09 01:33:48 +02:00
|
|
|
* @param bool $default
|
2018-07-17 20:08:34 +02:00
|
|
|
*/
|
2021-05-09 01:33:48 +02:00
|
|
|
protected function requiresRestart($default): bool
|
2018-07-17 20:08:34 +02:00
|
|
|
{
|
2018-07-17 22:40:03 +02:00
|
|
|
$this->required = (bool) array_filter(
|
|
|
|
$this->disabledExtensions,
|
2022-04-09 11:58:26 +02:00
|
|
|
static fn(string $extension): bool => extension_loaded($extension)
|
2018-07-17 22:40:03 +02:00
|
|
|
);
|
2018-07-17 20:08:34 +02:00
|
|
|
|
2021-05-09 01:33:48 +02:00
|
|
|
return $default || $this->required;
|
2018-07-17 20:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-26 12:52:01 +01:00
|
|
|
* No type hint to allow xdebug-handler v1 and v2 usage
|
|
|
|
* @param string|string[] $command
|
2018-07-17 20:08:34 +02:00
|
|
|
*/
|
2020-10-12 21:02:52 +02:00
|
|
|
protected function restart($command): void
|
2018-07-17 20:08:34 +02:00
|
|
|
{
|
|
|
|
if ($this->required && $this->tmpIni) {
|
2018-07-17 22:40:03 +02:00
|
|
|
$regex = '/^\s*(extension\s*=.*(' . implode('|', $this->disabledExtensions) . ').*)$/mi';
|
2018-07-17 20:08:34 +02:00
|
|
|
$content = file_get_contents($this->tmpIni);
|
|
|
|
|
|
|
|
$content = preg_replace($regex, ';$1', $content);
|
2018-07-17 22:40:03 +02:00
|
|
|
|
2018-07-17 20:08:34 +02:00
|
|
|
file_put_contents($this->tmpIni, $content);
|
|
|
|
}
|
|
|
|
|
2021-12-26 22:05:02 +01:00
|
|
|
/** @psalm-suppress PossiblyInvalidArgument */
|
2018-07-17 20:08:34 +02:00
|
|
|
parent::restart($command);
|
|
|
|
}
|
|
|
|
}
|