Skip to content

Commit

Permalink
refactor: App service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 9, 2024
1 parent 0e1024f commit 052630c
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,40 @@

use App\Models\User;
use App\Services\GreetingService;
use App\Services\SanctumAbilitiesService;
use Flare;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

/**
* Application service provider for core functionality.
* Handles version determination, service binding, and authorization gates.
* Core application service provider.
* Handles service registration and authorization setup.
*/
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* Sets up version determination for Flare, binds the GreetingService,
* and creates an alias for it.
* Register application services.
*/
public function register(): void
{
$this->registerFlareVersion();
$this->registerGreetingService();
$this->registerSanctumAbilitiesService();
}

/**
* Bootstrap application services.
*/
public function boot(): void
{
$this->defineGates();
}

/**
* Set up version determination for Flare.
*/
private function registerFlareVersion(): void
{
Flare::determineVersionUsing(function () {
$versionFile = base_path('VERSION');
Expand All @@ -34,18 +50,29 @@ public function register(): void

return str_replace("\n", '', File::get($versionFile));
});
}

$this->app->singleton(GreetingService::class, fn ($app): GreetingService => new GreetingService);

/**
* Register the GreetingService as a singleton.
*/
private function registerGreetingService(): void
{
$this->app->singleton(GreetingService::class);
$this->app->alias(GreetingService::class, 'Greeting');
}

/**
* Bootstrap any application services.
*
* Defines the 'viewPulse' gate for admin access.
* Register the SanctumAbilitiesService as a singleton.
*/
public function boot(): void
private function registerSanctumAbilitiesService(): void
{
$this->app->singleton(SanctumAbilitiesService::class);
}

/**
* Define application authorization gates.
*/
private function defineGates(): void
{
Gate::define('viewPulse', fn (User $user): bool => $user->isAdmin());
}
Expand Down

0 comments on commit 052630c

Please sign in to comment.