PageRenderTime 99ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 455 lines | 227 code | 56 blank | 172 comment | 27 complexity | a4fd2803f57dd96cc37a1104aed75043 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. * @param string $name The InputOption name
  253. *
  254. * @return bool true if the InputOption object exists, false otherwise
  255. */
  256. public function hasOption($name)
  257. {
  258. return isset($this->options[$name]);
  259. }
  260. /**
  261. * Gets the array of InputOption objects.
  262. *
  263. * @return InputOption[] An array of InputOption objects
  264. */
  265. public function getOptions()
  266. {
  267. return $this->options;
  268. }
  269. /**
  270. * Returns true if an InputOption object exists by shortcut.
  271. *
  272. * @param string $name The InputOption shortcut
  273. *
  274. * @return bool true if the InputOption object exists, false otherwise
  275. */
  276. public function hasShortcut($name)
  277. {
  278. return isset($this->shortcuts[$name]);
  279. }
  280. /**
  281. * Gets an InputOption by shortcut.
  282. *
  283. * @param string $shortcut the Shortcut name
  284. *
  285. * @return InputOption An InputOption object
  286. */
  287. public function getOptionForShortcut($shortcut)
  288. {
  289. return $this->getOption($this->shortcutToName($shortcut));
  290. }
  291. /**
  292. * Gets an array of default values.
  293. *
  294. * @return array An array of all default values
  295. */
  296. public function getOptionDefaults()
  297. {
  298. $values = array();
  299. foreach ($this->options as $option) {
  300. $values[$option->getName()] = $option->getDefault();
  301. }
  302. return $values;
  303. }
  304. /**
  305. * Returns the InputOption name given a shortcut.
  306. *
  307. * @param string $shortcut The shortcut
  308. *
  309. * @return string The InputOption name
  310. *
  311. * @throws \InvalidArgumentException When option given does not exist
  312. */
  313. private function shortcutToName($shortcut)
  314. {
  315. if (!isset($this->shortcuts[$shortcut])) {
  316. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  317. }
  318. return $this->shortcuts[$shortcut];
  319. }
  320. /**
  321. * Gets the synopsis.
  322. *
  323. * @param bool $short Whether to return the short version (with options folded) or not
  324. *
  325. * @return string The synopsis
  326. */
  327. public function getSynopsis($short = false)
  328. {
  329. $elements = array();
  330. if ($short && $this->getOptions()) {
  331. $elements[] = '[options]';
  332. } elseif (!$short) {
  333. foreach ($this->getOptions() as $option) {
  334. $value = '';
  335. if ($option->acceptValue()) {
  336. $value = sprintf(
  337. ' %s%s%s',
  338. $option->isValueOptional() ? '[' : '',
  339. strtoupper($option->getName()),
  340. $option->isValueOptional() ? ']' : ''
  341. );
  342. }
  343. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  344. $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
  345. }
  346. }
  347. if (count($elements) && $this->getArguments()) {
  348. $elements[] = '[--]';
  349. }
  350. foreach ($this->getArguments() as $argument) {
  351. $element = '<'.$argument->getName().'>';
  352. if (!$argument->isRequired()) {
  353. $element = '['.$element.']';
  354. } elseif ($argument->isArray()) {
  355. $element = $element.' ('.$element.')';
  356. }
  357. if ($argument->isArray()) {
  358. $element .= '...';
  359. }
  360. $elements[] = $element;
  361. }
  362. return implode(' ', $elements);
  363. }
  364. /**
  365. * Returns a textual representation of the InputDefinition.
  366. *
  367. * @return string A string representing the InputDefinition
  368. *
  369. * @deprecated since version 2.3, to be removed in 3.0.
  370. */
  371. public function asText()
  372. {
  373. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
  374. $descriptor = new TextDescriptor();
  375. $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
  376. $descriptor->describe($output, $this, array('raw_output' => true));
  377. return $output->fetch();
  378. }
  379. /**
  380. * Returns an XML representation of the InputDefinition.
  381. *
  382. * @param bool $asDom Whether to return a DOM or an XML string
  383. *
  384. * @return string|\DOMDocument An XML string representing the InputDefinition
  385. *
  386. * @deprecated since version 2.3, to be removed in 3.0.
  387. */
  388. public function asXml($asDom = false)
  389. {
  390. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
  391. $descriptor = new XmlDescriptor();
  392. if ($asDom) {
  393. return $descriptor->getInputDefinitionDocument($this);
  394. }
  395. $output = new BufferedOutput();
  396. $descriptor->describe($output, $this);
  397. return $output->fetch();
  398. }
  399. }