[7578] | 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\InvalidArgumentException; |
---|
| 15 | use Symfony\Component\Process\Exception\LogicException; |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * Process builder. |
---|
| 19 | * |
---|
| 20 | * @author Kris Wallsmith <kris@symfony.com> |
---|
| 21 | */ |
---|
| 22 | class ProcessBuilder |
---|
| 23 | { |
---|
| 24 | private $arguments; |
---|
| 25 | private $cwd; |
---|
| 26 | private $env; |
---|
| 27 | private $stdin; |
---|
| 28 | private $timeout; |
---|
| 29 | private $options; |
---|
| 30 | private $inheritEnv; |
---|
| 31 | |
---|
| 32 | public function __construct(array $arguments = array()) |
---|
| 33 | { |
---|
| 34 | $this->arguments = $arguments; |
---|
| 35 | |
---|
| 36 | $this->timeout = 60; |
---|
| 37 | $this->options = array(); |
---|
| 38 | $this->env = array(); |
---|
| 39 | $this->inheritEnv = true; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | public static function create(array $arguments = array()) |
---|
| 43 | { |
---|
| 44 | return new static($arguments); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Adds an unescaped argument to the command string. |
---|
| 49 | * |
---|
| 50 | * @param string $argument A command argument |
---|
| 51 | */ |
---|
| 52 | public function add($argument) |
---|
| 53 | { |
---|
| 54 | $this->arguments[] = $argument; |
---|
| 55 | |
---|
| 56 | return $this; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public function setWorkingDirectory($cwd) |
---|
| 60 | { |
---|
| 61 | $this->cwd = $cwd; |
---|
| 62 | |
---|
| 63 | return $this; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public function inheritEnvironmentVariables($inheritEnv = true) |
---|
| 67 | { |
---|
| 68 | $this->inheritEnv = $inheritEnv; |
---|
| 69 | |
---|
| 70 | return $this; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public function setEnv($name, $value) |
---|
| 74 | { |
---|
| 75 | $this->env[$name] = $value; |
---|
| 76 | |
---|
| 77 | return $this; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public function setInput($stdin) |
---|
| 81 | { |
---|
| 82 | $this->stdin = $stdin; |
---|
| 83 | |
---|
| 84 | return $this; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Sets the process timeout. |
---|
| 89 | * |
---|
| 90 | * To disable the timeout, set this value to null. |
---|
| 91 | * |
---|
| 92 | * @param integer|null |
---|
| 93 | */ |
---|
| 94 | public function setTimeout($timeout) |
---|
| 95 | { |
---|
| 96 | if (null === $timeout) { |
---|
| 97 | $this->timeout = null; |
---|
| 98 | |
---|
| 99 | return $this; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | $timeout = (integer) $timeout; |
---|
| 103 | |
---|
| 104 | if ($timeout < 0) { |
---|
| 105 | throw new InvalidArgumentException('The timeout value must be a valid positive integer.'); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | $this->timeout = $timeout; |
---|
| 109 | |
---|
| 110 | return $this; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public function setOption($name, $value) |
---|
| 114 | { |
---|
| 115 | $this->options[$name] = $value; |
---|
| 116 | |
---|
| 117 | return $this; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | public function getProcess() |
---|
| 121 | { |
---|
| 122 | if (!count($this->arguments)) { |
---|
| 123 | throw new LogicException('You must add() command arguments before calling getProcess().'); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | $options = $this->options; |
---|
| 127 | |
---|
| 128 | $script = implode(' ', array_map('escapeshellarg', $this->arguments)); |
---|
| 129 | |
---|
| 130 | if ($this->inheritEnv) { |
---|
| 131 | $env = $this->env ? $this->env + $_ENV : null; |
---|
| 132 | } else { |
---|
| 133 | $env = $this->env; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options); |
---|
| 137 | } |
---|
| 138 | } |
---|