update:更新默认加载commands

This commit is contained in:
JaguarJack 2021-01-24 20:24:43 +08:00
parent 9f6f02ad24
commit 937e1745d2
2 changed files with 35 additions and 13 deletions

View File

@ -50,7 +50,7 @@ class CatchAdminService extends Service
$this->app->bind('catch\console', $catchConsole); $this->app->bind('catch\console', $catchConsole);
$this->commands($catchConsole->commands()); $this->commands($catchConsole->defaultCommands());
} }
} }
/** /**

View File

@ -14,9 +14,7 @@ namespace catcher;
use catcher\library\Composer; use catcher\library\Composer;
use catcher\facade\FileSystem; use catcher\facade\FileSystem;
use Symfony\Component\Finder\SplFileInfo;
use think\App; use think\App;
use think\console\Command;
class CatchConsole class CatchConsole
{ {
@ -37,7 +35,7 @@ class CatchConsole
* @time 2020年07月02日 * @time 2020年07月02日
* @return array * @return array
*/ */
public function commands() public function commands(): array
{ {
$commandFiles = FileSystem::allFiles($this->path); $commandFiles = FileSystem::allFiles($this->path);
@ -62,14 +60,9 @@ class CatchConsole
* @time 2020年07月19日 * @time 2020年07月19日
* @return string * @return string
*/ */
protected function parseNamespace() protected function parseNamespace(): string
{ {
// 没有设置 namespace 默认使用 extend 目录 $psr4 = (new Composer)->psr4Autoload();
if (!$this->namespace) {
return root_path(). 'extend';
}
$psr4 = (new Composer())->psr4Autoload();
if (strpos($this->namespace, '\\') === false) { if (strpos($this->namespace, '\\') === false) {
$rootNamespace = $this->namespace . '\\'; $rootNamespace = $this->namespace . '\\';
@ -93,7 +86,7 @@ class CatchConsole
* @param $path * @param $path
* @return $this * @return $this
*/ */
public function path($path) public function path($path): CatchConsole
{ {
$this->path = $path; $this->path = $path;
@ -107,11 +100,40 @@ class CatchConsole
* @param $namespace * @param $namespace
* @return $this * @return $this
*/ */
public function setNamespace($namespace) public function setNamespace($namespace): CatchConsole
{ {
$this->namespace = $namespace; $this->namespace = $namespace;
return $this; return $this;
} }
/**
* 默认 commands
*
* @time 2021年01月24日
* @return array
*/
public function defaultCommands(): array
{
$defaultCommands = FileSystem::allFiles(__DIR__ . DIRECTORY_SEPARATOR . 'command');
$commands = [];
/* \Symfony\Component\Finder\SplFileInfo $command */
foreach ($defaultCommands as $command) {
if ($command->getExtension() === 'php') {
$filename = str_replace('.php', '', str_replace(__DIR__, '', $command->getPathname()));
$class = 'catcher' . str_replace(DIRECTORY_SEPARATOR, '\\', $filename);
if (class_exists($class)) {
$commands[] = $class;
}
}
}
return $commands;
}
} }