No Description

Command.php 6.1KB

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