Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Process; Chris@0: Chris@0: use Symfony\Component\Process\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * PhpProcess runs a PHP script in an independent process. Chris@0: * Chris@0: * $p = new PhpProcess(''); Chris@0: * $p->run(); Chris@0: * print $p->getOutput()."\n"; Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class PhpProcess extends Process Chris@0: { Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param string $script The PHP script to run (as a string) Chris@0: * @param string|null $cwd The working directory or null to use the working dir of the current PHP process Chris@0: * @param array|null $env The environment variables or null to use the same environment as the current PHP process Chris@0: * @param int $timeout The timeout in seconds Chris@0: * @param array $options An array of options for proc_open Chris@0: */ Chris@0: public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = array()) Chris@0: { Chris@0: $executableFinder = new PhpExecutableFinder(); Chris@0: if (false === $php = $executableFinder->find()) { Chris@0: $php = null; Chris@0: } Chris@0: if ('phpdbg' === PHP_SAPI) { Chris@0: $file = tempnam(sys_get_temp_dir(), 'dbg'); Chris@0: file_put_contents($file, $script); Chris@0: register_shutdown_function('unlink', $file); Chris@0: $php .= ' '.ProcessUtils::escapeArgument($file); Chris@0: $script = null; Chris@0: } Chris@0: if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) { Chris@0: // exec is mandatory to deal with sending a signal to the process Chris@0: // see https://github.com/symfony/symfony/issues/5030 about prepending Chris@0: // command with exec Chris@0: $php = 'exec '.$php; Chris@0: } Chris@0: Chris@0: parent::__construct($php, $cwd, $env, $script, $timeout, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the path to the PHP binary to use. Chris@0: */ Chris@0: public function setPhpBinary($php) Chris@0: { Chris@0: $this->setCommandLine($php); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function start(callable $callback = null) Chris@0: { Chris@0: if (null === $this->getCommandLine()) { Chris@0: throw new RuntimeException('Unable to find the PHP executable.'); Chris@0: } Chris@0: Chris@0: parent::start($callback); Chris@0: } Chris@0: }