First setup: module registry, switchable modules, permissions
This commit is contained in:
commit
936c794597
52 files changed
+1304
No files matched your search
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager;
|
||||
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
|
||||
class ManagerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__.'/../config/manager.php', 'manager');
|
||||
|
||||
$this->app->singleton(ModuleRegistry::class, fn ($app) => new ModuleRegistry(
|
||||
$app['config']->get('manager.available', []),
|
||||
$app['config']->get('manager.modules', []),
|
||||
));
|
||||
}
|
||||
|
||||
public function boot(ModuleRegistry $modules): void
|
||||
{
|
||||
$modules->validate();
|
||||
|
||||
$this->publishes([__DIR__.'/../config/manager.php' => config_path('manager.php')], 'manager-config');
|
||||
|
||||
$this->loadViewsFrom(__DIR__.'/../resources/views', 'manager');
|
||||
|
||||
// A module that is off adds no migrations, so a site without reviews has no reviews table.
|
||||
foreach ($modules->active() as $module) {
|
||||
if ($module->migrations() !== null) {
|
||||
$this->loadMigrationsFrom($module->migrations());
|
||||
}
|
||||
}
|
||||
|
||||
$this->registerGates($modules);
|
||||
$this->registerRoutes($modules);
|
||||
}
|
||||
|
||||
private function registerGates(ModuleRegistry $modules): void
|
||||
{
|
||||
foreach (array_keys($modules->permissions()) as $permission) {
|
||||
Gate::define($permission, fn ($user) => method_exists($user, 'hasManagerPermission') && $user->hasManagerPermission($permission));
|
||||
}
|
||||
}
|
||||
|
||||
private function registerRoutes(ModuleRegistry $modules): void
|
||||
{
|
||||
Route::middleware(config('manager.middleware'))
|
||||
->prefix(config('manager.path'))
|
||||
->name('manager.')
|
||||
->group(function () use ($modules) {
|
||||
$this->loadRoutesFrom(__DIR__.'/../routes/manager.php');
|
||||
|
||||
foreach ($modules->active() as $module) {
|
||||
if ($module->routes() !== null) {
|
||||
$this->loadRoutesFrom($module->routes());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user