PhpProcess.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * @api
  22. */
  23. class PhpProcess extends Process
  24. {
  25. private $executableFinder;
  26. /**
  27. * Constructor.
  28. *
  29. * @param string $script The PHP script to run (as a string)
  30. * @param string $cwd The working directory
  31. * @param array $env The environment variables
  32. * @param integer $timeout The timeout in seconds
  33. * @param array $options An array of options for proc_open
  34. *
  35. * @api
  36. */
  37. public function __construct($script, $cwd = null, array $env = array(), $timeout = 60, array $options = array())
  38. {
  39. parent::__construct(null, $cwd, $env, $script, $timeout, $options);
  40. $this->executableFinder = new PhpExecutableFinder();
  41. }
  42. /**
  43. * Sets the path to the PHP binary to use.
  44. *
  45. * @api
  46. */
  47. public function setPhpBinary($php)
  48. {
  49. $this->setCommandLine($php);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function start($callback = null)
  55. {
  56. if (null === $this->getCommandLine()) {
  57. if (false === $php = $this->executableFinder->find()) {
  58. throw new RuntimeException('Unable to find the PHP executable.');
  59. }
  60. $this->setCommandLine($php);
  61. }
  62. parent::start($callback);
  63. }
  64. }