PhpProcess.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /**
  12. * PhpProcess runs a PHP script in an independent process.
  13. *
  14. * $p = new PhpProcess('<?php echo "foo"; ?>');
  15. * $p->run();
  16. * print $p->getOutput()."\n";
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @api
  21. */
  22. class PhpProcess extends Process
  23. {
  24. private $executableFinder;
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $script The PHP script to run (as a string)
  29. * @param string $cwd The working directory
  30. * @param array $env The environment variables
  31. * @param integer $timeout The timeout in seconds
  32. * @param array $options An array of options for proc_open
  33. *
  34. * @api
  35. */
  36. public function __construct($script, $cwd = null, array $env = array(), $timeout = 60, array $options = array())
  37. {
  38. parent::__construct(null, $cwd, $env, $script, $timeout, $options);
  39. $this->executableFinder = new PhpExecutableFinder();
  40. }
  41. /**
  42. * Sets the path to the PHP binary to use.
  43. *
  44. * @api
  45. */
  46. public function setPhpBinary($php)
  47. {
  48. $this->setCommandLine($php);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function start($callback = null)
  54. {
  55. if (null === $this->getCommandLine()) {
  56. if (false === $php = $this->executableFinder->find()) {
  57. throw new \RuntimeException('Unable to find the PHP executable.');
  58. }
  59. $this->setCommandLine($php);
  60. }
  61. parent::start($callback);
  62. }
  63. }