PhpSubprocess.php 5.9 KB

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