| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Process; |
| 13 |
|
| 14 |
use Symfony\Component\Process\Exception\LogicException; |
| 15 |
use Symfony\Component\Process\Exception\RuntimeException; |
| 16 |
|
| 17 |
/** |
| 18 |
* PhpSubprocess runs a PHP command as a subprocess while keeping the original php.ini settings. |
| 19 |
* |
| 20 |
* For this, it generates a temporary php.ini file taking over all the current settings and disables |
| 21 |
* loading additional .ini files. Basically, your command gets prefixed using "php -n -c /tmp/temp.ini". |
| 22 |
* |
| 23 |
* Given your php.ini contains "memory_limit=-1" and you have a "MemoryTest.php" with the following content: |
| 24 |
* |
| 25 |
* <?php var_dump(ini_get('memory_limit')); |
| 26 |
* |
| 27 |
* These are the differences between the regular Process and PhpSubprocess classes: |
| 28 |
* |
| 29 |
* $p = new Process(['php', '-d', 'memory_limit=256M', 'MemoryTest.php']); |
| 30 |
* $p->run(); |
| 31 |
* print $p->getOutput()."\n"; |
| 32 |
* |
| 33 |
* This will output "string(2) "-1", because the process is started with the default php.ini settings. |
| 34 |
* |
| 35 |
* $p = new PhpSubprocess(['MemoryTest.php'], null, null, 60, ['php', '-d', 'memory_limit=256M']); |
| 36 |
* $p->run(); |
| 37 |
* print $p->getOutput()."\n"; |
| 38 |
* |
| 39 |
* This will output "string(4) "256M"", because the process is started with the temporarily created php.ini settings. |
| 40 |
* |
| 41 |
* @author Yanick Witschi <[email protected]> |
| 42 |
* @author Partially copied and heavily inspired from composer/xdebug-handler by John Stevenson <[email protected]> |
| 43 |
*/ |
| 44 |
class PhpSubprocess extends Process |
| 45 |
{ |
| 46 |
/** |
| 47 |
* @param array $command The command to run and its arguments listed as separate entries. They will automatically |
| 48 |
* get prefixed with the PHP binary |
| 49 |
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
| 50 |
* @param array|null $env The environment variables or null to use the same environment as the current PHP process |
| 51 |
* @param int $timeout The timeout in seconds |
| 52 |
* @param array|null $php Path to the PHP binary to use with any additional arguments |
| 53 |
*/ |
| 54 |
public function __construct(array $command, ?string $cwd = null, ?array $env = null, int $timeout = 60, ?array $php = null) |
| 55 |
{ |
| 56 |
if (null === $php) { |
| 57 |
$executableFinder = new PhpExecutableFinder(); |
| 58 |
$php = $executableFinder->find(false); |
| 59 |
$php = false === $php ? null : array_merge([$php], $executableFinder->findArguments()); |
| 60 |
} |
| 61 |
|
| 62 |
if (null === $php) { |
| 63 |
throw new RuntimeException('Unable to find PHP binary.'); |
| 64 |
} |
| 65 |
|
| 66 |
$tmpIni = $this->writeTmpIni($this->getAllIniFiles(), sys_get_temp_dir()); |
| 67 |
|
| 68 |
$php = array_merge($php, ['-n', '-c', $tmpIni]); |
| 69 |
register_shutdown_function('unlink', $tmpIni); |
| 70 |
|
| 71 |
$command = array_merge($php, $command); |
| 72 |
|
| 73 |
parent::__construct($command, $cwd, $env, null, $timeout); |
| 74 |
} |
| 75 |
|
| 76 |
public static function fromShellCommandline(string $command, ?string $cwd = null, ?array $env = null, mixed $input = null, ?float $timeout = 60): static |
| 77 |
{ |
| 78 |
throw new LogicException(\sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class)); |
| 79 |
} |
| 80 |
|
| 81 |
public function start(?callable $callback = null, array $env = []): void |
| 82 |
{ |
| 83 |
if (null === $this->getCommandLine()) { |
| 84 |
throw new RuntimeException('Unable to find the PHP executable.'); |
| 85 |
} |
| 86 |
|
| 87 |
parent::start($callback, $env); |
| 88 |
} |
| 89 |
|
| 90 |
private function writeTmpIni(array $iniFiles, string $tmpDir): string |
| 91 |
{ |
| 92 |
if (false === $tmpfile = @tempnam($tmpDir, '')) { |
| 93 |
throw new RuntimeException('Unable to create temporary ini file.'); |
| 94 |
} |
| 95 |
|
| 96 |
// $iniFiles has at least one item and it may be empty |
| 97 |
if ('' === $iniFiles[0]) { |
| 98 |
array_shift($iniFiles); |
| 99 |
} |
| 100 |
|
| 101 |
$content = ''; |
| 102 |
|
| 103 |
foreach ($iniFiles as $file) { |
| 104 |
// Check for inaccessible ini files |
| 105 |
if (($data = @file_get_contents($file)) === false) { |
| 106 |
throw new RuntimeException('Unable to read ini: '.$file); |
| 107 |
} |
| 108 |
// Check and remove directives after HOST and PATH sections |
| 109 |
if (preg_match('/^\s*\[(?:PATH|HOST)\s*=/mi', $data, $matches, \PREG_OFFSET_CAPTURE)) { |
| 110 |
$data = substr($data, 0, $matches[0][1]); |
| 111 |
} |
| 112 |
|
| 113 |
$content .= $data."\n"; |
| 114 |
} |
| 115 |
|
| 116 |
// Merge loaded settings into our ini content, if it is valid |
| 117 |
$config = parse_ini_string($content); |
| 118 |
$loaded = ini_get_all(null, false); |
| 119 |
|
| 120 |
if (false === $config || false === $loaded) { |
| 121 |
throw new RuntimeException('Unable to parse ini data.'); |
| 122 |
} |
| 123 |
|
| 124 |
$content .= $this->mergeLoadedConfig($loaded, $config); |
| 125 |
|
| 126 |
// Work-around for https://bugs.php.net/bug.php?id=75932 |
| 127 |
$content .= "opcache.enable_cli=0\n"; |
| 128 |
|
| 129 |
if (false === @file_put_contents($tmpfile, $content)) { |
| 130 |
throw new RuntimeException('Unable to write temporary ini file.'); |
| 131 |
} |
| 132 |
|
| 133 |
return $tmpfile; |
| 134 |
} |
| 135 |
|
| 136 |
private function mergeLoadedConfig(array $loadedConfig, array $iniConfig): string |
| 137 |
{ |
| 138 |
$content = ''; |
| 139 |
|
| 140 |
foreach ($loadedConfig as $name => $value) { |
| 141 |
if (!\is_string($value)) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
if (!isset($iniConfig[$name]) || $iniConfig[$name] !== $value) { |
| 146 |
// Double-quote escape each value |
| 147 |
$content .= $name.'="'.addcslashes($value, '\\"')."\"\n"; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return $content; |
| 152 |
} |
| 153 |
|
| 154 |
private function getAllIniFiles(): array |
| 155 |
{ |
| 156 |
$paths = [(string) php_ini_loaded_file()]; |
| 157 |
|
| 158 |
if (false !== $scanned = php_ini_scanned_files()) { |
| 159 |
$paths = array_merge($paths, array_map('trim', explode(',', $scanned))); |
| 160 |
} |
| 161 |
|
| 162 |
return $paths; |
| 163 |
} |
| 164 |
} |
| 165 |
|