PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/console/Symfony/Component/Console/Input/InputDefinition.php

https://gitlab.com/techniconline/kmc
PHP | 453 lines | 203 code | 50 blank | 200 comment | 21 complexity | f083cf523952103cfbe80d8f7911f5c7 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. * @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. * @return string The synopsis
  354. */
  355. public function getSynopsis()
  356. {
  357. $elements = array();
  358. foreach ($this->getOptions() as $option) {
  359. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  360. $elements[] = sprintf('[' . ($option->isValueRequired() ? '%s--%s="..."' : ($option->isValueOptional() ? '%s--%s[="..."]' : '%s--%s')) . ']', $shortcut, $option->getName());
  361. }
  362. foreach ($this->getArguments() as $argument) {
  363. $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName() . ($argument->isArray() ? '1' : ''));
  364. if ($argument->isArray()) {
  365. $elements[] = sprintf('... [%sN]', $argument->getName());
  366. }
  367. }
  368. return implode(' ', $elements);
  369. }
  370. /**
  371. * Returns a textual representation of the InputDefinition.
  372. *
  373. * @return string A string representing the InputDefinition
  374. *
  375. * @deprecated Deprecated since version 2.3, to be removed in 3.0.
  376. */
  377. public function asText()
  378. {
  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 Deprecated since version 2.3, to be removed in 3.0.
  392. */
  393. public function asXml($asDom = false)
  394. {
  395. $descriptor = new XmlDescriptor();
  396. if ($asDom) {
  397. return $descriptor->getInputDefinitionDocument($this);
  398. }
  399. $output = new BufferedOutput();
  400. $descriptor->describe($output, $this);
  401. return $output->fetch();
  402. }
  403. }