FEATURE: add support for submodules
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Pingpong\Modules\Json;
|
||||
|
||||
class SubmodulesManager extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$enabledModules = \Module::enabled();
|
||||
|
||||
foreach ($enabledModules as $module) {
|
||||
$modulePath = $module->getPath();
|
||||
$config = new Json($modulePath . DIRECTORY_SEPARATOR . 'module.json');
|
||||
|
||||
// Does this module specify any providers? If not, consider this module with submodules and register them
|
||||
if ($config->get('providers')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$submodules = \File::directories($modulePath);
|
||||
|
||||
foreach ($submodules as $submodulePath) {
|
||||
try {
|
||||
$config = new Json($submodulePath . DIRECTORY_SEPARATOR . 'module.json');
|
||||
|
||||
if (!$config->get('active')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$serviceProviders = $config->get('providers', []);
|
||||
foreach ($serviceProviders as $serviceProvider) {
|
||||
$this->app->register($serviceProvider);
|
||||
}
|
||||
|
||||
$initFiles = $config->get('files', []);
|
||||
|
||||
foreach ($initFiles as $file) {
|
||||
@include $submodulePath . DIRECTORY_SEPARATOR . $file;
|
||||
}
|
||||
} catch (FileNotFoundException $e) {
|
||||
// ignore missing module and do not register this submodule
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,6 +154,8 @@ return [
|
||||
App\Providers\ModuleManager::class,
|
||||
Pingpong\Modules\ModulesServiceProvider::class,
|
||||
Pingpong\Menus\MenusServiceProvider::class,
|
||||
App\Providers\SubmodulesManager::class,
|
||||
|
||||
Franzose\ClosureTable\ClosureTableServiceProvider::class,
|
||||
\Watson\Sitemap\SitemapServiceProvider::class,
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class MakeModuleHandlerNullable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('modules', function (Blueprint $table) {
|
||||
$table->string('handler')
|
||||
->nullable(true)
|
||||
->change();
|
||||
|
||||
$table->string('alias')
|
||||
->nullable(true)
|
||||
->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('modules', function (Blueprint $table) {
|
||||
|
||||
$table->string('alias')
|
||||
->nullable(false)
|
||||
->change();
|
||||
|
||||
// somehow I cannot change it back, sorry...
|
||||
|
||||
// $table->string('handler')
|
||||
// ->nullable(false)
|
||||
// ->change();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddParentModuleIdToModules extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('modules', function (Blueprint $table) {
|
||||
$table->integer('parent_id')
|
||||
->nullable(true)
|
||||
->comment('Parent module ID (when module is submodule)')
|
||||
->after('id');
|
||||
$table->boolean('has_submodules')
|
||||
->default(false);
|
||||
|
||||
$table->foreign('parent_id')
|
||||
->references('id')
|
||||
->on('modules')
|
||||
->onDelete('cascade')
|
||||
->onUpdate('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('modules', function (Blueprint $table) {
|
||||
$table->dropForeign('modules_parent_id_foreign');
|
||||
|
||||
$table->dropColumn('has_submodules');
|
||||
$table->dropColumn('parent_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user