laravel-roles/src/RolesServiceProvider.php

80 lines
2.7 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->registerModelBindings();
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
2025-06-16 06:34:58 +00:00
$this->publishes([
__DIR__ . '/../database/migrations/create_blax_role_tables.php.stub' => $this->getMigrationFileName('create_blax_role_tables.php'),
2026-02-23 10:16:27 +00:00
__DIR__ . '/../database/migrations/create_blax_access_table.php.stub' => $this->getMigrationFileName('create_blax_access_table.php'),
2025-06-16 05:37:35 +00:00
], 'roles-migrations');
}
/**
2025-06-16 05:42:57 +00:00
* Returns existing migration file if found, else uses the current timestamp.
*/
protected function getMigrationFileName(string $migrationFileName): string
{
$timestamp = date('Y_m_d_His');
$filesystem = $this->app->make(\Illuminate\Filesystem\Filesystem::class);
return \Illuminate\Support\Collection::make([$this->app->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR])
->flatMap(fn($path) => $filesystem->glob($path . '*_' . $migrationFileName))
->push($this->app->databasePath() . "/migrations/{$timestamp}_{$migrationFileName}")
2025-06-16 05:42:57 +00:00
->first();
}
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
}
}