TYPO3  7.6
vendor/symfony/finder/Shell/Command.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 
12 namespace Symfony\Component\Finder\Shell;
13 
17 class Command
18 {
22  private $parent;
23 
27  private $bits = array();
28 
32  private $labels = array();
33 
37  private $errorHandler;
38 
44  public function __construct(Command $parent = null)
45  {
46  $this->parent = $parent;
47  }
48 
54  public function __toString()
55  {
56  return $this->join();
57  }
58 
66  public static function create(Command $parent = null)
67  {
68  return new self($parent);
69  }
70 
78  public static function escape($input)
79  {
80  return escapeshellcmd($input);
81  }
82 
90  public static function quote($input)
91  {
92  return escapeshellarg($input);
93  }
94 
102  public function add($bit)
103  {
104  $this->bits[] = $bit;
105 
106  return $this;
107  }
108 
116  public function top($bit)
117  {
118  array_unshift($this->bits, $bit);
119 
120  foreach ($this->labels as $label => $index) {
121  $this->labels[$label] += 1;
122  }
123 
124  return $this;
125  }
126 
134  public function arg($arg)
135  {
136  $this->bits[] = self::quote($arg);
137 
138  return $this;
139  }
140 
148  public function cmd($esc)
149  {
150  $this->bits[] = self::escape($esc);
151 
152  return $this;
153  }
154 
164  public function ins($label)
165  {
166  if (isset($this->labels[$label])) {
167  throw new \RuntimeException(sprintf('Label "%s" already exists.', $label));
168  }
169 
170  $this->bits[] = self::create($this);
171  $this->labels[$label] = count($this->bits) - 1;
172 
173  return $this->bits[$this->labels[$label]];
174  }
175 
185  public function get($label)
186  {
187  if (!isset($this->labels[$label])) {
188  throw new \RuntimeException(sprintf('Label "%s" does not exist.', $label));
189  }
190 
191  return $this->bits[$this->labels[$label]];
192  }
193 
201  public function end()
202  {
203  if (null === $this->parent) {
204  throw new \RuntimeException('Calling end on root command doesn\'t make sense.');
205  }
206 
207  return $this->parent;
208  }
209 
215  public function length()
216  {
217  return count($this->bits);
218  }
219 
225  public function setErrorHandler(\Closure $errorHandler)
226  {
227  $this->errorHandler = $errorHandler;
228 
229  return $this;
230  }
231 
235  public function getErrorHandler()
236  {
237  return $this->errorHandler;
238  }
239 
247  public function execute()
248  {
249  if (null === $errorHandler = $this->errorHandler) {
250  exec($this->join(), $output);
251  } else {
252  $process = proc_open($this->join(), array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
253  $output = preg_split('~(\r\n|\r|\n)~', stream_get_contents($pipes[1]), -1, PREG_SPLIT_NO_EMPTY);
254 
255  if ($error = stream_get_contents($pipes[2])) {
256  $errorHandler($error);
257  }
258 
259  proc_close($process);
260  }
261 
262  return $output ?: array();
263  }
264 
270  public function join()
271  {
272  return implode(' ', array_filter(
273  array_map(function ($bit) {
274  return $bit instanceof Command ? $bit->join() : ($bit ?: null);
275  }, $this->bits),
276  function ($bit) { return null !== $bit; }
277  ));
278  }
279 
288  public function addAtIndex($bit, $index)
289  {
290  array_splice($this->bits, $index, 0, $bit instanceof self ? array($bit) : $bit);
291 
292  return $this;
293  }
294 }