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