123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace Symfony\Component\Process;
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * PhpProcess runs a PHP script in an independent process.
- *
- * $p = new PhpProcess('<?php echo "foo"; ?>');
- * $p->run();
- * print $p->getOutput()."\n";
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class PhpProcess extends Process
- {
- /**
- * Constructor.
- *
- * @param string $script The PHP script to run (as a string)
- * @param string $cwd The working directory
- * @param array $env The environment variables
- * @param integer $timeout The timeout in seconds
- * @param array $options An array of options for proc_open
- */
- public function __construct($script, $cwd = null, array $env = array(), $timeout = 60, array $options = array())
- {
- parent::__construct(null, $cwd, $env, $script, $timeout, $options);
- }
- /**
- * Sets the path to the PHP binary to use.
- */
- public function setPhpBinary($php)
- {
- $this->commandline = $php;
- }
- /**
- * Runs the process.
- *
- * @param Closure|string|array $callback A PHP callback to run whenever there is some
- * output available on STDOUT or STDERR
- *
- * @return integer The exit status code
- */
- public function run($callback = null)
- {
- if (null === $this->commandline) {
- $this->commandline = $this->getPhpBinary();
- }
- return parent::run($callback);
- }
- /**
- * Returns the PHP binary path.
- *
- * @return string The PHP binary path
- *
- * @throws \RuntimeException When defined PHP_PATH is not executable or not found
- */
- static public function getPhpBinary()
- {
- if (getenv('PHP_PATH')) {
- if (!is_executable($php = getenv('PHP_PATH'))) {
- throw new \RuntimeException('The defined PHP_PATH environment variable is not a valid PHP executable.');
- }
- return $php;
- }
- $suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array('');
- foreach ($suffixes as $suffix) {
- if (is_executable($php = PHP_BINDIR.DIRECTORY_SEPARATOR.'php'.$suffix)) {
- return $php;
- }
- }
- throw new \RuntimeException('Unable to find the PHP executable.');
- }
- }
|