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
|
|
12
|
namespace Symfony\Component\Process;
|
13
|
|
14
|
use 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
|
*/
|
25
|
class PhpProcess extends Process
|
26
|
{
|
27
|
/**
|
28
|
* Constructor.
|
29
|
*
|
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
|
35
|
*/
|
36
|
public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = array())
|
37
|
{
|
38
|
$executableFinder = new PhpExecutableFinder();
|
39
|
if (false === $php = $executableFinder->find()) {
|
40
|
$php = null;
|
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 .= ' '.ProcessUtils::escapeArgument($file);
|
47
|
$script = null;
|
48
|
}
|
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
|
52
|
// command with exec
|
53
|
$php = 'exec '.$php;
|
54
|
}
|
55
|
|
56
|
parent::__construct($php, $cwd, $env, $script, $timeout, $options);
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Sets the path to the PHP binary to use.
|
61
|
*/
|
62
|
public function setPhpBinary($php)
|
63
|
{
|
64
|
$this->setCommandLine($php);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* {@inheritdoc}
|
69
|
*/
|
70
|
public function start(callable $callback = null)
|
71
|
{
|
72
|
if (null === $this->getCommandLine()) {
|
73
|
throw new RuntimeException('Unable to find the PHP executable.');
|
74
|
}
|
75
|
|
76
|
parent::start($callback);
|
77
|
}
|
78
|
}
|