Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 55 additions & 83 deletions src/Entrust/EntrustServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php namespace Zizaco\Entrust;
<?php

/**
* This file is part of Entrust,
* a role & permission management solution for Laravel.
*
* @license MIT
* @package Zizaco\Entrust
*/
namespace Zizaco\Entrust;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
Expand All @@ -22,121 +16,99 @@ class EntrustServiceProvider extends ServiceProvider

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
public function boot(): void
{
// Publish config files
$this->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 "<?php if (\\Entrust::hasRole({$expression})) : ?>";
});

Blade::directive('endrole', function($expression) {
return "<?php endif; // Entrust::hasRole ?>";
});
$this->publishes([
__DIR__.'/../config/config.php' => config_path('entrust.php'),
], 'entrust-config');
}

// Call to Entrust::can
Blade::directive('permission', function($expression) {
return "<?php if (\\Entrust::can({$expression})) : ?>";
});
/**
* Register the artisan commands.
*/
protected function registerCommands(): void
{
$this->commands([
'command.entrust.migration'
]);

Blade::directive('endpermission', function($expression) {
return "<?php endif; // Entrust::can ?>";
});
$this->app->singleton('command.entrust.migration', fn() => new MigrationCommand());
}

// Call to Entrust::ability
Blade::directive('ability', function($expression) {
return "<?php if (\\Entrust::ability({$expression})) : ?>";
});
/**
* Register the blade directives.
*/
protected function registerBladeDirectives(): void
{
if (!class_exists('\Blade')) {
return;
}

Blade::directive('endability', function($expression) {
return "<?php endif; // Entrust::ability ?>";
});
$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) => "<?php if (\\Entrust::hasRole({$expression})) : ?>");
Blade::directive('endrole', fn() => "<?php endif; // Entrust::hasRole ?>");
}

/**
* 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) => "<?php if (\\Entrust::can({$expression})) : ?>");
Blade::directive('endpermission', fn() => "<?php endif; // Entrust::can ?>");
}

/**
* 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) => "<?php if (\\Entrust::ability({$expression})) : ?>");
Blade::directive('endability', fn() => "<?php endif; // Entrust::ability ?>");
}

/**
* Get the services provided.
*
* @return array
*/
public function provides()
public function provides(): array
{
return [
'command.entrust.migration'
'command.entrust.migration',
'entrust',
];
}
}