1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/src/Psalm/Internal/Fork/PsalmRestarter.php

76 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Fork;
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;
/**
* @internal
*/
class PsalmRestarter extends \Composer\XdebugHandler\XdebugHandler
{
/**
* @var bool
*/
private $required = false;
/**
* @var string[]
*/
private $disabledExtensions = [];
/**
* @param string $disabledExtension
*
* @return void
*/
public function disableExtension($disabledExtension)
{
$this->disabledExtensions[] = $disabledExtension;
}
/**
* @psalm-suppress UnusedMethod
*/
protected function requiresRestart($loaded)
{
$this->required = (bool) array_filter(
$this->disabledExtensions,
/**
* @param string $extension
*
* @return bool
*/
function ($extension) {
return extension_loaded($extension);
}
);
return $loaded || $this->required;
}
/**
* @psalm-suppress UnusedMethod
* @return void
*/
protected function restart($command)
{
if ($this->required && $this->tmpIni) {
$regex = '/^\s*(extension\s*=.*(' . implode('|', $this->disabledExtensions) . ').*)$/mi';
$content = file_get_contents($this->tmpIni);
$content = preg_replace($regex, ';$1', $content);
file_put_contents($this->tmpIni, $content);
}
/** @psalm-suppress MixedArgument */
parent::restart($command);
}
}