Skip to content

Commit

Permalink
refactor: Extracted feature flags into provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 20, 2024
1 parent 06c9dd5 commit a702275
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 280 deletions.
26 changes: 0 additions & 26 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use App\Services\GreetingService;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Laravel\Pennant\Feature;
use Laravel\Sanctum\Sanctum;

/**
Expand All @@ -34,7 +33,6 @@ public function boot(): void
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);

$this->defineGates();
$this->defineFeatures();
}

/**
Expand All @@ -53,28 +51,4 @@ private function defineGates(): void
{
Gate::define('viewPulse', fn (User $user): bool => $user->isAdmin());
}

/**
* Define feature flags with additional metadata.
*/
private function defineFeatures(): void
{
$features = [
'navigation-redesign' => [
'title' => 'Navigation Redesign',
'description' => 'A new, more intuitive navigation structure for improved user experience.',
'group' => 'UI/UX',
'icon' => 'heroicon-o-bars-3',
],
];

foreach ($features as $key => $metadata) {
Feature::define($key, function (): false {
return false;
});

// Store the metadata for later use
config(["features.{$key}" => $metadata]);
}
}
}
99 changes: 99 additions & 0 deletions app/Providers/FeatureServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Pennant\Feature;

/**
* Service provider for managing feature flags.
*
* This provider handles the definition and configuration of feature flags
* used throughout the application. It allows for centralized management
* of features that can be dynamically enabled or disabled.
*
* For more information on how to use and manage feature flags, please refer to:
*
* @see https://docs.vanguardbackup.com/experiments
*/
class FeatureServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* This method is called by Laravel during the service provider registration
* process. It's used to bind things in the service container.
*/
public function register(): void
{
//
}

/**
* Bootstrap services.
*
* This method is called after all other service providers have been registered.
* It's used to perform any boot-time operations or register event listeners.
*/
public function boot(): void
{
$this->defineFeatures();
}

/**
* Define feature flags with additional metadata.
*
* This method sets up feature flags and their associated metadata.
* It iterates through the defined features, setting up each feature
* with Laravel Pennant and storing its metadata in the config.
*
* Features can be toggled via the Experiments manager in user settings.
*/
private function defineFeatures(): void
{
$features = $this->getFeatures();

foreach ($features as $key => $metadata) {
Feature::define($key, fn (): bool => $this->isFeatureEnabled());
config(["features.{$key}" => $metadata]);
}
}

/**
* Get the list of features with their metadata.
*
* This method returns an array of feature definitions. Each feature
* is defined with a key and an array of metadata including title,
* description, group, and icon.
*
* @return array<string, array{title: string, description: string, group: string, icon: string}>
*/
private function getFeatures(): array
{
return [
// 'example-feature' => [
// 'title' => 'Example Feature',
// 'description' => 'Description of the example feature.',
// 'group' => 'General',
// 'icon' => 'heroicon-o-beaker',
// ],
];
}

/**
* Determine if a feature is enabled.
*
* This method checks whether a specific feature is enabled.
* The implementation of this method should include the logic
* to determine the state of each feature.
*
* @return bool True if the feature is enabled, false otherwise
*/
private function isFeatureEnabled(): bool
{
return false;
// Default to disabled
}
}
10 changes: 2 additions & 8 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php

/**
* Application Service Providers
*
* This array lists the service providers that will be loaded as part of the application bootstrap process.
* Each provider is responsible for binding services in the container, registering events, or performing any other tasks.
*/

return [
App\Providers\AppServiceProvider::class,
App\Providers\FeatureServiceProvider::class,
App\Providers\HorizonServiceProvider::class,
App\Providers\VoltServiceProvider::class,
App\Providers\ServerConnectionServiceProvider::class,
App\Providers\VoltServiceProvider::class,
Laravel\Sanctum\SanctumServiceProvider::class,
];
Loading

0 comments on commit a702275

Please sign in to comment.