update: 优化创建模块

This commit is contained in:
JaguarJack 2020-08-10 10:33:13 +08:00
parent 6c9f1cface
commit 4bfa907191

View File

@ -1,12 +1,15 @@
<?php <?php
namespace catcher\command; namespace catcher\command;
use catcher\facade\FileSystem;
use catcher\library\Composer;
use catcher\library\Compress; use catcher\library\Compress;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
use think\console\input\Argument; use think\console\input\Argument;
use think\console\Output; use think\console\Output;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use think\Exception;
class CreateModuleCommand extends Command class CreateModuleCommand extends Command
{ {
@ -59,9 +62,7 @@ class CreateModuleCommand extends Command
$this->stubDir = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR; $this->stubDir = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR;
$composer = json_decode(file_get_contents($this->app->getRootPath() . 'composer.json'), true); $psr4 = (new Composer())->psr4Autoload();
$psr4 = $composer['autoload']['psr-4'];
foreach ($psr4 as $namespace => $des) { foreach ($psr4 as $namespace => $des) {
if ($des === CatchAdmin::$root) { if ($des === CatchAdmin::$root) {
@ -88,7 +89,7 @@ class CreateModuleCommand extends Command
*/ */
protected function rollback() protected function rollback()
{ {
(new Compress())->rmDir($this->moduleDir); FileSystem::deleteDirectory($this->moduleDir);
} }
/** /**
@ -158,14 +159,13 @@ class CreateModuleCommand extends Command
*/ */
protected function createService() protected function createService()
{ {
$service = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'service.stub'); $service = FileSystem::sharedGet(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'service.stub');
$content = str_replace(['{NAMESPACE}', '{SERVICE}'], $content = str_replace(['{NAMESPACE}', '{SERVICE}'],
[substr($this->namespaces, 0, -1), [substr($this->namespaces, 0, -1),
ucfirst($this->module) . 'Service'], $service); ucfirst($this->module) . 'Service'], $service);
file_put_contents($this->moduleDir . ucfirst($this->module) . 'Service.php', $content); FileSystem::put($this->moduleDir . ucfirst($this->module) . 'Service.php', $content);
} }
/** /**
@ -176,13 +176,13 @@ class CreateModuleCommand extends Command
*/ */
protected function createModuleJson() protected function createModuleJson()
{ {
$moduleJson = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'module.stub'); $moduleJson = FileSystem::sharedGet(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'module.stub');
$content = str_replace(['{NAME}','{DESCRIPTION}','{MODULE}', '{SERVICE}'], $content = str_replace(['{NAME}','{DESCRIPTION}','{MODULE}', '{SERVICE}'],
[$this->name, $this->description, [$this->name, $this->description,
$this->module, '\\\\'. str_replace('\\', '\\\\',$this->namespaces . ucfirst($this->module) . 'Service')], $moduleJson); $this->module, '\\\\'. str_replace('\\', '\\\\',$this->namespaces . ucfirst($this->module) . 'Service')], $moduleJson);
file_put_contents($this->moduleDir . 'module.json', $content); FileSystem::put($this->moduleDir . 'module.json', $content);
} }
/** /**
@ -193,6 +193,6 @@ class CreateModuleCommand extends Command
*/ */
protected function createRoute() protected function createRoute()
{ {
file_put_contents($this->moduleDir . 'route.php', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route.stub')); FileSystem::put($this->moduleDir . 'route.php', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route.stub'));
} }
} }