scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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 = array();
27 private $input;
28 private $timeout = 60;
29 private $options;
30 private $inheritEnv = true;
31 private $prefix = array();
32 private $outputDisabled = false;
33  
34 /**
35 * Constructor.
36 *
37 * @param string[] $arguments An array of arguments
38 */
39 public function __construct(array $arguments = array())
40 {
41 $this->arguments = $arguments;
42 }
43  
44 /**
45 * Creates a process builder instance.
46 *
47 * @param string[] $arguments An array of arguments
48 *
49 * @return static
50 */
51 public static function create(array $arguments = array())
52 {
53 return new static($arguments);
54 }
55  
56 /**
57 * Adds an unescaped argument to the command string.
58 *
59 * @param string $argument A command argument
60 *
61 * @return $this
62 */
63 public function add($argument)
64 {
65 $this->arguments[] = $argument;
66  
67 return $this;
68 }
69  
70 /**
71 * Adds a prefix to the command string.
72 *
73 * The prefix is preserved when resetting arguments.
74 *
75 * @param string|array $prefix A command prefix or an array of command prefixes
76 *
77 * @return $this
78 */
79 public function setPrefix($prefix)
80 {
81 $this->prefix = is_array($prefix) ? $prefix : array($prefix);
82  
83 return $this;
84 }
85  
86 /**
87 * Sets the arguments of the process.
88 *
89 * Arguments must not be escaped.
90 * Previous arguments are removed.
91 *
92 * @param string[] $arguments
93 *
94 * @return $this
95 */
96 public function setArguments(array $arguments)
97 {
98 $this->arguments = $arguments;
99  
100 return $this;
101 }
102  
103 /**
104 * Sets the working directory.
105 *
106 * @param null|string $cwd The working directory
107 *
108 * @return $this
109 */
110 public function setWorkingDirectory($cwd)
111 {
112 $this->cwd = $cwd;
113  
114 return $this;
115 }
116  
117 /**
118 * Sets whether environment variables will be inherited or not.
119 *
120 * @param bool $inheritEnv
121 *
122 * @return $this
123 *
124 * @deprecated since version 3.3, to be removed in 4.0.
125 */
126 public function inheritEnvironmentVariables($inheritEnv = true)
127 {
128 @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
129  
130 $this->inheritEnv = $inheritEnv;
131  
132 return $this;
133 }
134  
135 /**
136 * Sets an environment variable.
137 *
138 * Setting a variable overrides its previous value. Use `null` to unset a
139 * defined environment variable.
140 *
141 * @param string $name The variable name
142 * @param null|string $value The variable value
143 *
144 * @return $this
145 */
146 public function setEnv($name, $value)
147 {
148 $this->env[$name] = $value;
149  
150 return $this;
151 }
152  
153 /**
154 * Adds a set of environment variables.
155 *
156 * Already existing environment variables with the same name will be
157 * overridden by the new values passed to this method. Pass `null` to unset
158 * a variable.
159 *
160 * @param array $variables The variables
161 *
162 * @return $this
163 */
164 public function addEnvironmentVariables(array $variables)
165 {
166 $this->env = array_replace($this->env, $variables);
167  
168 return $this;
169 }
170  
171 /**
172 * Sets the input of the process.
173 *
174 * @param resource|scalar|\Traversable|null $input The input content
175 *
176 * @return $this
177 *
178 * @throws InvalidArgumentException In case the argument is invalid
179 */
180 public function setInput($input)
181 {
182 $this->input = ProcessUtils::validateInput(__METHOD__, $input);
183  
184 return $this;
185 }
186  
187 /**
188 * Sets the process timeout.
189 *
190 * To disable the timeout, set this value to null.
191 *
192 * @param float|null $timeout
193 *
194 * @return $this
195 *
196 * @throws InvalidArgumentException
197 */
198 public function setTimeout($timeout)
199 {
200 if (null === $timeout) {
201 $this->timeout = null;
202  
203 return $this;
204 }
205  
206 $timeout = (float) $timeout;
207  
208 if ($timeout < 0) {
209 throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
210 }
211  
212 $this->timeout = $timeout;
213  
214 return $this;
215 }
216  
217 /**
218 * Adds a proc_open option.
219 *
220 * @param string $name The option name
221 * @param string $value The option value
222 *
223 * @return $this
224 *
225 * @deprecated since version 3.3, to be removed in 4.0.
226 */
227 public function setOption($name, $value)
228 {
229 @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
230  
231 $this->options[$name] = $value;
232  
233 return $this;
234 }
235  
236 /**
237 * Disables fetching output and error output from the underlying process.
238 *
239 * @return $this
240 */
241 public function disableOutput()
242 {
243 $this->outputDisabled = true;
244  
245 return $this;
246 }
247  
248 /**
249 * Enables fetching output and error output from the underlying process.
250 *
251 * @return $this
252 */
253 public function enableOutput()
254 {
255 $this->outputDisabled = false;
256  
257 return $this;
258 }
259  
260 /**
261 * Creates a Process instance and returns it.
262 *
263 * @return Process
264 *
265 * @throws LogicException In case no arguments have been provided
266 */
267 public function getProcess()
268 {
269 if (0 === count($this->prefix) && 0 === count($this->arguments)) {
270 throw new LogicException('You must add() command arguments before calling getProcess().');
271 }
272  
273 $arguments = array_merge($this->prefix, $this->arguments);
274 $process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
275  
276 if ($this->inheritEnv) {
277 $process->inheritEnvironmentVariables();
278 }
279 if ($this->outputDisabled) {
280 $process->disableOutput();
281 }
282  
283 return $process;
284 }
285 }