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 | * @api |
---|
26 | */ |
---|
27 | class PhpProcess extends Process |
---|
28 | { |
---|
29 | private $executableFinder; |
---|
30 | |
---|
31 | /** |
---|
32 | * Constructor. |
---|
33 | * |
---|
34 | * @param string $script The PHP script to run (as a string) |
---|
35 | * @param string $cwd The working directory |
---|
36 | * @param array $env The environment variables |
---|
37 | * @param integer $timeout The timeout in seconds |
---|
38 | * @param array $options An array of options for proc_open |
---|
39 | * |
---|
40 | * @api |
---|
41 | */ |
---|
42 | public function __construct($script, $cwd = null, array $env = array(), $timeout = 60, array $options = array()) |
---|
43 | { |
---|
44 | parent::__construct(null, $cwd, $env, $script, $timeout, $options); |
---|
45 | |
---|
46 | $this->executableFinder = new PhpExecutableFinder(); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Sets the path to the PHP binary to use. |
---|
51 | * |
---|
52 | * @api |
---|
53 | */ |
---|
54 | public function setPhpBinary($php) |
---|
55 | { |
---|
56 | $this->setCommandLine($php); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Runs the process. |
---|
61 | * |
---|
62 | * @param Closure|string|array $callback A PHP callback to run whenever there is some |
---|
63 | * output available on STDOUT or STDERR |
---|
64 | * |
---|
65 | * @return integer The exit status code |
---|
66 | * |
---|
67 | * @api |
---|
68 | */ |
---|
69 | public function run($callback = null) |
---|
70 | { |
---|
71 | if (null === $this->getCommandLine()) { |
---|
72 | if (false === $php = $this->executableFinder->find()) { |
---|
73 | throw new RuntimeException('Unable to find the PHP executable.'); |
---|
74 | } |
---|
75 | $this->setCommandLine($php); |
---|
76 | } |
---|
77 | |
---|
78 | return parent::run($callback); |
---|
79 | } |
---|
80 | } |
---|