PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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