From 317a09427d6931ed6cbb34b67fbcc7a4c78a2f76 Mon Sep 17 00:00:00 2001 From: Shavonn Brown Date: Tue, 13 Jan 2026 19:55:36 -0500 Subject: [PATCH] feat: share flags with Inertia via middleware --- README.md | 25 +++++++++++++++++++ composer.json | 1 + src/Commands/CreateCommand.php | 29 +++++++++++----------- src/Middleware/ShareTogglesWithInertia.php | 23 +++++++++++++++++ 4 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 src/Middleware/ShareTogglesWithInertia.php diff --git a/README.md b/README.md index 7f87827..7cb1f4e 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,31 @@ Toggle::updateOrCreate( The model automatically clears the cache when toggles are saved or deleted. +### Inertia + +Share all toggles with your frontend by using the provided Inertia middleware. Replace your `HandleInertiaRequests` middleware in `bootstrap/app.php`: + +```php +use OffloadProject\Toggle\Middleware\ShareTogglesWithInertia; + +->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ + ShareTogglesWithInertia::class, + ]); +}) +``` + +All toggles will be available as the `flags` prop in your frontend: + +```js +// Vue/React +const { flags } = usePage().props + +if (flags['new-checkout']) { + // Show new checkout +} +``` + ## Artisan Commands ### List all toggles diff --git a/composer.json b/composer.json index 142ebab..2e02dbe 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ }, "require-dev": { "captainhook/captainhook-phar": "^5.27", + "inertiajs/inertia-laravel": "^2.0", "larastan/larastan": "^3.8.1", "laravel/pint": "^1.26.0", "orchestra/testbench": "^9.0|^10.8.0", diff --git a/src/Commands/CreateCommand.php b/src/Commands/CreateCommand.php index 2440e46..e325439 100644 --- a/src/Commands/CreateCommand.php +++ b/src/Commands/CreateCommand.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; use OffloadProject\Toggle\Models\Toggle; + use function Laravel\Prompts\error; use function Laravel\Prompts\info; use function Laravel\Prompts\warning; @@ -24,8 +25,7 @@ class CreateCommand extends Command public function __construct( protected Filesystem $files, - ) - { + ) { parent::__construct(); } @@ -33,8 +33,8 @@ public function handle(): int { /** @var string $name */ $name = $this->argument('name'); - $active = (bool)$this->option('active'); - $createDb = (bool)$this->option('db'); + $active = (bool) $this->option('active'); + $createDb = (bool) $this->option('db'); $envKey = $this->generateEnvKey($name); @@ -57,14 +57,14 @@ public function handle(): int protected function generateEnvKey(string $name): string { - return 'TOGGLE_' . strtoupper(str_replace('-', '_', $name)); + return 'TOGGLE_'.strtoupper(str_replace('-', '_', $name)); } protected function updateConfigFile(string $name, string $envKey, bool $active): bool { $configPath = config_path('toggle.php'); - if (!$this->files->exists($configPath)) { + if (! $this->files->exists($configPath)) { warning('Config file not found. Please publish it first:'); info('php artisan vendor:publish --tag=toggle-config'); @@ -92,7 +92,7 @@ protected function updateConfigFile(string $name, string $envKey, bool $active): // Find the flags array and insert the new entry $pattern = "/('flags'\s*=>\s*\[)([^]]*?)(\s*],)/s"; - if (!preg_match($pattern, $content, $matches)) { + if (! preg_match($pattern, $content, $matches)) { $this->showManualInstructions($name, $envKey, $active); return false; @@ -126,7 +126,7 @@ protected function updateEnvFile(string $envKey, bool $active): bool { $envPath = base_path('.env'); - if (!$this->files->exists($envPath)) { + if (! $this->files->exists($envPath)) { return false; } @@ -146,7 +146,7 @@ protected function updateEnvFile(string $envKey, bool $active): bool $value = $active ? 'true' : 'false'; // Add to end of file - $content = rtrim($content) . "\n{$envKey}={$value}\n"; + $content = rtrim($content)."\n{$envKey}={$value}\n"; $this->files->put($envPath, $content); @@ -184,12 +184,11 @@ protected function showManualInstructions(string $name, string $envKey, bool $ac protected function showSummary( string $name, string $envKey, - bool $active, - bool $configUpdated, - bool $envUpdated, - bool $createDb, - ): void - { + bool $active, + bool $configUpdated, + bool $envUpdated, + bool $createDb, + ): void { info("Toggle '{$name}' created!"); if ($configUpdated) { diff --git a/src/Middleware/ShareTogglesWithInertia.php b/src/Middleware/ShareTogglesWithInertia.php new file mode 100644 index 0000000..aef32a7 --- /dev/null +++ b/src/Middleware/ShareTogglesWithInertia.php @@ -0,0 +1,23 @@ + Toggle::all(), + ]; + } +}