ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ProcessBuilder.php
Go to the documentation of this file.
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 
13 
16 
23 {
24  private $arguments;
25  private $cwd;
26  private $env = array();
27  private $input;
28  private $timeout = 60;
29  private $options = array();
30  private $inheritEnv = true;
31  private $prefix = array();
32  private $outputDisabled = false;
33 
39  public function __construct(array $arguments = array())
40  {
41  $this->arguments = $arguments;
42  }
43 
51  public static function create(array $arguments = array())
52  {
53  return new static($arguments);
54  }
55 
63  public function add($argument)
64  {
65  $this->arguments[] = $argument;
66 
67  return $this;
68  }
69 
79  public function setPrefix($prefix)
80  {
81  $this->prefix = is_array($prefix) ? $prefix : array($prefix);
82 
83  return $this;
84  }
85 
96  public function setArguments(array $arguments)
97  {
98  $this->arguments = $arguments;
99 
100  return $this;
101  }
102 
110  public function setWorkingDirectory($cwd)
111  {
112  $this->cwd = $cwd;
113 
114  return $this;
115  }
116 
124  public function inheritEnvironmentVariables($inheritEnv = true)
125  {
126  $this->inheritEnv = $inheritEnv;
127 
128  return $this;
129  }
130 
142  public function setEnv($name, $value)
143  {
144  $this->env[$name] = $value;
145 
146  return $this;
147  }
148 
160  public function addEnvironmentVariables(array $variables)
161  {
162  $this->env = array_replace($this->env, $variables);
163 
164  return $this;
165  }
166 
176  public function setInput($input)
177  {
178  $this->input = ProcessUtils::validateInput(__METHOD__, $input);
179 
180  return $this;
181  }
182 
194  public function setTimeout($timeout)
195  {
196  if (null === $timeout) {
197  $this->timeout = null;
198 
199  return $this;
200  }
201 
202  $timeout = (float) $timeout;
203 
204  if ($timeout < 0) {
205  throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
206  }
207 
208  $this->timeout = $timeout;
209 
210  return $this;
211  }
212 
221  public function setOption($name, $value)
222  {
223  $this->options[$name] = $value;
224 
225  return $this;
226  }
227 
233  public function disableOutput()
234  {
235  $this->outputDisabled = true;
236 
237  return $this;
238  }
239 
245  public function enableOutput()
246  {
247  $this->outputDisabled = false;
248 
249  return $this;
250  }
251 
259  public function getProcess()
260  {
261  if (0 === count($this->prefix) && 0 === count($this->arguments)) {
262  throw new LogicException('You must add() command arguments before calling getProcess().');
263  }
264 
266 
267  $arguments = array_merge($this->prefix, $this->arguments);
268  $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
269 
270  if ($this->inheritEnv) {
271  $env = array_replace($_ENV, $_SERVER, $this->env);
272  } else {
273  $env = $this->env;
274  }
275 
276  $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
277 
278  if ($this->outputDisabled) {
279  $process->disableOutput();
280  }
281 
282  return $process;
283  }
284 }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
enableOutput()
Enables fetching output and error output from the underlying process.
static create(array $arguments=array())
Creates a process builder instance.
setTimeout($timeout)
Sets the process timeout.
setPrefix($prefix)
Adds a prefix to the command string.
setEnv($name, $value)
Sets an environment variable.
input
Definition: langcheck.php:166
inheritEnvironmentVariables($inheritEnv=true)
Sets whether environment variables will be inherited or not.
setOption($name, $value)
Adds a proc_open option.
static validateInput($caller, $input)
Validates and normalizes a Process input.
Create styles array
The data for the language used.
LogicException for the Process Component.
getProcess()
Creates a Process instance and returns it.
Process is a thin wrapper around proc_* functions to easily start independent PHP processes...
Definition: Process.php:30
setArguments(array $arguments)
Sets the arguments of the process.
setWorkingDirectory($cwd)
Sets the working directory.
disableOutput()
Disables fetching output and error output from the underlying process.
__construct(array $arguments=array())
Constructor.
setInput($input)
Sets the input of the process.
addEnvironmentVariables(array $variables)
Adds a set of environment variables.
add($argument)
Adds an unescaped argument to the command string.