diff --git a/extend/catcher/command/publish/WechatCommand.php b/catch/wechat/command/WechatCommand.php similarity index 100% rename from extend/catcher/command/publish/WechatCommand.php rename to catch/wechat/command/WechatCommand.php diff --git a/extend/catcher/CatchAdminService.php b/extend/catcher/CatchAdminService.php index b113e8b..edef1c5 100644 --- a/extend/catcher/CatchAdminService.php +++ b/extend/catcher/CatchAdminService.php @@ -62,27 +62,7 @@ class CatchAdminService extends Service */ protected function registerCommands(): void { - $this->commands([ - InstallProjectCommand::class, - ModuleCacheCommand::class, - MigrateRunCommand::class, - ModelGeneratorCommand::class, - SeedRunCommand::class, - BackupCommand::class, - CompressPackageCommand::class, - CreateModuleCommand::class, - MigrateRollbackCommand::class, - MigrateCreateCommand::class, - WsWorkerCommand::class, - ExportDataCommand::class, - MakeMenuCommand::class, - ExcelTaskCommand::class, - WechatCommand::class, - CacheTrieCommand::class, - ModuleServiceDiscoverCommand::class, - DisableModuleCommand::class, - EnableModuleCommand::class, - ]); + $this->commands((new CatchConsole($this->app))->commands()); } /** * diff --git a/extend/catcher/CatchConsole.php b/extend/catcher/CatchConsole.php index ef90e0a..94d2bd6 100644 --- a/extend/catcher/CatchConsole.php +++ b/extend/catcher/CatchConsole.php @@ -8,3 +8,95 @@ // +---------------------------------------------------------------------- // | Author: JaguarJack [ njphper@gmail.com ] // +---------------------------------------------------------------------- +namespace catcher; + +use catcher\library\FileSystem; +use think\App; +use think\console\Command; +use function GuzzleHttp\Psr7\str; + +class CatchConsole +{ + protected $app; + + protected $namespace = ''; + + protected $path = __DIR__ . DIRECTORY_SEPARATOR . 'command'; + + public function __construct(App $app) + { + $this->app = $app; + } + + /** + * 获取 commands + * + * @time 2020年07月02日 + * @return array + */ + public function commands() + { + $commandFiles = (new FileSystem())->allFiles($this->path); + + $commands = []; + + // dd($this->parseNamespace()); + foreach ($commandFiles as $command) { + if (pathinfo($command, PATHINFO_EXTENSION) == 'php') { + $lastPath = str_replace($this->parseNamespace(), '',pathinfo($command, PATHINFO_DIRNAME)); + $namespace = $this->namespace . str_replace(DIRECTORY_SEPARATOR, '\\', $lastPath) . '\\'; + $commandClass = $namespace . pathinfo($command, PATHINFO_FILENAME); + if (is_subclass_of($commandClass, Command::class)) { + $commands[] = $commandClass; + } + } + } + + return $commands; + } + + protected function parseNamespace() + { + // 没有设置 namespace 默认使用 extend 目录 + if (!$this->namespace) { + return root_path(). 'extend'; + } + + $composer = \json_decode(file_get_contents(root_path(). 'composer.json'), true); + + $rootNamespace = substr($this->namespace, 0, strpos($this->namespace, '\\') + 1); + + return root_path(). $composer['autoload']['psr-4'][$rootNamespace] . DIRECTORY_SEPARATOR . + + str_replace('\\', DIRECTORY_SEPARATOR, substr($this->namespace, strpos($this->namespace, '\\') + 1)); + } + + /** + * set path + * + * @time 2020年07月02日 + * @param $path + * @return $this + */ + public function path($path) + { + $this->path = $path; + + return $this; + } + + /** + * 设置命名空间 + * + * @time 2020年07月02日 + * @param $namespace + * @return $this + */ + public function setNamespace($namespace) + { + $this->namespace = $namespace; + + return $this; + } + +} \ No newline at end of file diff --git a/extend/catcher/ModuleService.php b/extend/catcher/ModuleService.php index b6e6093..840931d 100644 --- a/extend/catcher/ModuleService.php +++ b/extend/catcher/ModuleService.php @@ -16,15 +16,23 @@ abstract class ModuleService extends Service { abstract public function loadRouteFrom(); + /** + * 注册 + * + * @time 2020年07月02日 + * @return void + */ public function register() { $this->app->make('routePath')->loadRouterFrom($this->loadRouteFrom()); $this->registerEvents(); + + $this->registerCommands();; } /** - * 时间注册 + * 事件注册 * * @time 2020年06月24日 * @return void @@ -36,6 +44,22 @@ abstract class ModuleService extends Service } } + /** + * 注册commands + * + * @time 2020年07月02日 + * @return void + */ + protected function registerCommands() + { + if (method_exists($this,'loadCommands')) { + list($namespace, $path) = $this->loadCommands(); + $this->commands((new CatchConsole($this->app)) + ->setNamespace($namespace) + ->path($path) + ->commands()); + } + } } \ No newline at end of file