PhpProcess.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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|null $php Path to the PHP binary to use with any additional arguments
  29. */
  30. public function __construct(string $script, string $cwd = null, array $env = null, int $timeout = 60, array $php = null)
  31. {
  32. if (null === $php) {
  33. $executableFinder = new PhpExecutableFinder();
  34. $php = $executableFinder->find(false);
  35. $php = false === $php ? null : array_merge([$php], $executableFinder->findArguments());
  36. }
  37. if ('phpdbg' === \PHP_SAPI) {
  38. $file = tempnam(sys_get_temp_dir(), 'dbg');
  39. file_put_contents($file, $script);
  40. register_shutdown_function('unlink', $file);
  41. $php[] = $file;
  42. $script = null;
  43. }
  44. parent::__construct($php, $cwd, $env, $script, $timeout);
  45. }
  46. /**
  47. * Sets the path to the PHP binary to use.
  48. *
  49. * @deprecated since Symfony 4.2, use the $php argument of the constructor instead.
  50. */
  51. public function setPhpBinary($php)
  52. {
  53. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the $php argument of the constructor instead.', __METHOD__), E_USER_DEPRECATED);
  54. $this->setCommandLine($php);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function start(callable $callback = null, array $env = [])
  60. {
  61. if (null === $this->getCommandLine()) {
  62. throw new RuntimeException('Unable to find the PHP executable.');
  63. }
  64. parent::start($callback, $env);
  65. }
  66. }