2018-07-17 20:08:34 +02:00
|
|
|
<?php
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Fork;
|
2018-07-17 20:08:34 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_filter;
|
|
|
|
use function extension_loaded;
|
|
|
|
use function implode;
|
|
|
|
use function file_get_contents;
|
|
|
|
use function preg_replace;
|
|
|
|
use function file_put_contents;
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-07-17 20:08:34 +02:00
|
|
|
class PsalmRestarter extends \Composer\XdebugHandler\XdebugHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2018-07-17 22:40:03 +02:00
|
|
|
* @param string $disabledExtension
|
|
|
|
*
|
2018-07-17 20:08:34 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-07-17 22:40:03 +02:00
|
|
|
public function disableExtension($disabledExtension)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress UnusedMethod
|
|
|
|
*/
|
|
|
|
protected function requiresRestart($loaded)
|
|
|
|
{
|
2018-07-17 22:40:03 +02:00
|
|
|
$this->required = (bool) array_filter(
|
|
|
|
$this->disabledExtensions,
|
|
|
|
/**
|
|
|
|
* @param string $extension
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function ($extension) {
|
|
|
|
return extension_loaded($extension);
|
|
|
|
}
|
|
|
|
);
|
2018-07-17 20:08:34 +02:00
|
|
|
|
|
|
|
return $loaded || $this->required;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress UnusedMethod
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function restart($command)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress MixedArgument */
|
|
|
|
parent::restart($command);
|
|
|
|
}
|
|
|
|
}
|