1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Process;
13
14use Symfony\Component\Process\Exception\RuntimeException;
15
16/**
17 * PhpProcess runs a PHP script in an independent process.
18 *
19 *     $p = new PhpProcess('<?php echo "foo"; ?>');
20 *     $p->run();
21 *     print $p->getOutput()."\n";
22 *
23 * @author Fabien Potencier <fabien@symfony.com>
24 */
25class PhpProcess extends Process
26{
27    /**
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
33     */
34    public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = null)
35    {
36        $executableFinder = new PhpExecutableFinder();
37        if (false === $php = $executableFinder->find(false)) {
38            $php = null;
39        } else {
40            $php = array_merge([$php], $executableFinder->findArguments());
41        }
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[] = $file;
47            $script = null;
48        }
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);
51        }
52
53        parent::__construct($php, $cwd, $env, $script, $timeout, $options);
54    }
55
56    /**
57     * Sets the path to the PHP binary to use.
58     */
59    public function setPhpBinary($php)
60    {
61        $this->setCommandLine($php);
62    }
63
64    /**
65     * {@inheritdoc}
66     */
67    public function start(callable $callback = null/*, array $env = []*/)
68    {
69        if (null === $this->getCommandLine()) {
70            throw new RuntimeException('Unable to find the PHP executable.');
71        }
72        $env = 1 < \func_num_args() ? func_get_arg(1) : null;
73
74        parent::start($callback, $env);
75    }
76}
77