InputDefinition.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. namespace Symfony\Component\Console\Input;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * A InputDefinition represents a set of valid command line arguments and options.
  13. *
  14. * Usage:
  15. *
  16. * $definition = new InputDefinition(array(
  17. * new InputArgument('name', InputArgument::REQUIRED),
  18. * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
  19. * ));
  20. *
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. */
  23. class InputDefinition
  24. {
  25. protected $arguments;
  26. protected $requiredCount;
  27. protected $hasAnArrayArgument = false;
  28. protected $hasOptional;
  29. protected $options;
  30. protected $shortcuts;
  31. /**
  32. * Constructor.
  33. *
  34. * @param array $definition An array of InputArgument and InputOption instance
  35. */
  36. public function __construct(array $definition = array())
  37. {
  38. $this->setDefinition($definition);
  39. }
  40. public function setDefinition(array $definition)
  41. {
  42. $arguments = array();
  43. $options = array();
  44. foreach ($definition as $item) {
  45. if ($item instanceof InputOption) {
  46. $options[] = $item;
  47. } else {
  48. $arguments[] = $item;
  49. }
  50. }
  51. $this->setArguments($arguments);
  52. $this->setOptions($options);
  53. }
  54. /**
  55. * Sets the InputArgument objects.
  56. *
  57. * @param array $arguments An array of InputArgument objects
  58. */
  59. public function setArguments($arguments = array())
  60. {
  61. $this->arguments = array();
  62. $this->requiredCount = 0;
  63. $this->hasOptional = false;
  64. $this->hasAnArrayArgument = false;
  65. $this->addArguments($arguments);
  66. }
  67. /**
  68. * Add an array of InputArgument objects.
  69. *
  70. * @param InputArgument[] $arguments An array of InputArgument objects
  71. */
  72. public function addArguments($arguments = array())
  73. {
  74. if (null !== $arguments) {
  75. foreach ($arguments as $argument) {
  76. $this->addArgument($argument);
  77. }
  78. }
  79. }
  80. /**
  81. * Add an InputArgument object.
  82. *
  83. * @param InputArgument $argument An InputArgument object
  84. *
  85. * @throws \LogicException When incorrect argument is given
  86. */
  87. public function addArgument(InputArgument $argument)
  88. {
  89. if (isset($this->arguments[$argument->getName()])) {
  90. throw new \LogicException(sprintf('An argument with name "%s" already exist.', $argument->getName()));
  91. }
  92. if ($this->hasAnArrayArgument) {
  93. throw new \LogicException('Cannot add an argument after an array argument.');
  94. }
  95. if ($argument->isRequired() && $this->hasOptional) {
  96. throw new \LogicException('Cannot add a required argument after an optional one.');
  97. }
  98. if ($argument->isArray()) {
  99. $this->hasAnArrayArgument = true;
  100. }
  101. if ($argument->isRequired()) {
  102. ++$this->requiredCount;
  103. } else {
  104. $this->hasOptional = true;
  105. }
  106. $this->arguments[$argument->getName()] = $argument;
  107. }
  108. /**
  109. * Returns an InputArgument by name or by position.
  110. *
  111. * @param string|integer $name The InputArgument name or position
  112. *
  113. * @return InputArgument An InputArgument object
  114. *
  115. * @throws \InvalidArgumentException When argument given doesn't exist
  116. */
  117. public function getArgument($name)
  118. {
  119. $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
  120. if (!$this->hasArgument($name)) {
  121. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  122. }
  123. return $arguments[$name];
  124. }
  125. /**
  126. * Returns true if an InputArgument object exists by name or position.
  127. *
  128. * @param string|integer $name The InputArgument name or position
  129. *
  130. * @return Boolean true if the InputArgument object exists, false otherwise
  131. */
  132. public function hasArgument($name)
  133. {
  134. $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
  135. return isset($arguments[$name]);
  136. }
  137. /**
  138. * Gets the array of InputArgument objects.
  139. *
  140. * @return array An array of InputArgument objects
  141. */
  142. public function getArguments()
  143. {
  144. return $this->arguments;
  145. }
  146. /**
  147. * Returns the number of InputArguments.
  148. *
  149. * @return integer The number of InputArguments
  150. */
  151. public function getArgumentCount()
  152. {
  153. return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
  154. }
  155. /**
  156. * Returns the number of required InputArguments.
  157. *
  158. * @return integer The number of required InputArguments
  159. */
  160. public function getArgumentRequiredCount()
  161. {
  162. return $this->requiredCount;
  163. }
  164. /**
  165. * Gets the default values.
  166. *
  167. * @return array An array of default values
  168. */
  169. public function getArgumentDefaults()
  170. {
  171. $values = array();
  172. foreach ($this->arguments as $argument) {
  173. $values[$argument->getName()] = $argument->getDefault();
  174. }
  175. return $values;
  176. }
  177. /**
  178. * Sets the InputOption objects.
  179. *
  180. * @param array $options An array of InputOption objects
  181. */
  182. public function setOptions($options = array())
  183. {
  184. $this->options = array();
  185. $this->shortcuts = array();
  186. $this->addOptions($options);
  187. }
  188. /**
  189. * Add an array of InputOption objects.
  190. *
  191. * @param InputOption[] $options An array of InputOption objects
  192. */
  193. public function addOptions($options = array())
  194. {
  195. foreach ($options as $option) {
  196. $this->addOption($option);
  197. }
  198. }
  199. /**
  200. * Add an InputOption object.
  201. *
  202. * @param InputOption $option An InputOption object
  203. *
  204. * @throws \LogicException When option given already exist
  205. */
  206. public function addOption(InputOption $option)
  207. {
  208. if (isset($this->options[$option->getName()])) {
  209. throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName()));
  210. } else if (isset($this->shortcuts[$option->getShortcut()])) {
  211. throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut()));
  212. }
  213. $this->options[$option->getName()] = $option;
  214. if ($option->getShortcut()) {
  215. $this->shortcuts[$option->getShortcut()] = $option->getName();
  216. }
  217. }
  218. /**
  219. * Returns an InputOption by name.
  220. *
  221. * @param string $name The InputOption name
  222. *
  223. * @return InputOption A InputOption object
  224. */
  225. public function getOption($name)
  226. {
  227. if (!$this->hasOption($name)) {
  228. throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  229. }
  230. return $this->options[$name];
  231. }
  232. /**
  233. * Returns true if an InputOption object exists by name.
  234. *
  235. * @param string $name The InputOption name
  236. *
  237. * @return Boolean true if the InputOption object exists, false otherwise
  238. */
  239. public function hasOption($name)
  240. {
  241. return isset($this->options[$name]);
  242. }
  243. /**
  244. * Gets the array of InputOption objects.
  245. *
  246. * @return array An array of InputOption objects
  247. */
  248. public function getOptions()
  249. {
  250. return $this->options;
  251. }
  252. /**
  253. * Returns true if an InputOption object exists by shortcut.
  254. *
  255. * @param string $name The InputOption shortcut
  256. *
  257. * @return Boolean true if the InputOption object exists, false otherwise
  258. */
  259. public function hasShortcut($name)
  260. {
  261. return isset($this->shortcuts[$name]);
  262. }
  263. /**
  264. * Gets an InputOption by shortcut.
  265. *
  266. * @return InputOption An InputOption object
  267. */
  268. public function getOptionForShortcut($shortcut)
  269. {
  270. return $this->getOption($this->shortcutToName($shortcut));
  271. }
  272. /**
  273. * Gets an array of default values.
  274. *
  275. * @return array An array of all default values
  276. */
  277. public function getOptionDefaults()
  278. {
  279. $values = array();
  280. foreach ($this->options as $option) {
  281. $values[$option->getName()] = $option->getDefault();
  282. }
  283. return $values;
  284. }
  285. /**
  286. * Returns the InputOption name given a shortcut.
  287. *
  288. * @param string $shortcut The shortcut
  289. *
  290. * @return string The InputOption name
  291. *
  292. * @throws \InvalidArgumentException When option given does not exist
  293. */
  294. protected function shortcutToName($shortcut)
  295. {
  296. if (!isset($this->shortcuts[$shortcut])) {
  297. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  298. }
  299. return $this->shortcuts[$shortcut];
  300. }
  301. /**
  302. * Gets the synopsis.
  303. *
  304. * @return string The synopsis
  305. */
  306. public function getSynopsis()
  307. {
  308. $elements = array();
  309. foreach ($this->getOptions() as $option) {
  310. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  311. $elements[] = sprintf('['.($option->isValueRequired() ? '%s--%s="..."' : ($option->isValueOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName());
  312. }
  313. foreach ($this->getArguments() as $argument) {
  314. $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : ''));
  315. if ($argument->isArray()) {
  316. $elements[] = sprintf('... [%sN]', $argument->getName());
  317. }
  318. }
  319. return implode(' ', $elements);
  320. }
  321. /**
  322. * Returns a textual representation of the InputDefinition.
  323. *
  324. * @return string A string representing the InputDefinition
  325. */
  326. public function asText()
  327. {
  328. // find the largest option or argument name
  329. $max = 0;
  330. foreach ($this->getOptions() as $option) {
  331. $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
  332. }
  333. foreach ($this->getArguments() as $argument) {
  334. $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
  335. }
  336. ++$max;
  337. $text = array();
  338. if ($this->getArguments()) {
  339. $text[] = '<comment>Arguments:</comment>';
  340. foreach ($this->getArguments() as $argument) {
  341. if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
  342. $default = sprintf('<comment> (default: %s)</comment>', is_array($argument->getDefault()) ? str_replace("\n", '', var_export($argument->getDefault(), true)): $argument->getDefault());
  343. } else {
  344. $default = '';
  345. }
  346. $text[] = sprintf(" <info>%-${max}s</info> %s%s", $argument->getName(), $argument->getDescription(), $default);
  347. }
  348. $text[] = '';
  349. }
  350. if ($this->getOptions()) {
  351. $text[] = '<comment>Options:</comment>';
  352. foreach ($this->getOptions() as $option) {
  353. if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
  354. $default = sprintf('<comment> (default: %s)</comment>', is_array($option->getDefault()) ? str_replace("\n", '', print_r($option->getDefault(), true)): $option->getDefault());
  355. } else {
  356. $default = '';
  357. }
  358. $multiple = $option->isArray() ? '<comment> (multiple values allowed)</comment>' : '';
  359. $text[] = sprintf(' %-'.$max.'s %s%s%s%s', '<info>--'.$option->getName().'</info>', $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getDescription(), $default, $multiple);
  360. }
  361. $text[] = '';
  362. }
  363. return implode("\n", $text);
  364. }
  365. /**
  366. * Returns an XML representation of the InputDefinition.
  367. *
  368. * @param Boolean $asDom Whether to return a DOM or an XML string
  369. *
  370. * @return string|DOMDocument An XML string representing the InputDefinition
  371. */
  372. public function asXml($asDom = false)
  373. {
  374. $dom = new \DOMDocument('1.0', 'UTF-8');
  375. $dom->formatOutput = true;
  376. $dom->appendChild($definitionXML = $dom->createElement('definition'));
  377. $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
  378. foreach ($this->getArguments() as $argument) {
  379. $argumentsXML->appendChild($argumentXML = $dom->createElement('argument'));
  380. $argumentXML->setAttribute('name', $argument->getName());
  381. $argumentXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
  382. $argumentXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
  383. $argumentXML->appendChild($descriptionXML = $dom->createElement('description'));
  384. $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
  385. $argumentXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  386. $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : ($argument->getDefault() ? array($argument->getDefault()) : array());
  387. foreach ($defaults as $default) {
  388. $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  389. $defaultXML->appendChild($dom->createTextNode($default));
  390. }
  391. }
  392. $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
  393. foreach ($this->getOptions() as $option) {
  394. $optionsXML->appendChild($optionXML = $dom->createElement('option'));
  395. $optionXML->setAttribute('name', '--'.$option->getName());
  396. $optionXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
  397. $optionXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
  398. $optionXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
  399. $optionXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
  400. $optionXML->appendChild($descriptionXML = $dom->createElement('description'));
  401. $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
  402. if ($option->acceptValue()) {
  403. $optionXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  404. $defaults = is_array($option->getDefault()) ? $option->getDefault() : ($option->getDefault() ? array($option->getDefault()) : array());
  405. foreach ($defaults as $default) {
  406. $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  407. $defaultXML->appendChild($dom->createTextNode($default));
  408. }
  409. }
  410. }
  411. return $asDom ? $dom : $dom->saveXml();
  412. }
  413. }