/vendor/symfony/console/Input/InputDefinition.php

https://gitlab.com/gideonmarked/PLCPortal · PHP · 458 lines · 227 code · 56 blank · 175 comment · 27 complexity · cf415ce9acbde7ffa7b23abe96c85965 MD5 · raw file

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