Console.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TopThink [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2015 http://www.topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: zhangyajun <448901948@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace think;
  10. use think\console\Command;
  11. use think\console\command\Help as HelpCommand;
  12. use think\console\Input;
  13. use think\console\input\Argument as InputArgument;
  14. use think\console\input\Definition as InputDefinition;
  15. use think\console\input\Option as InputOption;
  16. use think\console\Output;
  17. use think\console\output\driver\Buffer;
  18. class Console
  19. {
  20. private $name;
  21. private $version;
  22. /** @var Command[] */
  23. private $commands = [];
  24. private $wantHelps = false;
  25. private $catchExceptions = true;
  26. private $autoExit = true;
  27. private $definition;
  28. private $defaultCommand;
  29. private static $defaultCommands = [
  30. 'help' => "think\\console\\command\\Help",
  31. 'list' => "think\\console\\command\\Lists",
  32. 'build' => "think\\console\\command\\Build",
  33. 'clear' => "think\\console\\command\\Clear",
  34. 'make:command' => "think\\console\\command\\make\\Command",
  35. 'make:controller' => "think\\console\\command\\make\\Controller",
  36. 'make:model' => "think\\console\\command\\make\\Model",
  37. 'make:middleware' => "think\\console\\command\\make\\Middleware",
  38. 'make:validate' => "think\\console\\command\\make\\Validate",
  39. 'optimize:autoload' => "think\\console\\command\\optimize\\Autoload",
  40. 'optimize:config' => "think\\console\\command\\optimize\\Config",
  41. 'optimize:schema' => "think\\console\\command\\optimize\\Schema",
  42. 'optimize:route' => "think\\console\\command\\optimize\\Route",
  43. 'run' => "think\\console\\command\\RunServer",
  44. 'version' => "think\\console\\command\\Version",
  45. 'route:list' => "think\\console\\command\\RouteList",
  46. ];
  47. /**
  48. * Console constructor.
  49. * @access public
  50. * @param string $name 名称
  51. * @param string $version 版本
  52. * @param null|string $user 执行用户
  53. */
  54. public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', $user = null)
  55. {
  56. $this->name = $name;
  57. $this->version = $version;
  58. if ($user) {
  59. $this->setUser($user);
  60. }
  61. $this->defaultCommand = 'list';
  62. $this->definition = $this->getDefaultInputDefinition();
  63. }
  64. /**
  65. * 设置执行用户
  66. * @param $user
  67. */
  68. public function setUser($user)
  69. {
  70. if (DIRECTORY_SEPARATOR == '\\') {
  71. return;
  72. }
  73. $user = posix_getpwnam($user);
  74. if ($user) {
  75. posix_setuid($user['uid']);
  76. posix_setgid($user['gid']);
  77. }
  78. }
  79. /**
  80. * 初始化 Console
  81. * @access public
  82. * @param bool $run 是否运行 Console
  83. * @return int|Console
  84. */
  85. public static function init($run = true)
  86. {
  87. static $console;
  88. if (!$console) {
  89. $config = Container::get('config')->pull('console');
  90. $console = new self($config['name'], $config['version'], $config['user']);
  91. $commands = $console->getDefinedCommands($config);
  92. // 添加指令集
  93. $console->addCommands($commands);
  94. }
  95. if ($run) {
  96. // 运行
  97. return $console->run();
  98. } else {
  99. return $console;
  100. }
  101. }
  102. /**
  103. * @access public
  104. * @param array $config
  105. * @return array
  106. */
  107. public function getDefinedCommands(array $config = [])
  108. {
  109. $commands = self::$defaultCommands;
  110. if (!empty($config['auto_path']) && is_dir($config['auto_path'])) {
  111. // 自动加载指令类
  112. $files = scandir($config['auto_path']);
  113. if (count($files) > 2) {
  114. $beforeClass = get_declared_classes();
  115. foreach ($files as $file) {
  116. if (pathinfo($file, PATHINFO_EXTENSION) == 'php') {
  117. include $config['auto_path'] . $file;
  118. }
  119. }
  120. $afterClass = get_declared_classes();
  121. $commands = array_merge($commands, array_diff($afterClass, $beforeClass));
  122. }
  123. }
  124. $file = Container::get('env')->get('app_path') . 'command.php';
  125. if (is_file($file)) {
  126. $appCommands = include $file;
  127. if (is_array($appCommands)) {
  128. $commands = array_merge($commands, $appCommands);
  129. }
  130. }
  131. return $commands;
  132. }
  133. /**
  134. * @access public
  135. * @param string $command
  136. * @param array $parameters
  137. * @param string $driver
  138. * @return Output|Buffer
  139. */
  140. public static function call($command, array $parameters = [], $driver = 'buffer')
  141. {
  142. $console = self::init(false);
  143. array_unshift($parameters, $command);
  144. $input = new Input($parameters);
  145. $output = new Output($driver);
  146. $console->setCatchExceptions(false);
  147. $console->find($command)->run($input, $output);
  148. return $output;
  149. }
  150. /**
  151. * 执行当前的指令
  152. * @access public
  153. * @return int
  154. * @throws \Exception
  155. * @api
  156. */
  157. public function run()
  158. {
  159. $input = new Input();
  160. $output = new Output();
  161. $this->configureIO($input, $output);
  162. try {
  163. $exitCode = $this->doRun($input, $output);
  164. } catch (\Exception $e) {
  165. if (!$this->catchExceptions) {
  166. throw $e;
  167. }
  168. $output->renderException($e);
  169. $exitCode = $e->getCode();
  170. if (is_numeric($exitCode)) {
  171. $exitCode = (int) $exitCode;
  172. if (0 === $exitCode) {
  173. $exitCode = 1;
  174. }
  175. } else {
  176. $exitCode = 1;
  177. }
  178. }
  179. if ($this->autoExit) {
  180. if ($exitCode > 255) {
  181. $exitCode = 255;
  182. }
  183. exit($exitCode);
  184. }
  185. return $exitCode;
  186. }
  187. /**
  188. * 执行指令
  189. * @access public
  190. * @param Input $input
  191. * @param Output $output
  192. * @return int
  193. */
  194. public function doRun(Input $input, Output $output)
  195. {
  196. if (true === $input->hasParameterOption(['--version', '-V'])) {
  197. $output->writeln($this->getLongVersion());
  198. return 0;
  199. }
  200. $name = $this->getCommandName($input);
  201. if (true === $input->hasParameterOption(['--help', '-h'])) {
  202. if (!$name) {
  203. $name = 'help';
  204. $input = new Input(['help']);
  205. } else {
  206. $this->wantHelps = true;
  207. }
  208. }
  209. if (!$name) {
  210. $name = $this->defaultCommand;
  211. $input = new Input([$this->defaultCommand]);
  212. }
  213. $command = $this->find($name);
  214. $exitCode = $this->doRunCommand($command, $input, $output);
  215. return $exitCode;
  216. }
  217. /**
  218. * 设置输入参数定义
  219. * @access public
  220. * @param InputDefinition $definition
  221. */
  222. public function setDefinition(InputDefinition $definition)
  223. {
  224. $this->definition = $definition;
  225. }
  226. /**
  227. * 获取输入参数定义
  228. * @access public
  229. * @return InputDefinition The InputDefinition instance
  230. */
  231. public function getDefinition()
  232. {
  233. return $this->definition;
  234. }
  235. /**
  236. * Gets the help message.
  237. * @access public
  238. * @return string A help message.
  239. */
  240. public function getHelp()
  241. {
  242. return $this->getLongVersion();
  243. }
  244. /**
  245. * 是否捕获异常
  246. * @access public
  247. * @param bool $boolean
  248. * @api
  249. */
  250. public function setCatchExceptions($boolean)
  251. {
  252. $this->catchExceptions = (bool) $boolean;
  253. }
  254. /**
  255. * 是否自动退出
  256. * @access public
  257. * @param bool $boolean
  258. * @api
  259. */
  260. public function setAutoExit($boolean)
  261. {
  262. $this->autoExit = (bool) $boolean;
  263. }
  264. /**
  265. * 获取名称
  266. * @access public
  267. * @return string
  268. */
  269. public function getName()
  270. {
  271. return $this->name;
  272. }
  273. /**
  274. * 设置名称
  275. * @access public
  276. * @param string $name
  277. */
  278. public function setName($name)
  279. {
  280. $this->name = $name;
  281. }
  282. /**
  283. * 获取版本
  284. * @access public
  285. * @return string
  286. * @api
  287. */
  288. public function getVersion()
  289. {
  290. return $this->version;
  291. }
  292. /**
  293. * 设置版本
  294. * @access public
  295. * @param string $version
  296. */
  297. public function setVersion($version)
  298. {
  299. $this->version = $version;
  300. }
  301. /**
  302. * 获取完整的版本号
  303. * @access public
  304. * @return string
  305. */
  306. public function getLongVersion()
  307. {
  308. if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
  309. return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
  310. }
  311. return '<info>Console Tool</info>';
  312. }
  313. /**
  314. * 注册一个指令 (便于动态创建指令)
  315. * @access public
  316. * @param string $name 指令名
  317. * @return Command
  318. */
  319. public function register($name)
  320. {
  321. return $this->add(new Command($name));
  322. }
  323. /**
  324. * 添加指令集
  325. * @access public
  326. * @param array $commands
  327. */
  328. public function addCommands(array $commands)
  329. {
  330. foreach ($commands as $key => $command) {
  331. if (is_subclass_of($command, "\\think\\console\\Command")) {
  332. // 注册指令
  333. $this->add($command, is_numeric($key) ? '' : $key);
  334. }
  335. }
  336. }
  337. /**
  338. * 注册一个指令(对象)
  339. * @access public
  340. * @param mixed $command 指令对象或者指令类名
  341. * @param string $name 指令名 留空则自动获取
  342. * @return mixed
  343. */
  344. public function add($command, $name)
  345. {
  346. if ($name) {
  347. $this->commands[$name] = $command;
  348. return;
  349. }
  350. if (is_string($command)) {
  351. $command = new $command();
  352. }
  353. $command->setConsole($this);
  354. if (!$command->isEnabled()) {
  355. $command->setConsole(null);
  356. return;
  357. }
  358. if (null === $command->getDefinition()) {
  359. throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
  360. }
  361. $this->commands[$command->getName()] = $command;
  362. foreach ($command->getAliases() as $alias) {
  363. $this->commands[$alias] = $command;
  364. }
  365. return $command;
  366. }
  367. /**
  368. * 获取指令
  369. * @access public
  370. * @param string $name 指令名称
  371. * @return Command
  372. * @throws \InvalidArgumentException
  373. */
  374. public function get($name)
  375. {
  376. if (!isset($this->commands[$name])) {
  377. throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
  378. }
  379. $command = $this->commands[$name];
  380. if (is_string($command)) {
  381. $command = new $command();
  382. }
  383. $command->setConsole($this);
  384. if ($this->wantHelps) {
  385. $this->wantHelps = false;
  386. /** @var HelpCommand $helpCommand */
  387. $helpCommand = $this->get('help');
  388. $helpCommand->setCommand($command);
  389. return $helpCommand;
  390. }
  391. return $command;
  392. }
  393. /**
  394. * 某个指令是否存在
  395. * @access public
  396. * @param string $name 指令名称
  397. * @return bool
  398. */
  399. public function has($name)
  400. {
  401. return isset($this->commands[$name]);
  402. }
  403. /**
  404. * 获取所有的命名空间
  405. * @access public
  406. * @return array
  407. */
  408. public function getNamespaces()
  409. {
  410. $namespaces = [];
  411. foreach ($this->commands as $name => $command) {
  412. if (is_string($command)) {
  413. $namespaces = array_merge($namespaces, $this->extractAllNamespaces($name));
  414. } else {
  415. $namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
  416. foreach ($command->getAliases() as $alias) {
  417. $namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
  418. }
  419. }
  420. }
  421. return array_values(array_unique(array_filter($namespaces)));
  422. }
  423. /**
  424. * 查找注册命名空间中的名称或缩写。
  425. * @access public
  426. * @param string $namespace
  427. * @return string
  428. * @throws \InvalidArgumentException
  429. */
  430. public function findNamespace($namespace)
  431. {
  432. $allNamespaces = $this->getNamespaces();
  433. $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
  434. return preg_quote($matches[1]) . '[^:]*';
  435. }, $namespace);
  436. $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
  437. if (empty($namespaces)) {
  438. $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
  439. if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {
  440. if (1 == count($alternatives)) {
  441. $message .= "\n\nDid you mean this?\n ";
  442. } else {
  443. $message .= "\n\nDid you mean one of these?\n ";
  444. }
  445. $message .= implode("\n ", $alternatives);
  446. }
  447. throw new \InvalidArgumentException($message);
  448. }
  449. $exact = in_array($namespace, $namespaces, true);
  450. if (count($namespaces) > 1 && !$exact) {
  451. throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))));
  452. }
  453. return $exact ? $namespace : reset($namespaces);
  454. }
  455. /**
  456. * 查找指令
  457. * @access public
  458. * @param string $name 名称或者别名
  459. * @return Command
  460. * @throws \InvalidArgumentException
  461. */
  462. public function find($name)
  463. {
  464. $allCommands = array_keys($this->commands);
  465. $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
  466. return preg_quote($matches[1]) . '[^:]*';
  467. }, $name);
  468. $commands = preg_grep('{^' . $expr . '}', $allCommands);
  469. if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
  470. if (false !== $pos = strrpos($name, ':')) {
  471. $this->findNamespace(substr($name, 0, $pos));
  472. }
  473. $message = sprintf('Command "%s" is not defined.', $name);
  474. if ($alternatives = $this->findAlternatives($name, $allCommands)) {
  475. if (1 == count($alternatives)) {
  476. $message .= "\n\nDid you mean this?\n ";
  477. } else {
  478. $message .= "\n\nDid you mean one of these?\n ";
  479. }
  480. $message .= implode("\n ", $alternatives);
  481. }
  482. throw new \InvalidArgumentException($message);
  483. }
  484. $exact = in_array($name, $commands, true);
  485. if (count($commands) > 1 && !$exact) {
  486. $suggestions = $this->getAbbreviationSuggestions(array_values($commands));
  487. throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));
  488. }
  489. return $this->get($exact ? $name : reset($commands));
  490. }
  491. /**
  492. * 获取所有的指令
  493. * @access public
  494. * @param string $namespace 命名空间
  495. * @return Command[]
  496. * @api
  497. */
  498. public function all($namespace = null)
  499. {
  500. if (null === $namespace) {
  501. return $this->commands;
  502. }
  503. $commands = [];
  504. foreach ($this->commands as $name => $command) {
  505. if ($this->extractNamespace($name, substr_count($namespace, ':') + 1) === $namespace) {
  506. $commands[$name] = $command;
  507. }
  508. }
  509. return $commands;
  510. }
  511. /**
  512. * 获取可能的指令名
  513. * @access public
  514. * @param array $names
  515. * @return array
  516. */
  517. public static function getAbbreviations($names)
  518. {
  519. $abbrevs = [];
  520. foreach ($names as $name) {
  521. for ($len = strlen($name); $len > 0; --$len) {
  522. $abbrev = substr($name, 0, $len);
  523. $abbrevs[$abbrev][] = $name;
  524. }
  525. }
  526. return $abbrevs;
  527. }
  528. /**
  529. * 配置基于用户的参数和选项的输入和输出实例。
  530. * @access protected
  531. * @param Input $input 输入实例
  532. * @param Output $output 输出实例
  533. */
  534. protected function configureIO(Input $input, Output $output)
  535. {
  536. if (true === $input->hasParameterOption(['--ansi'])) {
  537. $output->setDecorated(true);
  538. } elseif (true === $input->hasParameterOption(['--no-ansi'])) {
  539. $output->setDecorated(false);
  540. }
  541. if (true === $input->hasParameterOption(['--no-interaction', '-n'])) {
  542. $input->setInteractive(false);
  543. }
  544. if (true === $input->hasParameterOption(['--quiet', '-q'])) {
  545. $output->setVerbosity(Output::VERBOSITY_QUIET);
  546. } else {
  547. if ($input->hasParameterOption('-vvv') || $input->hasParameterOption('--verbose=3') || $input->getParameterOption('--verbose') === 3) {
  548. $output->setVerbosity(Output::VERBOSITY_DEBUG);
  549. } elseif ($input->hasParameterOption('-vv') || $input->hasParameterOption('--verbose=2') || $input->getParameterOption('--verbose') === 2) {
  550. $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE);
  551. } elseif ($input->hasParameterOption('-v') || $input->hasParameterOption('--verbose=1') || $input->hasParameterOption('--verbose') || $input->getParameterOption('--verbose')) {
  552. $output->setVerbosity(Output::VERBOSITY_VERBOSE);
  553. }
  554. }
  555. }
  556. /**
  557. * 执行指令
  558. * @access protected
  559. * @param Command $command 指令实例
  560. * @param Input $input 输入实例
  561. * @param Output $output 输出实例
  562. * @return int
  563. * @throws \Exception
  564. */
  565. protected function doRunCommand(Command $command, Input $input, Output $output)
  566. {
  567. return $command->run($input, $output);
  568. }
  569. /**
  570. * 获取指令的基础名称
  571. * @access protected
  572. * @param Input $input
  573. * @return string
  574. */
  575. protected function getCommandName(Input $input)
  576. {
  577. return $input->getFirstArgument();
  578. }
  579. /**
  580. * 获取默认输入定义
  581. * @access protected
  582. * @return InputDefinition
  583. */
  584. protected function getDefaultInputDefinition()
  585. {
  586. return new InputDefinition([
  587. new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
  588. new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
  589. new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this console version'),
  590. new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
  591. new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
  592. new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
  593. new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
  594. new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
  595. ]);
  596. }
  597. public static function addDefaultCommands(array $classnames)
  598. {
  599. self::$defaultCommands = array_merge(self::$defaultCommands, $classnames);
  600. }
  601. /**
  602. * 获取可能的建议
  603. * @access private
  604. * @param array $abbrevs
  605. * @return string
  606. */
  607. private function getAbbreviationSuggestions($abbrevs)
  608. {
  609. return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
  610. }
  611. /**
  612. * 返回命名空间部分
  613. * @access public
  614. * @param string $name 指令
  615. * @param string $limit 部分的命名空间的最大数量
  616. * @return string
  617. */
  618. public function extractNamespace($name, $limit = null)
  619. {
  620. $parts = explode(':', $name);
  621. array_pop($parts);
  622. return implode(':', null === $limit ? $parts : array_slice($parts, 0, $limit));
  623. }
  624. /**
  625. * 查找可替代的建议
  626. * @access private
  627. * @param string $name
  628. * @param array|\Traversable $collection
  629. * @return array
  630. */
  631. private function findAlternatives($name, $collection)
  632. {
  633. $threshold = 1e3;
  634. $alternatives = [];
  635. $collectionParts = [];
  636. foreach ($collection as $item) {
  637. $collectionParts[$item] = explode(':', $item);
  638. }
  639. foreach (explode(':', $name) as $i => $subname) {
  640. foreach ($collectionParts as $collectionName => $parts) {
  641. $exists = isset($alternatives[$collectionName]);
  642. if (!isset($parts[$i]) && $exists) {
  643. $alternatives[$collectionName] += $threshold;
  644. continue;
  645. } elseif (!isset($parts[$i])) {
  646. continue;
  647. }
  648. $lev = levenshtein($subname, $parts[$i]);
  649. if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
  650. $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
  651. } elseif ($exists) {
  652. $alternatives[$collectionName] += $threshold;
  653. }
  654. }
  655. }
  656. foreach ($collection as $item) {
  657. $lev = levenshtein($name, $item);
  658. if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
  659. $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev;
  660. }
  661. }
  662. $alternatives = array_filter($alternatives, function ($lev) use ($threshold) {
  663. return $lev < 2 * $threshold;
  664. });
  665. asort($alternatives);
  666. return array_keys($alternatives);
  667. }
  668. /**
  669. * 设置默认的指令
  670. * @access public
  671. * @param string $commandName The Command name
  672. */
  673. public function setDefaultCommand($commandName)
  674. {
  675. $this->defaultCommand = $commandName;
  676. }
  677. /**
  678. * 返回所有的命名空间
  679. * @access private
  680. * @param string $name
  681. * @return array
  682. */
  683. private function extractAllNamespaces($name)
  684. {
  685. $parts = explode(':', $name, -1);
  686. $namespaces = [];
  687. foreach ($parts as $part) {
  688. if (count($namespaces)) {
  689. $namespaces[] = end($namespaces) . ':' . $part;
  690. } else {
  691. $namespaces[] = $part;
  692. }
  693. }
  694. return $namespaces;
  695. }
  696. public function __debugInfo()
  697. {
  698. $data = get_object_vars($this);
  699. unset($data['commands'], $data['definition']);
  700. return $data;
  701. }
  702. }