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;
|
2023-02-07 18:13:29 +01:00
|
|
|
use function array_splice;
|
2019-06-26 22:52:29 +02:00
|
|
|
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;
|
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
|
|
|
{
|
2022-12-11 23:26:05 +01:00
|
|
|
private bool $required = false;
|
2018-07-17 20:08:34 +02:00
|
|
|
|
|
|
|
/**
|
2018-07-17 22:40:03 +02:00
|
|
|
* @var string[]
|
2018-07-17 20:08:34 +02:00
|
|
|
*/
|
2022-12-11 23:26:05 +01:00
|
|
|
private array $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
|
2022-12-14 20:34:41 +01:00
|
|
|
*
|
2021-05-09 01:33:48 +02:00
|
|
|
* @param bool $default
|
2022-12-15 03:26:17 +01:00
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
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
|
|
|
);
|
2023-02-11 11:42:48 +01:00
|
|
|
|
2023-02-07 18:13:29 +01:00
|
|
|
if (!in_array(ini_get('opcache.enable_cli'), ['1', 'true', true, 1])) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-11 11:42:48 +01:00
|
|
|
|
2023-02-07 18:13:29 +01:00
|
|
|
if (((int) ini_get('opcache.jit')) !== 1205) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-11 11:42:48 +01:00
|
|
|
|
2023-02-07 18:13:29 +01:00
|
|
|
if (((int) ini_get('opcache.jit')) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
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
|
2022-12-14 20:34:41 +01:00
|
|
|
*
|
2023-02-07 18:13:29 +01:00
|
|
|
* @param string[] $command
|
2018-07-17 20:08:34 +02:00
|
|
|
*/
|
2023-02-07 18:13:29 +01:00
|
|
|
protected function restart(array $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);
|
|
|
|
}
|
2023-02-11 11:42:48 +01:00
|
|
|
|
|
|
|
$additional_options = [];
|
|
|
|
|
|
|
|
// executed in the parent process (before restart)
|
|
|
|
// if it wasn't loaded then we apparently don't have opcache installed and there's no point trying
|
|
|
|
// to tweak it
|
|
|
|
if (extension_loaded('opcache') || extension_loaded('Zend OPcache')) {
|
|
|
|
$additional_options = [
|
|
|
|
'-dopcache.enable_cli=true',
|
|
|
|
'-dopcache.jit_buffer_size=512M',
|
|
|
|
'-dopcache.jit=1205',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:13:29 +01:00
|
|
|
array_splice(
|
|
|
|
$command,
|
|
|
|
1,
|
|
|
|
0,
|
2023-02-11 11:42:48 +01:00
|
|
|
$additional_options,
|
2023-02-07 18:13:29 +01:00
|
|
|
);
|
2018-07-17 20:08:34 +02:00
|
|
|
|
|
|
|
parent::restart($command);
|
|
|
|
}
|
|
|
|
}
|