Console.php 23 KB

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