2010-04-17 13:49:58 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Process;
|
2010-04-17 13:49:58 +01:00
|
|
|
|
|
|
|
/*
|
2010-04-24 00:22:16 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-17 13:49:58 +01:00
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-09-19 23:40:30 +01:00
|
|
|
* PhpProcess runs a PHP script in an independent process.
|
2010-04-17 13:49:58 +01:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class PhpProcess extends Process
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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())
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
parent::__construct(null, $cwd, $env, $script, $timeout, $options);
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets the path to the PHP binary to use.
|
|
|
|
*/
|
|
|
|
public function setPhpBinary($php)
|
|
|
|
{
|
|
|
|
$this->commandline = $php;
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-09-19 23:40:30 +01:00
|
|
|
* run the process.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @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)
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $this->commandline) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->commandline = $this->getPhpBinary();
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
parent::run($callback);
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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()
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (getenv('PHP_PATH')) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!is_executable($php = getenv('PHP_PATH'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException('The defined PHP_PATH environment variable is not a valid PHP executable.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $php;
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array('');
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($suffixes as $suffix) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_executable($php = PHP_BINDIR.DIRECTORY_SEPARATOR.'php'.$suffix)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $php;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException('Unable to find the PHP executable.');
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|