PhpProcess.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\RuntimeException;
  12. /**
  13. * PhpProcess runs a PHP script in an independent process.
  14. *
  15. * $p = new PhpProcess('<?php echo "foo"; ?>');
  16. * $p->run();
  17. * print $p->getOutput()."\n";
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class PhpProcess extends Process
  22. {
  23. /**
  24. * @param string $script The PHP script to run (as a string)
  25. * @param string|null $cwd The working directory or null to use the working dir of the current PHP process
  26. * @param array|null $env The environment variables or null to use the same environment as the current PHP process
  27. * @param int $timeout The timeout in seconds
  28. * @param array $options An array of options for proc_open
  29. */
  30. public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = null)
  31. {
  32. $executableFinder = new PhpExecutableFinder();
  33. if (false === $php = $executableFinder->find(false)) {
  34. $php = null;
  35. } else {
  36. $php = array_merge([$php], $executableFinder->findArguments());
  37. }
  38. if ('phpdbg' === \PHP_SAPI) {
  39. $file = tempnam(sys_get_temp_dir(), 'dbg');
  40. file_put_contents($file, $script);
  41. register_shutdown_function('unlink', $file);
  42. $php[] = $file;
  43. $script = null;
  44. }
  45. if (null !== $options) {
  46. @trigger_error(sprintf('The $options parameter of the %s constructor is deprecated since Symfony 3.3 and will be removed in 4.0.', __CLASS__), \E_USER_DEPRECATED);
  47. }
  48. parent::__construct($php, $cwd, $env, $script, $timeout, $options);
  49. }
  50. /**
  51. * Sets the path to the PHP binary to use.
  52. */
  53. public function setPhpBinary($php)
  54. {
  55. $this->setCommandLine($php);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function start(callable $callback = null/*, array $env = []*/)
  61. {
  62. if (null === $this->getCommandLine()) {
  63. throw new RuntimeException('Unable to find the PHP executable.');
  64. }
  65. $env = 1 < \func_num_args() ? func_get_arg(1) : null;
  66. parent::start($callback, $env);
  67. }
  68. }