Skip to content

Commit

Permalink
Laravel Pennant Sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Aug 4, 2023
1 parent 188b9df commit 2d6b6d5
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/Console/Commands/SandboxActivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Console\Commands;

use App\Features\Sandbox;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Laravel\Pennant\FeatureManager;

class SandboxActivate extends Command
{
protected $signature = 'app:sandbox:activate {--for=*}';

protected $description = 'Activate Sandbox feature';

public function handle(FeatureManager $feature): int
{
$scopes = $this->option('for');

if (!empty($scopes)) {
$feature->for($scopes)->activate(Sandbox::class);
}

$this->table(['Scope', 'status'], Sandbox::toList($scopes)->toArray());

return self::SUCCESS;
}
}
28 changes: 28 additions & 0 deletions app/Console/Commands/SandboxDeactivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Console\Commands;

use App\Features\Sandbox;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Laravel\Pennant\FeatureManager;

class SandboxDeactivate extends Command
{
protected $signature = 'app:sandbox:deactivate {--for=*}';

protected $description = 'Deactivate Sandbox feature';

public function handle(FeatureManager $feature): int
{
$scopes = $this->option('for');

if (!empty($scopes)) {
$feature->for($scopes)->deactivate(Sandbox::class);
}

$this->table(['Scope', 'status'], Sandbox::toList($scopes)->toArray());

return self::SUCCESS;
}
}
26 changes: 26 additions & 0 deletions app/Console/Commands/SandboxStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Console\Commands;

use App\Features\Sandbox;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Laravel\Pennant\FeatureManager;

class SandboxStatus extends Command
{
protected $signature = 'app:sandbox:status {--for=*}';

protected $description = 'Inspect status of Sandbox feature';

public function handle(FeatureManager $feature): int
{
$scopes = $this->option('for');

$feature->for($scopes)->load(Sandbox::class);

$this->table(['Scope', 'status'], Sandbox::toList($scopes)->toArray());

return self::SUCCESS;
}
}
30 changes: 30 additions & 0 deletions app/Features/Sandbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Features;

use Illuminate\Support\Collection;
use Illuminate\Support\Lottery;
use Laravel\Pennant\Feature;

class Sandbox
{
public readonly string $name;

public function __construct()
{
$this->name = 'sandbox';
}

public function resolve(mixed $scope): Lottery
{
return Lottery::odds(1 / 2);
}

public static function toList($scopes): Collection
{
return Collection::make($scopes)->map(fn(string $scope) => [
$scope,
Feature::for($scope)->active('sandbox') ? 'ON' : 'OFF',
]);
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Feature/Activate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers\Feature;

use Laravel\Pennant\FeatureManager;

class Activate
{
public function __invoke(FeatureManager $feature, string $for, string $name): array
{
$feature->for($for)->activate($name);

return [
'active' => $feature->for($for)->active($name),
];
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Feature/Deactivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers\Feature;

use Laravel\Pennant\FeatureManager;

class Deactivate
{
public function __invoke(FeatureManager $feature, string $for, string $name): array
{
$feature->for($for)->deactivate($name);

return [
'active' => $feature->for($for)->active($name),
];
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/Feature/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers\Feature;

use Laravel\Pennant\FeatureManager;

class Status
{
public function __invoke(FeatureManager $feature, string $for, string $name): array
{
return [
'active' => $feature->for($for)->active($name),
];
}
}
7 changes: 7 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use App\Http\Controllers\AccountController;
use App\Http\Controllers\Admin\MyCancellationController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\Feature\Activate;
use App\Http\Controllers\Feature\Deactivate;
use App\Http\Controllers\Feature\Status;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\MyOrderController;
use App\Http\Controllers\User\Catalog;
Expand Down Expand Up @@ -56,3 +59,7 @@
Route::get('/user', [UserController::class, 'index'])->name('user.index');
Route::post('/user/address', [UserController::class, 'address'])->name('user.address');
});

Route::get('/feature/status/{for}/{name}', Status::class);
Route::get('/feature/activate/{for}/{name}', Activate::class);
Route::get('/feature/deactivate/{for}/{name}', Deactivate::class);

0 comments on commit 2d6b6d5

Please sign in to comment.