templave v1
This commit is contained in:
121
src/app/Providers/ModuleServiceProvider.php.stub
Normal file
121
src/app/Providers/ModuleServiceProvider.php.stub
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ModuleServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->registerModules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->bootModules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all modules.
|
||||
*/
|
||||
protected function registerModules(): void
|
||||
{
|
||||
$modulesPath = app_path('Modules');
|
||||
|
||||
if (!File::isDirectory($modulesPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$modules = File::directories($modulesPath);
|
||||
|
||||
foreach ($modules as $modulePath) {
|
||||
$moduleName = basename($modulePath);
|
||||
$providerClass = "App\\Modules\\{$moduleName}\\{$moduleName}ServiceProvider";
|
||||
|
||||
if (class_exists($providerClass)) {
|
||||
$this->app->register($providerClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot all modules.
|
||||
*/
|
||||
protected function bootModules(): void
|
||||
{
|
||||
$modulesPath = app_path('Modules');
|
||||
|
||||
if (!File::isDirectory($modulesPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$modules = File::directories($modulesPath);
|
||||
|
||||
foreach ($modules as $modulePath) {
|
||||
$moduleName = basename($modulePath);
|
||||
|
||||
// Load module config
|
||||
$configPath = "{$modulePath}/Config";
|
||||
if (File::isDirectory($configPath)) {
|
||||
foreach (File::files($configPath) as $file) {
|
||||
$this->mergeConfigFrom(
|
||||
$file->getPathname(),
|
||||
strtolower($moduleName) . '.' . $file->getFilenameWithoutExtension()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Load module routes
|
||||
$this->loadModuleRoutes($modulePath, $moduleName);
|
||||
|
||||
// Load module views
|
||||
$viewsPath = "{$modulePath}/Resources/views";
|
||||
if (File::isDirectory($viewsPath)) {
|
||||
$this->loadViewsFrom($viewsPath, strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $moduleName)));
|
||||
}
|
||||
|
||||
// Load module migrations
|
||||
$migrationsPath = "{$modulePath}/Database/Migrations";
|
||||
if (File::isDirectory($migrationsPath)) {
|
||||
$this->loadMigrationsFrom($migrationsPath);
|
||||
}
|
||||
|
||||
// Load module translations
|
||||
$langPath = "{$modulePath}/Resources/lang";
|
||||
if (File::isDirectory($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, strtolower($moduleName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load module routes.
|
||||
*/
|
||||
protected function loadModuleRoutes(string $modulePath, string $moduleName): void
|
||||
{
|
||||
$routesPath = "{$modulePath}/Routes";
|
||||
|
||||
if (!File::isDirectory($routesPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$webRoutes = "{$routesPath}/web.php";
|
||||
$apiRoutes = "{$routesPath}/api.php";
|
||||
|
||||
if (File::exists($webRoutes)) {
|
||||
$this->loadRoutesFrom($webRoutes);
|
||||
}
|
||||
|
||||
if (File::exists($apiRoutes)) {
|
||||
$this->loadRoutesFrom($apiRoutes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user