InputDefinition.php 13 KB

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