4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Shell;
14 @trigger_error('The '.__NAMESPACE__.'\Command class is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
17 * @author Jean-François Simon <contact@jfsimon.fr>
19 * @deprecated since 2.8, to be removed in 3.0.
24 private $bits = array();
25 private $labels = array();
30 private $errorHandler;
32 public function __construct(Command $parent = null)
34 $this->parent = $parent;
38 * Returns command as string.
42 public function __toString()
48 * Creates a new Command instance.
52 public static function create(Command $parent = null)
54 return new self($parent);
58 * Escapes special chars from input.
60 * @param string $input A string to escape
62 * @return string The escaped string
64 public static function escape($input)
66 return escapeshellcmd($input);
72 * @param string $input An argument string
74 * @return string The quoted string
76 public static function quote($input)
78 return escapeshellarg($input);
82 * Appends a string or a Command instance.
84 * @param string|Command $bit
88 public function add($bit)
96 * Prepends a string or a command instance.
98 * @param string|Command $bit
102 public function top($bit)
104 array_unshift($this->bits, $bit);
106 foreach ($this->labels as $label => $index) {
107 $this->labels[$label] += 1;
114 * Appends an argument, will be quoted.
120 public function arg($arg)
122 $this->bits[] = self::quote($arg);
128 * Appends escaped special command chars.
134 public function cmd($esc)
136 $this->bits[] = self::escape($esc);
142 * Inserts a labeled command to feed later.
144 * @param string $label The unique label
146 * @return self|string
148 * @throws \RuntimeException If label already exists
150 public function ins($label)
152 if (isset($this->labels[$label])) {
153 throw new \RuntimeException(sprintf('Label "%s" already exists.', $label));
156 $this->bits[] = self::create($this);
157 $this->labels[$label] = count($this->bits) - 1;
159 return $this->bits[$this->labels[$label]];
163 * Retrieves a previously labeled command.
165 * @param string $label
167 * @return self|string
169 * @throws \RuntimeException
171 public function get($label)
173 if (!isset($this->labels[$label])) {
174 throw new \RuntimeException(sprintf('Label "%s" does not exist.', $label));
177 return $this->bits[$this->labels[$label]];
181 * Returns parent command (if any).
185 * @throws \RuntimeException If command has no parent
187 public function end()
189 if (null === $this->parent) {
190 throw new \RuntimeException('Calling end on root command doesn\'t make sense.');
193 return $this->parent;
197 * Counts bits stored in command.
199 * @return int The bits count
201 public function length()
203 return count($this->bits);
209 public function setErrorHandler(\Closure $errorHandler)
211 $this->errorHandler = $errorHandler;
217 * @return \Closure|null
219 public function getErrorHandler()
221 return $this->errorHandler;
225 * Executes current command.
227 * @return array The command result
229 * @throws \RuntimeException
231 public function execute()
233 if (null === $errorHandler = $this->errorHandler) {
234 exec($this->join(), $output);
236 $process = proc_open($this->join(), array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
237 $output = preg_split('~(\r\n|\r|\n)~', stream_get_contents($pipes[1]), -1, PREG_SPLIT_NO_EMPTY);
239 if ($error = stream_get_contents($pipes[2])) {
240 $errorHandler($error);
243 proc_close($process);
246 return $output ?: array();
254 public function join()
256 return implode(' ', array_filter(
257 array_map(function ($bit) {
258 return $bit instanceof Command ? $bit->join() : ($bit ?: null);
260 function ($bit) { return null !== $bit; }
265 * Insert a string or a Command instance before the bit at given position $index (index starts from 0).
267 * @param string|Command $bit
272 public function addAtIndex($bit, $index)
274 array_splice($this->bits, $index, 0, $bit instanceof self ? array($bit) : $bit);