Skip to content

Commit

Permalink
- 优化Module代码
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Nov 8, 2023
1 parent 5c9e7d3 commit aba57df
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 46 deletions.
48 changes: 22 additions & 26 deletions src/Console/Module/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ class InitCommand extends Command

protected $module;

protected $moduleVersion = 9;

protected $directory;

public function handle(): void
{
$this->checkOption();

// $this->initDB();
$this->initDB();
$this->initAdminDirectory();
}

Expand Down Expand Up @@ -68,10 +66,6 @@ protected function initAdminDirectory()
{
$this->setDirectory();

if (is_dir($this->directory . '/app')) {
$this->moduleVersion = 10;
}

$this->createAuthController();
$this->createBootstrapFile();
$this->createRoutesFile();
Expand All @@ -87,14 +81,14 @@ private function makeDir($path = '')
$this->laravel['files']->makeDirectory("{$this->directory}/$path", 0755, true, true);
}

private function getAppPath()
public function getPath($path, $isApp = false)
{
return $this->directory . ($this->moduleVersion == 10 ? '/app' : '');
return AdminModule::getModulePath($this->module->getName(), $path, $isApp);
}

public function createAuthController(): void
{
$authController = $this->getAppPath() . '/Http/Controllers/AuthController.php';
$authController = $this->getPath('/Http/Controllers/AuthController.php', true);
$contents = $this->getStub('AuthController');
$this->laravel['files']->put(
$authController,
Expand All @@ -105,17 +99,16 @@ public function createAuthController(): void

protected function createBootstrapFile()
{
$file = $this->getAppPath() . '/bootstrap.php';

$file = $this->getPath('/bootstrap.php', true);
$contents = $this->getStub('bootstrap');

$this->laravel['files']->put($file, $contents);
$this->line('<info>Bootstrap file was created:</info> ' . str_replace(base_path(), '', $file));
}

protected function createRoutesFile()
{
$file = $this->directory . ($this->moduleVersion == 10 ? '/routes/admin.php' : '/Routes/admin.php');

$file = $this->getPath('/routes/admin.php');
$contents = $this->getStub('routes');
$content = str_replace('{{Namespace}}', $this->getNamespace('Http\Controllers'), $contents);
$content = str_replace('{{module}}', $this->module->getLowerName(), $content);
Expand All @@ -126,8 +119,9 @@ protected function createRoutesFile()

public function createHomeController(): void
{
$homeController = $this->getAppPath() . '/Http/Controllers/HomeController.php';
$homeController = $this->getPath('/Http/Controllers/HomeController.php', true);
$contents = $this->getStub('HomeController');

$this->laravel['files']->put(
$homeController,
str_replace('{{Namespace}}', $this->getNamespace('Http\Controllers'), $contents)
Expand All @@ -137,8 +131,9 @@ public function createHomeController(): void

public function createSettingController()
{
$settingController = $this->getAppPath() . '/Http/Controllers/SettingController.php';
$settingController = $this->getPath('/Http/Controllers/SettingController.php', true);
$contents = $this->getStub('SettingController');

$this->laravel['files']->put(
$settingController,
str_replace('{{Namespace}}', $this->getNamespace('Http\Controllers'), $contents)
Expand All @@ -150,7 +145,7 @@ public function createSettingController()

protected function getNamespace($name = null): string
{
$prefix = $this->moduleVersion == 10 ? 'app\\' : '';
$prefix = AdminModule::isV10() ? 'app\\' : '';

return config('modules.namespace') . "\\{$this->module->getName()}\\{$prefix}{$name}";
}
Expand All @@ -176,27 +171,28 @@ protected function createViews()

protected function createConfig()
{
$config = $this->directory . ($this->moduleVersion == 10 ? "/config/admin.php" : "/Config/admin.php");
$config = $this->getPath("/config/admin.php");
$contents = $this->getStub('config');
$_path = 'Modules/' . $this->module->getName() . ($this->moduleVersion == 10 ? '/app' : '') . '/bootstrap.php';
$content = str_replace('{{bootstrap}}', 'base_path(\'' . $_path . '\')', $contents);
$content = str_replace('{{route_prefix}}', $this->module->getLowerName() . '-api', $content);
$content = str_replace('{{module_name}}', $this->module->getLowerName(), $content);
$content = str_replace('{{route_namespace}}', $this->getNamespace('Http\Controllers'), $content);
$content = str_replace('{{model_namespace}}', $this->getNamespace('Models'), $content);
$_path = 'Modules/' . $this->module->getName() . (AdminModule::isV10() ? '/app' : '') . '/bootstrap.php';

$content = str_replace('{{bootstrap}}', 'base_path(\'' . $_path . '\')', $contents);
$content = str_replace('{{route_prefix}}', $this->module->getLowerName() . '-api', $content);
$content = str_replace('{{module_name}}', $this->module->getLowerName(), $content);
$content = str_replace('{{route_namespace}}', $this->getNamespace('Http\Controllers'), $content);
$content = str_replace('{{model_namespace}}', $this->getNamespace('Models'), $content);

$this->laravel['files']->put($config, $content);
$this->line('<info>Config file was created:</info> ' . str_replace(base_path(), '', $config));
}

protected function createModel()
{
if ($this->moduleVersion == 9) {
if (!AdminModule::isV10()) {
$this->makeDir('Models');
}

$run = function ($name) {
$file = $this->getAppPath() . "/Models/{$name}.php";
$file = $this->getPath("/Models/{$name}.php", true);
$contents = $this->getStub($name);
$content = str_replace('{{Namespace}}', $this->getNamespace('Models'), $contents);
$content = str_replace('{{module}}', $this->module->getLowerName(), $content);
Expand Down
13 changes: 5 additions & 8 deletions src/Console/Module/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ protected function updateViews()
}

foreach ($modules as $module) {
$module = Module::find(ucfirst($module));
$script = '<script>window.$adminApiPrefix = "/' . $module->getLowerName() . '-api"</script>';
$module = Module::find(ucfirst($module));

$script = '<script>window.$adminApiPrefix = "/' . $module->getLowerName() . '-api"</script>';

$content = preg_replace('/<script>window.*?<\/script>/is', $script, $content);

$_path = $module->getPath();
if (is_dir($_path . '/app')) {
$_path = $_path . '/resources';
} else {
$_path = $_path . '/Resources';
}
$_path = AdminModule::getModulePath($module->getName(), '/Resources');

file_put_contents($_path . '/views/index.blade.php', $content);
}
Expand Down
47 changes: 35 additions & 12 deletions src/Support/Cores/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@

namespace Slowlyo\OwlAdmin\Support\Cores;

use Illuminate\Support\Str;
use Slowlyo\OwlAdmin\Support\Composer;

class Module
{
public static function installed()
{
return class_exists('Nwidart\Modules\Facades\Module');
}

public static function isV10()
{
return version_compare(Composer::getVersion('nwidart/laravel-modules'), '10', '>=');
}

public static function getModulePath($module, $path, $isApp = false)
{
$base = rtrim(config('modules.paths.modules'), '/') . '/' . $module . '/';

if (self::isV10()) {
if ($isApp) {
$base .= 'app/';
} else {
$path = Str::lcfirst(ltrim($path, '/'));
}
} else {
$path = Str::ucfirst(ltrim($path, '/'));
}

return $base . $path;
}

public static function allModules()
{
return config('admin.modules');
}

public static function allRoutePath()
{
$path = [];

$modules = config('admin.modules');
$modules = self::allModules();
if (self::installed() && $modules) {
foreach ($modules as $module) {
$_base = config('modules.paths.modules') . '/' . $module;
$_path = $_base . '/Routes/admin.php';
if (is_dir($_base . '/app')) {
$_path = $_base . '/routes/admin.php';
}
$_path = self::getModulePath($module, '/routes/admin.php');

if (file_exists($_path)) {
$path[] = $_path;
}
Expand All @@ -34,14 +61,10 @@ public static function allConfigPath()
{
$path = [];

$modules = config('admin.modules');
$modules = self::allModules();
if (self::installed() && $modules) {
foreach ($modules as $module) {
$_base = config('modules.paths.modules') . '/' . $module;
$_path = $_base . '/Config/admin.php';
if (is_dir($_base . '/app')) {
$_path = $_base . '/config/admin.php';
}
$_path = self::getModulePath($module, '/config/admin.php');

if (file_exists($_path)) {
$path[strtolower($module) . '.admin'] = $_path;
Expand Down

0 comments on commit aba57df

Please sign in to comment.