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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
})
Comment on lines +191 to +200

Copilot AI Jan 14, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation states "Replace your HandleInertiaRequests middleware" but the code example shows appending the middleware using append: [ShareTogglesWithInertia::class]. This is inconsistent and misleading. The middleware extends Inertia\Middleware and overrides the share method to add flags, so it should indeed replace the default HandleInertiaRequests middleware, not be appended to it. The code example should either use a replacement approach or the description should be clarified to indicate that this extends the existing HandleInertiaRequests setup.

Copilot uses AI. Check for mistakes.
```

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
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 14 additions & 15 deletions src/Commands/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,17 +25,16 @@ class CreateCommand extends Command

public function __construct(
protected Filesystem $files,
)
{
) {
parent::__construct();
}

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);

Expand All @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions src/Middleware/ShareTogglesWithInertia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace OffloadProject\Toggle\Middleware;

use Illuminate\Http\Request;
use Inertia\Middleware;
use OffloadProject\Toggle\Facades\Toggle;

class ShareTogglesWithInertia extends Middleware
{
/**
* Share Toggle feature flags with Inertia
*/
public function share(Request $request): array
{
return [
...parent::share($request),
'flags' => Toggle::all(),
];
}
Comment on lines +11 to +22

Copilot AI Jan 14, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new ShareTogglesWithInertia middleware lacks test coverage. The project has comprehensive test coverage for other features (as seen in tests/Feature/), and this new middleware should also have tests to verify that it correctly shares toggles with Inertia props. Consider adding a test that verifies the middleware properly calls Toggle::all() and includes the flags in the shared props.

Copilot uses AI. Check for mistakes.
}