laravel-roles/src/PermissionsServiceProvider.php

78 lines
2.5 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;
class PermissionsServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
2025-06-16 05:57:01 +00:00
$this->mergeConfigFrom(
__DIR__.'/../config/roles.php',
'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([
2025-06-16 06:31:14 +00:00
__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'),
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}")
->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\RolePermission::class, fn ($app) => $app->make($app->config['roles.models.role_permission']));
$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']));
2025-06-15 09:56:28 +00:00
}
}