Definition.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\input;
  12. class Definition
  13. {
  14. /**
  15. * @var Argument[]
  16. */
  17. private $arguments;
  18. private $requiredCount;
  19. private $hasAnArrayArgument = false;
  20. private $hasOptional;
  21. /**
  22. * @var Option[]
  23. */
  24. private $options;
  25. private $shortcuts;
  26. /**
  27. * 构造方法
  28. * @param array $definition
  29. * @api
  30. */
  31. public function __construct(array $definition = [])
  32. {
  33. $this->setDefinition($definition);
  34. }
  35. /**
  36. * 设置指令的定义
  37. * @param array $definition 定义的数组
  38. */
  39. public function setDefinition(array $definition)
  40. {
  41. $arguments = [];
  42. $options = [];
  43. foreach ($definition as $item) {
  44. if ($item instanceof Option) {
  45. $options[] = $item;
  46. } else {
  47. $arguments[] = $item;
  48. }
  49. }
  50. $this->setArguments($arguments);
  51. $this->setOptions($options);
  52. }
  53. /**
  54. * 设置参数
  55. * @param Argument[] $arguments 参数数组
  56. */
  57. public function setArguments($arguments = [])
  58. {
  59. $this->arguments = [];
  60. $this->requiredCount = 0;
  61. $this->hasOptional = false;
  62. $this->hasAnArrayArgument = false;
  63. $this->addArguments($arguments);
  64. }
  65. /**
  66. * 添加参数
  67. * @param Argument[] $arguments 参数数组
  68. * @api
  69. */
  70. public function addArguments($arguments = [])
  71. {
  72. if (null !== $arguments) {
  73. foreach ($arguments as $argument) {
  74. $this->addArgument($argument);
  75. }
  76. }
  77. }
  78. /**
  79. * 添加一个参数
  80. * @param Argument $argument 参数
  81. * @throws \LogicException
  82. */
  83. public function addArgument(Argument $argument)
  84. {
  85. if (isset($this->arguments[$argument->getName()])) {
  86. throw new \LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
  87. }
  88. if ($this->hasAnArrayArgument) {
  89. throw new \LogicException('Cannot add an argument after an array argument.');
  90. }
  91. if ($argument->isRequired() && $this->hasOptional) {
  92. throw new \LogicException('Cannot add a required argument after an optional one.');
  93. }
  94. if ($argument->isArray()) {
  95. $this->hasAnArrayArgument = true;
  96. }
  97. if ($argument->isRequired()) {
  98. ++$this->requiredCount;
  99. } else {
  100. $this->hasOptional = true;
  101. }
  102. $this->arguments[$argument->getName()] = $argument;
  103. }
  104. /**
  105. * 根据名称或者位置获取参数
  106. * @param string|int $name 参数名或者位置
  107. * @return Argument 参数
  108. * @throws \InvalidArgumentException
  109. */
  110. public function getArgument($name)
  111. {
  112. if (!$this->hasArgument($name)) {
  113. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  114. }
  115. $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
  116. return $arguments[$name];
  117. }
  118. /**
  119. * 根据名称或位置检查是否具有某个参数
  120. * @param string|int $name 参数名或者位置
  121. * @return bool
  122. * @api
  123. */
  124. public function hasArgument($name)
  125. {
  126. $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
  127. return isset($arguments[$name]);
  128. }
  129. /**
  130. * 获取所有的参数
  131. * @return Argument[] 参数数组
  132. */
  133. public function getArguments()
  134. {
  135. return $this->arguments;
  136. }
  137. /**
  138. * 获取参数数量
  139. * @return int
  140. */
  141. public function getArgumentCount()
  142. {
  143. return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
  144. }
  145. /**
  146. * 获取必填的参数的数量
  147. * @return int
  148. */
  149. public function getArgumentRequiredCount()
  150. {
  151. return $this->requiredCount;
  152. }
  153. /**
  154. * 获取参数默认值
  155. * @return array
  156. */
  157. public function getArgumentDefaults()
  158. {
  159. $values = [];
  160. foreach ($this->arguments as $argument) {
  161. $values[$argument->getName()] = $argument->getDefault();
  162. }
  163. return $values;
  164. }
  165. /**
  166. * 设置选项
  167. * @param Option[] $options 选项数组
  168. */
  169. public function setOptions($options = [])
  170. {
  171. $this->options = [];
  172. $this->shortcuts = [];
  173. $this->addOptions($options);
  174. }
  175. /**
  176. * 添加选项
  177. * @param Option[] $options 选项数组
  178. * @api
  179. */
  180. public function addOptions($options = [])
  181. {
  182. foreach ($options as $option) {
  183. $this->addOption($option);
  184. }
  185. }
  186. /**
  187. * 添加一个选项
  188. * @param Option $option 选项
  189. * @throws \LogicException
  190. * @api
  191. */
  192. public function addOption(Option $option)
  193. {
  194. if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
  195. throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
  196. }
  197. if ($option->getShortcut()) {
  198. foreach (explode('|', $option->getShortcut()) as $shortcut) {
  199. if (isset($this->shortcuts[$shortcut])
  200. && !$option->equals($this->options[$this->shortcuts[$shortcut]])
  201. ) {
  202. throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
  203. }
  204. }
  205. }
  206. $this->options[$option->getName()] = $option;
  207. if ($option->getShortcut()) {
  208. foreach (explode('|', $option->getShortcut()) as $shortcut) {
  209. $this->shortcuts[$shortcut] = $option->getName();
  210. }
  211. }
  212. }
  213. /**
  214. * 根据名称获取选项
  215. * @param string $name 选项名
  216. * @return Option
  217. * @throws \InvalidArgumentException
  218. * @api
  219. */
  220. public function getOption($name)
  221. {
  222. if (!$this->hasOption($name)) {
  223. throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  224. }
  225. return $this->options[$name];
  226. }
  227. /**
  228. * 根据名称检查是否有这个选项
  229. * @param string $name 选项名
  230. * @return bool
  231. * @api
  232. */
  233. public function hasOption($name)
  234. {
  235. return isset($this->options[$name]);
  236. }
  237. /**
  238. * 获取所有选项
  239. * @return Option[]
  240. * @api
  241. */
  242. public function getOptions()
  243. {
  244. return $this->options;
  245. }
  246. /**
  247. * 根据名称检查某个选项是否有短名称
  248. * @param string $name 短名称
  249. * @return bool
  250. */
  251. public function hasShortcut($name)
  252. {
  253. return isset($this->shortcuts[$name]);
  254. }
  255. /**
  256. * 根据短名称获取选项
  257. * @param string $shortcut 短名称
  258. * @return Option
  259. */
  260. public function getOptionForShortcut($shortcut)
  261. {
  262. return $this->getOption($this->shortcutToName($shortcut));
  263. }
  264. /**
  265. * 获取所有选项的默认值
  266. * @return array
  267. */
  268. public function getOptionDefaults()
  269. {
  270. $values = [];
  271. foreach ($this->options as $option) {
  272. $values[$option->getName()] = $option->getDefault();
  273. }
  274. return $values;
  275. }
  276. /**
  277. * 根据短名称获取选项名
  278. * @param string $shortcut 短名称
  279. * @return string
  280. * @throws \InvalidArgumentException
  281. */
  282. private function shortcutToName($shortcut)
  283. {
  284. if (!isset($this->shortcuts[$shortcut])) {
  285. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  286. }
  287. return $this->shortcuts[$shortcut];
  288. }
  289. /**
  290. * 获取该指令的介绍
  291. * @param bool $short 是否简洁介绍
  292. * @return string
  293. */
  294. public function getSynopsis($short = false)
  295. {
  296. $elements = [];
  297. if ($short && $this->getOptions()) {
  298. $elements[] = '[options]';
  299. } elseif (!$short) {
  300. foreach ($this->getOptions() as $option) {
  301. $value = '';
  302. if ($option->acceptValue()) {
  303. $value = sprintf(' %s%s%s', $option->isValueOptional() ? '[' : '', strtoupper($option->getName()), $option->isValueOptional() ? ']' : '');
  304. }
  305. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  306. $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
  307. }
  308. }
  309. if (count($elements) && $this->getArguments()) {
  310. $elements[] = '[--]';
  311. }
  312. foreach ($this->getArguments() as $argument) {
  313. $element = '<' . $argument->getName() . '>';
  314. if (!$argument->isRequired()) {
  315. $element = '[' . $element . ']';
  316. } elseif ($argument->isArray()) {
  317. $element .= ' (' . $element . ')';
  318. }
  319. if ($argument->isArray()) {
  320. $element .= '...';
  321. }
  322. $elements[] = $element;
  323. }
  324. return implode(' ', $elements);
  325. }
  326. }