Drupal investigation

Command.php 6.1KB

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