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
28 * @param string $script The PHP script to run (as a string)
29 * @param string|null $cwd The working directory or null to use the working dir of the current PHP process
30 * @param array|null $env The environment variables or null to use the same environment as the current PHP process
31 * @param int $timeout The timeout in seconds
32 * @param array $options An array of options for proc_open
34 public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = null)
36 $executableFinder = new PhpExecutableFinder();
37 if (false === $php = $executableFinder->find(false)) {
40 $php = array_merge(array($php), $executableFinder->findArguments());
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);
49 if (null !== $options) {
50 @trigger_error(sprintf('The $options parameter of the %s constructor is deprecated since Symfony 3.3 and will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
53 parent::__construct($php, $cwd, $env, $script, $timeout, $options);
57 * Sets the path to the PHP binary to use.
59 public function setPhpBinary($php)
61 $this->setCommandLine($php);
67 public function start(callable $callback = null/*, array $env = array()*/)
69 if (null === $this->getCommandLine()) {
70 throw new RuntimeException('Unable to find the PHP executable.');
72 $env = 1 < \func_num_args() ? func_get_arg(1) : null;
74 parent::start($callback, $env);