Command.php 5.8 KB

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