PhpProcess.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * Runs the process.
  52. *
  53. * @param Closure|string|array $callback A PHP callback to run whenever there is some
  54. * output available on STDOUT or STDERR
  55. *
  56. * @return integer The exit status code
  57. *
  58. * @api
  59. */
  60. public function run($callback = null)
  61. {
  62. if (null === $this->getCommandLine()) {
  63. if (false === $php = $this->executableFinder->find()) {
  64. throw new \RuntimeException('Unable to find the PHP executable.');
  65. }
  66. $this->setCommandLine($php);
  67. }
  68. return parent::run($callback);
  69. }
  70. }