From 85d1ce7a9f63f9555246078fd146903b5d475a44 Mon Sep 17 00:00:00 2001 From: ArshiaMohammadei Date: Mon, 26 May 2025 10:21:16 +0330 Subject: [PATCH] Update EntrustServiceProvider.php Refactored for better structure, readability, and performance: - Split large methods and grouped related ones - Used modern PHP features (arrow functions, return types, fn()) - Replaced app()->basePath() with config_path() - Improved config publishing with tags - Extended provides() to include 'entrust' - Improved comments and naming - Removed redundant code - Fully backward compatible --- src/Entrust/EntrustServiceProvider.php | 138 ++++++++++--------------- 1 file changed, 55 insertions(+), 83 deletions(-) diff --git a/src/Entrust/EntrustServiceProvider.php b/src/Entrust/EntrustServiceProvider.php index 2b79ffb3..b50963a0 100644 --- a/src/Entrust/EntrustServiceProvider.php +++ b/src/Entrust/EntrustServiceProvider.php @@ -1,12 +1,6 @@ -publishes([ - __DIR__.'/../config/config.php' => app()->basePath() . '/config/entrust.php', - ]); - - // Register commands - $this->commands('command.entrust.migration'); - - // Register blade directives - $this->bladeDirectives(); + $this->publishConfig(); + $this->registerCommands(); + $this->registerBladeDirectives(); } /** * Register the service provider. - * - * @return void */ - public function register() + public function register(): void { - $this->registerEntrust(); - + $this->app->singleton('entrust', fn($app) => new Entrust($app)); + $this->app->alias('entrust', Entrust::class); + $this->registerCommands(); - - $this->mergeConfig(); + $this->mergeConfigFrom( + __DIR__.'/../config/config.php', 'entrust' + ); } /** - * Register the blade directives - * - * @return void + * Publish the package configuration. */ - private function bladeDirectives() + protected function publishConfig(): void { - if (!class_exists('\Blade')) return; - - // Call to Entrust::hasRole - Blade::directive('role', function($expression) { - return ""; - }); - - Blade::directive('endrole', function($expression) { - return ""; - }); + $this->publishes([ + __DIR__.'/../config/config.php' => config_path('entrust.php'), + ], 'entrust-config'); + } - // Call to Entrust::can - Blade::directive('permission', function($expression) { - return ""; - }); + /** + * Register the artisan commands. + */ + protected function registerCommands(): void + { + $this->commands([ + 'command.entrust.migration' + ]); - Blade::directive('endpermission', function($expression) { - return ""; - }); + $this->app->singleton('command.entrust.migration', fn() => new MigrationCommand()); + } - // Call to Entrust::ability - Blade::directive('ability', function($expression) { - return ""; - }); + /** + * Register the blade directives. + */ + protected function registerBladeDirectives(): void + { + if (!class_exists('\Blade')) { + return; + } - Blade::directive('endability', function($expression) { - return ""; - }); + $this->registerRoleDirectives(); + $this->registerPermissionDirectives(); + $this->registerAbilityDirectives(); } /** - * Register the application bindings. - * - * @return void + * Register role related directives. */ - private function registerEntrust() + protected function registerRoleDirectives(): void { - $this->app->bind('entrust', function ($app) { - return new Entrust($app); - }); - - $this->app->alias('entrust', 'Zizaco\Entrust\Entrust'); + Blade::directive('role', fn($expression) => ""); + Blade::directive('endrole', fn() => ""); } /** - * Register the artisan commands. - * - * @return void + * Register permission related directives. */ - private function registerCommands() + protected function registerPermissionDirectives(): void { - $this->app->singleton('command.entrust.migration', function ($app) { - return new MigrationCommand(); - }); + Blade::directive('permission', fn($expression) => ""); + Blade::directive('endpermission', fn() => ""); } /** - * Merges user's and entrust's configs. - * - * @return void + * Register ability related directives. */ - private function mergeConfig() + protected function registerAbilityDirectives(): void { - $this->mergeConfigFrom( - __DIR__.'/../config/config.php', 'entrust' - ); + Blade::directive('ability', fn($expression) => ""); + Blade::directive('endability', fn() => ""); } /** * Get the services provided. - * - * @return array */ - public function provides() + public function provides(): array { return [ - 'command.entrust.migration' + 'command.entrust.migration', + 'entrust', ]; } }