Yaffs site version 1.1
[yaffs-website] / vendor / symfony / finder / Shell / Command.php
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
14 @trigger_error('The '.__NAMESPACE__.'\Command class is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
15
16 /**
17  * @author Jean-François Simon <contact@jfsimon.fr>
18  *
19  * @deprecated since 2.8, to be removed in 3.0.
20  */
21 class Command
22 {
23     /**
24      * @var Command|null
25      */
26     private $parent;
27
28     /**
29      * @var array
30      */
31     private $bits = array();
32
33     /**
34      * @var array
35      */
36     private $labels = array();
37
38     /**
39      * @var \Closure|null
40      */
41     private $errorHandler;
42
43     /**
44      * Constructor.
45      *
46      * @param Command|null $parent Parent command
47      */
48     public function __construct(Command $parent = null)
49     {
50         $this->parent = $parent;
51     }
52
53     /**
54      * Returns command as string.
55      *
56      * @return string
57      */
58     public function __toString()
59     {
60         return $this->join();
61     }
62
63     /**
64      * Creates a new Command instance.
65      *
66      * @param Command|null $parent Parent command
67      *
68      * @return self
69      */
70     public static function create(Command $parent = null)
71     {
72         return new self($parent);
73     }
74
75     /**
76      * Escapes special chars from input.
77      *
78      * @param string $input A string to escape
79      *
80      * @return string The escaped string
81      */
82     public static function escape($input)
83     {
84         return escapeshellcmd($input);
85     }
86
87     /**
88      * Quotes input.
89      *
90      * @param string $input An argument string
91      *
92      * @return string The quoted string
93      */
94     public static function quote($input)
95     {
96         return escapeshellarg($input);
97     }
98
99     /**
100      * Appends a string or a Command instance.
101      *
102      * @param string|Command $bit
103      *
104      * @return $this
105      */
106     public function add($bit)
107     {
108         $this->bits[] = $bit;
109
110         return $this;
111     }
112
113     /**
114      * Prepends a string or a command instance.
115      *
116      * @param string|Command $bit
117      *
118      * @return $this
119      */
120     public function top($bit)
121     {
122         array_unshift($this->bits, $bit);
123
124         foreach ($this->labels as $label => $index) {
125             $this->labels[$label] += 1;
126         }
127
128         return $this;
129     }
130
131     /**
132      * Appends an argument, will be quoted.
133      *
134      * @param string $arg
135      *
136      * @return $this
137      */
138     public function arg($arg)
139     {
140         $this->bits[] = self::quote($arg);
141
142         return $this;
143     }
144
145     /**
146      * Appends escaped special command chars.
147      *
148      * @param string $esc
149      *
150      * @return $this
151      */
152     public function cmd($esc)
153     {
154         $this->bits[] = self::escape($esc);
155
156         return $this;
157     }
158
159     /**
160      * Inserts a labeled command to feed later.
161      *
162      * @param string $label The unique label
163      *
164      * @return self|string
165      *
166      * @throws \RuntimeException If label already exists
167      */
168     public function ins($label)
169     {
170         if (isset($this->labels[$label])) {
171             throw new \RuntimeException(sprintf('Label "%s" already exists.', $label));
172         }
173
174         $this->bits[] = self::create($this);
175         $this->labels[$label] = count($this->bits) - 1;
176
177         return $this->bits[$this->labels[$label]];
178     }
179
180     /**
181      * Retrieves a previously labeled command.
182      *
183      * @param string $label
184      *
185      * @return self|string
186      *
187      * @throws \RuntimeException
188      */
189     public function get($label)
190     {
191         if (!isset($this->labels[$label])) {
192             throw new \RuntimeException(sprintf('Label "%s" does not exist.', $label));
193         }
194
195         return $this->bits[$this->labels[$label]];
196     }
197
198     /**
199      * Returns parent command (if any).
200      *
201      * @return self
202      *
203      * @throws \RuntimeException If command has no parent
204      */
205     public function end()
206     {
207         if (null === $this->parent) {
208             throw new \RuntimeException('Calling end on root command doesn\'t make sense.');
209         }
210
211         return $this->parent;
212     }
213
214     /**
215      * Counts bits stored in command.
216      *
217      * @return int The bits count
218      */
219     public function length()
220     {
221         return count($this->bits);
222     }
223
224     /**
225      * @param \Closure $errorHandler
226      *
227      * @return $this
228      */
229     public function setErrorHandler(\Closure $errorHandler)
230     {
231         $this->errorHandler = $errorHandler;
232
233         return $this;
234     }
235
236     /**
237      * @return \Closure|null
238      */
239     public function getErrorHandler()
240     {
241         return $this->errorHandler;
242     }
243
244     /**
245      * Executes current command.
246      *
247      * @return array The command result
248      *
249      * @throws \RuntimeException
250      */
251     public function execute()
252     {
253         if (null === $errorHandler = $this->errorHandler) {
254             exec($this->join(), $output);
255         } else {
256             $process = proc_open($this->join(), array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
257             $output = preg_split('~(\r\n|\r|\n)~', stream_get_contents($pipes[1]), -1, PREG_SPLIT_NO_EMPTY);
258
259             if ($error = stream_get_contents($pipes[2])) {
260                 $errorHandler($error);
261             }
262
263             proc_close($process);
264         }
265
266         return $output ?: array();
267     }
268
269     /**
270      * Joins bits.
271      *
272      * @return string
273      */
274     public function join()
275     {
276         return implode(' ', array_filter(
277             array_map(function ($bit) {
278                 return $bit instanceof Command ? $bit->join() : ($bit ?: null);
279             }, $this->bits),
280             function ($bit) { return null !== $bit; }
281         ));
282     }
283
284     /**
285      * Insert a string or a Command instance before the bit at given position $index (index starts from 0).
286      *
287      * @param string|Command $bit
288      * @param int            $index
289      *
290      * @return $this
291      */
292     public function addAtIndex($bit, $index)
293     {
294         array_splice($this->bits, $index, 0, $bit instanceof self ? array($bit) : $bit);
295
296         return $this;
297     }
298 }