InputDefinition.php 13 KB

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