From 027ba1acdb0a06aa5d64db901c3a501161f10793 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 19 Jul 2020 10:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96catchConsole=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/CatchConsole.php | 20 ++++++---- extend/catcher/facade/FileSystem.php | 58 ++++++++++++++++++++++++++++ extend/catcher/facade/Http.php | 5 +-- extend/catcher/library/Composer.php | 31 +++++++++++++++ 4 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 extend/catcher/facade/FileSystem.php create mode 100644 extend/catcher/library/Composer.php diff --git a/extend/catcher/CatchConsole.php b/extend/catcher/CatchConsole.php index 94d2bd6..2d80789 100644 --- a/extend/catcher/CatchConsole.php +++ b/extend/catcher/CatchConsole.php @@ -10,10 +10,10 @@ // +---------------------------------------------------------------------- namespace catcher; -use catcher\library\FileSystem; +use catcher\library\Composer; +use catcher\facade\FileSystem; use think\App; use think\console\Command; -use function GuzzleHttp\Psr7\str; class CatchConsole { @@ -36,13 +36,13 @@ class CatchConsole */ public function commands() { - $commandFiles = (new FileSystem())->allFiles($this->path); + $commandFiles = FileSystem::allFiles($this->path); $commands = []; - // dd($this->parseNamespace()); + /* \Symfony\Component\Finder\SplFileInfo $command */ foreach ($commandFiles as $command) { - if (pathinfo($command, PATHINFO_EXTENSION) == 'php') { + if ($command->getExtension() === 'php') { $lastPath = str_replace($this->parseNamespace(), '',pathinfo($command, PATHINFO_DIRNAME)); $namespace = $this->namespace . str_replace(DIRECTORY_SEPARATOR, '\\', $lastPath) . '\\'; $commandClass = $namespace . pathinfo($command, PATHINFO_FILENAME); @@ -55,6 +55,12 @@ class CatchConsole return $commands; } + /** + * 命名空间解析 + * + * @time 2020年07月19日 + * @return string + */ protected function parseNamespace() { // 没有设置 namespace 默认使用 extend 目录 @@ -62,11 +68,11 @@ class CatchConsole return root_path(). 'extend'; } - $composer = \json_decode(file_get_contents(root_path(). 'composer.json'), true); + $psr4 = (new Composer())->psr4Autoload(); $rootNamespace = substr($this->namespace, 0, strpos($this->namespace, '\\') + 1); - return root_path(). $composer['autoload']['psr-4'][$rootNamespace] . DIRECTORY_SEPARATOR . + return root_path(). $psr4[$rootNamespace] . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($this->namespace, strpos($this->namespace, '\\') + 1)); } diff --git a/extend/catcher/facade/FileSystem.php b/extend/catcher/facade/FileSystem.php new file mode 100644 index 0000000..6289fa5 --- /dev/null +++ b/extend/catcher/facade/FileSystem.php @@ -0,0 +1,58 @@ +composerContent()['autoload']['psr-4']; + } + + protected function composerContent() + { + return \json_decode(FileSystem::get($this->composerJsonPath()), true); + } + + protected function composerJsonPath() + { + return root_path() . 'composer.json'; + } +}