1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 23:18:40 +01:00
psalm/src/Psalm/Internal/Fork/PsalmRestarter.php

92 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Fork;
2021-12-03 20:11:20 +01:00
use Composer\XdebugHandler\XdebugHandler;
use function array_filter;
2023-02-07 18:13:29 +01:00
use function array_splice;
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;
2023-02-07 18:13:29 +01:00
use function in_array;
use function ini_get;
2019-07-05 22:24:00 +02:00
use function preg_replace;
/**
* @internal
*/
2021-12-03 20:11:20 +01:00
class PsalmRestarter extends XdebugHandler
{
2022-12-11 23:26:05 +01:00
private bool $required = false;
/**
* @var string[]
*/
2022-12-11 23:26:05 +01:00
private array $disabledExtensions = [];
public function disableExtension(string $disabledExtension): void
{
$this->disabledExtensions[] = $disabledExtension;
}
/**
2021-12-26 12:52:01 +01:00
* No type hint to allow xdebug-handler v1 and v2 usage
*
* @param bool $default
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
protected function requiresRestart($default): bool
{
$this->required = (bool) array_filter(
$this->disabledExtensions,
static fn(string $extension): bool => extension_loaded($extension)
);
2023-02-07 18:13:29 +01:00
if (!extension_loaded('opcache')) {
return true;
}
if (!in_array(ini_get('opcache.enable_cli'), ['1', 'true', true, 1])) {
return true;
}
if (((int) ini_get('opcache.jit')) !== 1205) {
return true;
}
if (((int) ini_get('opcache.jit')) === 0) {
return true;
}
return $default || $this->required;
}
/**
2021-12-26 12:52:01 +01:00
* No type hint to allow xdebug-handler v1 and v2 usage
*
2023-02-07 18:13:29 +01:00
* @param string[] $command
*/
2023-02-07 18:13:29 +01:00
protected function restart(array $command): void
{
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);
}
2023-02-07 18:13:29 +01:00
array_splice(
$command,
1,
0,
[
'-dzend_extension=opcache',
'-dopcache.enable_cli=true',
'-dopcache.jit_buffer_size=512M',
'-dopcache.jit=1205',
],
);
parent::restart($command);
}
}