laravel-roles/src/RolesServiceProvider.php

88 lines
2.9 KiB
PHP
Raw Normal View History

2025-06-11 13:44:39 +00:00
<?php
2025-06-15 09:56:28 +00:00
namespace Blax\Roles;
2025-06-16 07:49:36 +00:00
class RolesServiceProvider extends \Illuminate\Support\ServiceProvider
2025-06-15 09:56:28 +00:00
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
2025-06-16 05:57:01 +00:00
$this->mergeConfigFrom(
__DIR__ . '/../config/roles.php',
2025-06-16 05:57:01 +00:00
'roles'
);
2025-06-15 09:56:28 +00:00
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
2025-06-16 05:37:35 +00:00
$this->offerPublishing();
$this->registerMigrations();
2025-06-16 05:37:35 +00:00
$this->registerModelBindings();
2025-06-15 09:56:28 +00:00
}
/**
* Auto-load the package's migrations so fresh installs work without
* publishing. Disabled via `roles.run_migrations = false` for projects
* that prefer to publish + manage migrations themselves.
*/
protected function registerMigrations(): void
{
if (! config('roles.run_migrations', true)) {
return;
}
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
}
2025-06-15 09:56:28 +00:00
/**
* Set up the publishing of configuration files.
*
* @return void
*/
protected function offerPublishing()
{
2025-06-16 06:31:14 +00:00
if (
! $this->app->runningInConsole()
) {
2025-06-15 09:56:28 +00:00
return;
}
$this->publishes([
__DIR__ . '/../config/roles.php' => $this->app->configPath('roles.php'),
2025-06-16 05:37:35 +00:00
], 'roles-config');
2025-06-15 09:56:28 +00:00
// Publish migrations to the host project keeping the same filename as
// the source file. That filename is what Laravel's migrator records in
// the `migrations` table, so any migration that has already run via
// auto-load will be marked as run for the published copy too — no
// duplicate execution.
$migrationsPath = __DIR__ . '/../database/migrations';
$publishMap = [];
foreach (glob($migrationsPath . '/*.php') as $sourcePath) {
$publishMap[$sourcePath] = $this->app->databasePath('migrations/' . basename($sourcePath));
}
$this->publishes($publishMap, 'roles-migrations');
2025-06-16 05:42:57 +00:00
}
2025-06-16 05:37:35 +00:00
protected function registerModelBindings(): void
{
$this->app->bind(\Blax\Roles\Models\Role::class, fn($app) => $app->make($app->config['roles.models.role']));
$this->app->bind(\Blax\Roles\Models\RoleMember::class, fn($app) => $app->make($app->config['roles.models.role_member']));
$this->app->bind(\Blax\Roles\Models\Permission::class, fn($app) => $app->make($app->config['roles.models.permission']));
$this->app->bind(\Blax\Roles\Models\PermissionUsage::class, fn($app) => $app->make($app->config['roles.models.permission_usage']));
$this->app->bind(\Blax\Roles\Models\PermissionMember::class, fn($app) => $app->make($app->config['roles.models.permission_member']));
2026-02-23 10:16:27 +00:00
$this->app->bind(\Blax\Roles\Models\Access::class, fn($app) => $app->make($app->config['roles.models.access']));
2025-06-15 09:56:28 +00:00
}
}