Command.php 6.2 KB

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