Skip to content
Merged
Show file tree
Hide file tree
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
279 changes: 173 additions & 106 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/laravel-app/routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

// Optional: refresh definitions and send buffered telemetry outside of requests.
// Run `php artisan schedule:work` locally, or `php artisan reflag:listen` for push updates.
Schedule::command('reflag:refresh')->everyMinute()->withoutOverlapping();
Schedule::command('reflag:flush')->everyMinute()->withoutOverlapping();
Schedule::command('reflag:refresh')->everyMinute()->onOneServer();
Schedule::command('reflag:flush')->everyMinute()->onOneServer();
5 changes: 5 additions & 0 deletions src/Laravel/Facades/Reflag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
* @method static int drainTelemetry()
* @method static \Reflag\ClientStatus status()
* @method static ?int flagStateVersion()
* @method static void assertFlagChecked(string $key, ?\Closure $callback = null) After {@see fake()}.
* @method static void assertFlagNotChecked(string $key) After {@see fake()}.
* @method static void assertUserUpdated(string|int $userId) After {@see fake()}.
* @method static void assertCompanyUpdated(string|int $companyId) After {@see fake()}.
* @method static list<array<string, mixed>> recorded(?string $type = null) After {@see fake()}.
*
* @see ReflagClient
*/
Expand Down
38 changes: 38 additions & 0 deletions src/Laravel/Middleware/EnsureFlagsAreEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Reflag\Laravel\Middleware;

use Closure;
use Illuminate\Http\Request;
use Reflag\ReflagClient;
use Symfony\Component\HttpFoundation\Response;

/**
* Route middleware that responds 404 unless every given flag is enabled for the signed-in user:
* `->middleware('reflag:new-dashboard')` or `->middleware(EnsureFlagsAreEnabled::using('a', 'b'))`.
*/
final class EnsureFlagsAreEnabled
{
public function __construct(private readonly ReflagClient $reflag)
{
}

/** The middleware definition for routes that require all of the given flags. */
public static function using(string ...$flags): string
{
return self::class . ':' . implode(',', $flags);
}

public function handle(Request $request, Closure $next, string ...$flags): Response
{
foreach ($flags as $flag) {
if ($this->reflag->getFlag($flag) !== true) {
abort(404);
}
}

return $next($request);
}
}
4 changes: 4 additions & 0 deletions src/Laravel/ReflagServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Reflag\Laravel\Commands\FlushCommand;
use Reflag\Laravel\Commands\ListenCommand;
use Reflag\Laravel\Commands\RefreshCommand;
use Reflag\Laravel\Middleware\EnsureFlagsAreEnabled;
use Reflag\ReflagClient;
use Reflag\ReflagOptions;
use Reflag\TelemetryDriver;
Expand Down Expand Up @@ -47,6 +48,9 @@ public function boot(): void
$this->commands([RefreshCommand::class, FlushCommand::class, ListenCommand::class]);
}

// Route::get(...)->middleware('reflag:new-dashboard')
$this->app['router']->aliasMiddleware('reflag', EnsureFlagsAreEnabled::class);

$flush = function (): void {
if ($this->app->resolved(ReflagClient::class)) {
$this->app->make(ReflagClient::class)->terminate();
Expand Down
31 changes: 31 additions & 0 deletions tests/Laravel/LaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,35 @@ public function testConfigIsPublishable(): void
$this->assertArrayHasKey(ReflagServiceProvider::class, \Illuminate\Support\ServiceProvider::$publishes);
$this->assertSame('sec_laravel', config('reflag.secret_key'));
}

public function testFakeAssertionsThroughTheFacade(): void
{
Reflag::fake(['new-dashboard' => true]);
$this->actingAs(new GenericUser(['id' => 7, 'name' => 'Ada', 'email' => 'ada@example.com', 'remember_token' => null]));
\Illuminate\Support\Facades\Route::get('/dashboard', fn () => Reflag::getFlag('new-dashboard') ? 'new' : 'old');

$this->get('/dashboard')->assertOk()->assertSee('new');

Reflag::assertFlagChecked('new-dashboard');
Reflag::assertFlagNotChecked('old-dashboard');
Reflag::assertUserUpdated(7);
}

public function testRouteMiddlewareRequiresEveryFlag(): void
{
Reflag::fake(fn (ReflagContext $context) => ['beta' => $context->userId() === '7', 'search' => true, 'config' => ['on' => true]]);
$route = \Illuminate\Support\Facades\Route::class;
$route::get('/beta', fn () => 'beta')->middleware('reflag:beta');
$route::get('/both', fn () => 'both')->middleware(\Reflag\Laravel\Middleware\EnsureFlagsAreEnabled::using('beta', 'search'));
$route::get('/off', fn () => 'off')->middleware('reflag:beta,missing');
$route::get('/value', fn () => 'value')->middleware('reflag:config');

$this->get('/beta')->assertNotFound();
$this->actingAs(new GenericUser(['id' => 7, 'name' => 'Ada', 'email' => 'ada@example.com', 'remember_token' => null]));
$this->get('/beta')->assertOk()->assertSee('beta');
$this->get('/both')->assertOk();
$this->get('/off')->assertNotFound();
$this->get('/value')->assertNotFound(); // only boolean true enables a route
Reflag::assertFlagChecked('beta');
}
}