Console.php 23 KB

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