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@0
|
19 * $p = new PhpProcess('<?php echo "foo"; ?>');
|
Chris@0
|
20 * $p->run();
|
Chris@0
|
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 * Constructor.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @param string $script The PHP script to run (as a string)
|
Chris@0
|
31 * @param string|null $cwd The working directory or null to use the working dir of the current PHP process
|
Chris@0
|
32 * @param array|null $env The environment variables or null to use the same environment as the current PHP process
|
Chris@0
|
33 * @param int $timeout The timeout in seconds
|
Chris@0
|
34 * @param array $options An array of options for proc_open
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = array())
|
Chris@0
|
37 {
|
Chris@0
|
38 $executableFinder = new PhpExecutableFinder();
|
Chris@0
|
39 if (false === $php = $executableFinder->find()) {
|
Chris@0
|
40 $php = null;
|
Chris@0
|
41 }
|
Chris@0
|
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@0
|
46 $php .= ' '.ProcessUtils::escapeArgument($file);
|
Chris@0
|
47 $script = null;
|
Chris@0
|
48 }
|
Chris@0
|
49 if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) {
|
Chris@0
|
50 // exec is mandatory to deal with sending a signal to the process
|
Chris@0
|
51 // see https://github.com/symfony/symfony/issues/5030 about prepending
|
Chris@0
|
52 // command with exec
|
Chris@0
|
53 $php = 'exec '.$php;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 parent::__construct($php, $cwd, $env, $script, $timeout, $options);
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Sets the path to the PHP binary to use.
|
Chris@0
|
61 */
|
Chris@0
|
62 public function setPhpBinary($php)
|
Chris@0
|
63 {
|
Chris@0
|
64 $this->setCommandLine($php);
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * {@inheritdoc}
|
Chris@0
|
69 */
|
Chris@0
|
70 public function start(callable $callback = null)
|
Chris@0
|
71 {
|
Chris@0
|
72 if (null === $this->getCommandLine()) {
|
Chris@0
|
73 throw new RuntimeException('Unable to find the PHP executable.');
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 parent::start($callback);
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|