feature flag management by admins

This commit is contained in:
Doar Mihai
2026-02-21 15:10:37 +02:00
parent 0f17570476
commit b936a14a1f
4 changed files with 333 additions and 0 deletions
@@ -0,0 +1,127 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Laravel\Pennant\Feature;
class FeatureFlagController extends Controller
{
/**
* Display a listing of feature flags.
*/
public function index()
{
// Get all feature flag classes from app/Features directory
$featurePath = app_path('Features');
$features = [];
if (File::isDirectory($featurePath)) {
$files = File::files($featurePath);
foreach ($files as $file) {
$className = 'App\\Features\\' . $file->getFilenameWithoutExtension();
if (class_exists($className)) {
$featureName = $this->getFeatureName($className);
// Get the current state for null scope (global)
$isActive = Feature::for(null)->active($featureName);
$features[] = [
'name' => $featureName,
'class' => $className,
'is_active' => $isActive,
'description' => $this->getFeatureDescription($className),
];
}
}
}
// Sort features by name
usort($features, fn($a, $b) => strcmp($a['name'], $b['name']));
return view('admin.feature-flags.index', compact('features'));
}
/**
* Toggle a feature flag on or off.
*/
public function toggle(Request $request)
{
$validated = $request->validate([
'feature' => ['required', 'string'],
'active' => ['required', 'boolean'],
]);
$featureName = $validated['feature'];
// Activate or deactivate the feature for null scope (global)
if ($validated['active']) {
Feature::for(null)->activate($featureName);
$message = "Feature '{$featureName}' has been activated.";
} else {
Feature::for(null)->deactivate($featureName);
$message = "Feature '{$featureName}' has been deactivated.";
}
return redirect()->route('admin.feature-flags.index')
->with('success', $message);
}
/**
* Purge a feature flag (remove all stored values).
*/
public function purge(Request $request)
{
$validated = $request->validate([
'feature' => ['required', 'string'],
]);
$featureName = $validated['feature'];
Feature::purge($featureName);
return redirect()->route('admin.feature-flags.index')
->with('success', "Feature '{$featureName}' has been purged from storage.");
}
/**
* Get the feature name from the class.
*/
private function getFeatureName(string $className): string
{
// Check if class has Name attribute
$reflection = new \ReflectionClass($className);
$attributes = $reflection->getAttributes(\Laravel\Pennant\Attributes\Name::class);
if (!empty($attributes)) {
$nameAttribute = $attributes[0]->newInstance();
return $nameAttribute->name;
}
// Default to class basename
return class_basename($className);
}
/**
* Get the feature description from class docblock.
*/
private function getFeatureDescription(string $className): ?string
{
$reflection = new \ReflectionClass($className);
$docComment = $reflection->getDocComment();
if ($docComment) {
// Extract the first line of the docblock as description
preg_match('/\/\*\*\s*\n\s*\*\s*(.+?)\n/s', $docComment, $matches);
if (isset($matches[1])) {
return trim($matches[1]);
}
}
return null;
}
}
@@ -0,0 +1,186 @@
@extends('layouts.app')
@section('content')
<div class="bg-gray-50 dark:bg-gray-900 min-h-screen">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Header -->
<div class="mb-8">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-gray-900 dark:text-white">Feature Flags</h1>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Manage application feature flags</p>
</div>
<a
href="{{ route('admin.dashboard') }}"
class="px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md font-medium transition-colors"
>
Back to Dashboard
</a>
</div>
</div>
<!-- Success/Error Messages -->
@if(session('success'))
<div class="mb-6 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 text-green-800 dark:text-green-200 px-4 py-3 rounded-lg">
{{ session('success') }}
</div>
@endif
@if(session('error'))
<div class="mb-6 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 text-red-800 dark:text-red-200 px-4 py-3 rounded-lg">
{{ session('error') }}
</div>
@endif
<!-- Info Box -->
<div class="mb-6 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-4">
<div class="flex items-start">
<svg class="h-5 w-5 text-blue-400 mt-0.5 mr-3" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path>
</svg>
<div class="flex-1">
<p class="text-sm text-blue-800 dark:text-blue-200">
Feature flags allow you to enable or disable features across the application without deploying new code.
Toggle flags on/off to control feature availability.
</p>
</div>
</div>
</div>
<!-- Feature Flags Table -->
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden">
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
<thead class="bg-gray-50 dark:bg-gray-900">
<tr>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Feature
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Status
</th>
<th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Actions
</th>
</tr>
</thead>
<tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
@forelse($features as $feature)
<tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors">
<td class="px-6 py-4">
<div class="flex items-start">
<div>
<div class="text-sm font-medium text-gray-900 dark:text-white">
{{ $feature['name'] }}
</div>
@if($feature['description'])
<div class="text-sm text-gray-500 dark:text-gray-400 mt-1">
{{ $feature['description'] }}
</div>
@endif
<div class="text-xs text-gray-400 dark:text-gray-500 mt-1 font-mono">
{{ $feature['class'] }}
</div>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
@if($feature['is_active'])
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 dark:bg-green-900 text-green-800 dark:text-green-200">
<svg class="mr-1.5 h-2 w-2" fill="currentColor" viewBox="0 0 8 8">
<circle cx="4" cy="4" r="3" />
</svg>
Active
</span>
@else
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-300">
<svg class="mr-1.5 h-2 w-2" fill="currentColor" viewBox="0 0 8 8">
<circle cx="4" cy="4" r="3" />
</svg>
Inactive
</span>
@endif
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<div class="flex items-center justify-end gap-3">
<!-- Toggle Form -->
<form
action="{{ route('admin.feature-flags.toggle') }}"
method="POST"
class="inline"
>
@csrf
<input type="hidden" name="feature" value="{{ $feature['name'] }}">
<input type="hidden" name="active" value="{{ $feature['is_active'] ? '0' : '1' }}">
<button
type="submit"
class="inline-flex items-center px-3 py-1.5 {{ $feature['is_active'] ? 'bg-red-600 hover:bg-red-700 text-white' : 'bg-green-600 hover:bg-green-700 text-white' }} rounded-md text-sm font-medium transition-colors"
>
@if($feature['is_active'])
<svg class="h-4 w-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636"></path>
</svg>
Deactivate
@else
<svg class="h-4 w-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
Activate
@endif
</button>
</form>
<!-- Purge Form -->
<form
action="{{ route('admin.feature-flags.purge') }}"
method="POST"
onsubmit="return confirm('Are you sure you want to purge this feature flag? This will remove all stored values.');"
class="inline"
>
@csrf
<input type="hidden" name="feature" value="{{ $feature['name'] }}">
<button
type="submit"
class="text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-200"
title="Purge feature flag"
>
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
</svg>
</button>
</form>
</div>
</td>
</tr>
@empty
<tr>
<td colspan="3" class="px-6 py-12 text-center">
<div class="flex flex-col items-center">
<svg class="h-12 w-12 text-gray-400 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 21v-4m0 0V5a2 2 0 012-2h6.5l1 1H21l-3 6 3 6h-8.5l-1-1H5a2 2 0 00-2 2zm9-13.5V9"></path>
</svg>
<p class="text-sm text-gray-500 dark:text-gray-400">No feature flags found</p>
<p class="text-xs text-gray-400 dark:text-gray-500 mt-1">
Add feature flag classes to the <code class="bg-gray-100 dark:bg-gray-700 px-1 rounded">app/Features</code> directory
</p>
</div>
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
</div>
<!-- Help Section -->
<div class="mt-6 bg-gray-50 dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-6">
<h3 class="text-sm font-medium text-gray-900 dark:text-white mb-3">How Feature Flags Work</h3>
<div class="space-y-2 text-sm text-gray-600 dark:text-gray-400">
<p><strong class="text-gray-900 dark:text-white">Activate/Deactivate:</strong> Toggle a feature on or off globally for all users.</p>
<p><strong class="text-gray-900 dark:text-white">Purge:</strong> Remove all stored values for a feature flag. The feature will revert to its default state defined in the code.</p>
<p><strong class="text-gray-900 dark:text-white">Adding Features:</strong> Create new feature flag classes in <code class="bg-gray-100 dark:bg-gray-700 px-1 rounded text-xs">app/Features</code> directory.</p>
</div>
</div>
</div>
</div>
@endsection
@@ -132,6 +132,16 @@
Manage Users Manage Users
</div> </div>
</a> </a>
<a href="{{ route('admin.feature-flags.index') }}"
class="block px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
>
<div class="flex items-center">
<svg class="mr-2 h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 21v-4m0 0V5a2 2 0 012-2h6.5l1 1H21l-3 6 3 6h-8.5l-1-1H5a2 2 0 00-2 2zm9-13.5V9"></path>
</svg>
Feature Flags
</div>
</a>
@endif @endif
@if(auth()->user()->hasAnyRole(['admin', 'moderator'])) @if(auth()->user()->hasAnyRole(['admin', 'moderator']))
<a href="{{ route('products.manage') }}" <a href="{{ route('products.manage') }}"
@@ -210,6 +220,10 @@
class="block pl-3 pr-4 py-2 border-l-4 text-base font-medium {{ request()->routeIs('admin.users.*') ? 'border-primary-500 text-primary-700 bg-primary-50 dark:bg-primary-900 dark:text-primary-200' : 'border-transparent text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300' }}"> class="block pl-3 pr-4 py-2 border-l-4 text-base font-medium {{ request()->routeIs('admin.users.*') ? 'border-primary-500 text-primary-700 bg-primary-50 dark:bg-primary-900 dark:text-primary-200' : 'border-transparent text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300' }}">
Manage Users Manage Users
</a> </a>
<a href="{{ route('admin.feature-flags.index') }}"
class="block pl-3 pr-4 py-2 border-l-4 text-base font-medium {{ request()->routeIs('admin.feature-flags.*') ? 'border-primary-500 text-primary-700 bg-primary-50 dark:bg-primary-900 dark:text-primary-200' : 'border-transparent text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300' }}">
Feature Flags
</a>
@endif @endif
@if(auth()->user()->hasAnyRole(['admin', 'moderator'])) @if(auth()->user()->hasAnyRole(['admin', 'moderator']))
<a href="{{ route('products.manage') }}" <a href="{{ route('products.manage') }}"
+6
View File
@@ -1,5 +1,6 @@
<?php <?php
use App\Http\Controllers\Admin\FeatureFlagController;
use App\Http\Controllers\Admin\UserManagementController; use App\Http\Controllers\Admin\UserManagementController;
use App\Http\Controllers\AdminController; use App\Http\Controllers\AdminController;
use App\Http\Controllers\BookmarkController; use App\Http\Controllers\BookmarkController;
@@ -75,6 +76,11 @@ Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(fun
Route::delete('/users/{user}', [UserManagementController::class, 'destroy'])->name('users.destroy'); Route::delete('/users/{user}', [UserManagementController::class, 'destroy'])->name('users.destroy');
Route::post('/users/{user}/roles/{role}/toggle', [UserManagementController::class, 'toggleRole'])->name('users.roles.toggle'); Route::post('/users/{user}/roles/{role}/toggle', [UserManagementController::class, 'toggleRole'])->name('users.roles.toggle');
// Feature Flag Management
Route::get('/feature-flags', [FeatureFlagController::class, 'index'])->name('feature-flags.index');
Route::post('/feature-flags/toggle', [FeatureFlagController::class, 'toggle'])->name('feature-flags.toggle');
Route::post('/feature-flags/purge', [FeatureFlagController::class, 'purge'])->name('feature-flags.purge');
Route::get('/settings', function () { Route::get('/settings', function () {
return 'Settings (Admin Only)'; return 'Settings (Admin Only)';
})->name('settings'); })->name('settings');