4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process;
14 use Symfony\Component\Process\Exception\RuntimeException;
17 * PhpProcess runs a PHP script in an independent process.
19 * $p = new PhpProcess('<?php echo "foo"; ?>');
21 * print $p->getOutput()."\n";
23 * @author Fabien Potencier <fabien@symfony.com>
25 class PhpProcess extends Process
30 * @param string $script The PHP script to run (as a string)
31 * @param string|null $cwd The working directory or null to use the working dir of the current PHP process
32 * @param array|null $env The environment variables or null to use the same environment as the current PHP process
33 * @param int $timeout The timeout in seconds
34 * @param array $options An array of options for proc_open
36 public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = array())
38 $executableFinder = new PhpExecutableFinder();
39 if (false === $php = $executableFinder->find()) {
42 if ('phpdbg' === PHP_SAPI) {
43 $file = tempnam(sys_get_temp_dir(), 'dbg');
44 file_put_contents($file, $script);
45 register_shutdown_function('unlink', $file);
46 $php .= ' '.ProcessUtils::escapeArgument($file);
49 if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) {
50 // exec is mandatory to deal with sending a signal to the process
51 // see https://github.com/symfony/symfony/issues/5030 about prepending
56 parent::__construct($php, $cwd, $env, $script, $timeout, $options);
60 * Sets the path to the PHP binary to use.
62 public function setPhpBinary($php)
64 $this->setCommandLine($php);
70 public function start($callback = null)
72 if (null === $this->getCommandLine()) {
73 throw new RuntimeException('Unable to find the PHP executable.');
76 parent::start($callback);