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. 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. /**
  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 int $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. $executableFinder = new PhpExecutableFinder();
  39. if (false === $php = $executableFinder->find()) {
  40. $php = null;
  41. }
  42. parent::__construct($php, $cwd, $env, $script, $timeout, $options);
  43. }
  44. /**
  45. * Sets the path to the PHP binary to use.
  46. *
  47. * @api
  48. */
  49. public function setPhpBinary($php)
  50. {
  51. $this->setCommandLine($php);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function start($callback = null)
  57. {
  58. if (null === $this->getCommandLine()) {
  59. throw new RuntimeException('Unable to find the PHP executable.');
  60. }
  61. parent::start($callback);
  62. }
  63. }