Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel Pennant Sandbox #7

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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

Check warning on line 17 in app/Console/Commands/SandboxActivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxActivate.php#L17

Added line #L17 was not covered by tests
{
$scopes = $this->option('for');

Check warning on line 19 in app/Console/Commands/SandboxActivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxActivate.php#L19

Added line #L19 was not covered by tests

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

Check warning on line 22 in app/Console/Commands/SandboxActivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxActivate.php#L21-L22

Added lines #L21 - L22 were not covered by tests
}

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

Check warning on line 25 in app/Console/Commands/SandboxActivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxActivate.php#L25

Added line #L25 was not covered by tests

return self::SUCCESS;

Check warning on line 27 in app/Console/Commands/SandboxActivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxActivate.php#L27

Added line #L27 was not covered by tests
}
}
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

Check warning on line 16 in app/Console/Commands/SandboxDeactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxDeactivate.php#L16

Added line #L16 was not covered by tests
{
$scopes = $this->option('for');

Check warning on line 18 in app/Console/Commands/SandboxDeactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxDeactivate.php#L18

Added line #L18 was not covered by tests

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

Check warning on line 21 in app/Console/Commands/SandboxDeactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxDeactivate.php#L20-L21

Added lines #L20 - L21 were not covered by tests
}

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

Check warning on line 24 in app/Console/Commands/SandboxDeactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxDeactivate.php#L24

Added line #L24 was not covered by tests

return self::SUCCESS;

Check warning on line 26 in app/Console/Commands/SandboxDeactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxDeactivate.php#L26

Added line #L26 was not covered by tests
}
}
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

Check warning on line 16 in app/Console/Commands/SandboxStatus.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxStatus.php#L16

Added line #L16 was not covered by tests
{
$scopes = $this->option('for');

Check warning on line 18 in app/Console/Commands/SandboxStatus.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxStatus.php#L18

Added line #L18 was not covered by tests

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

Check warning on line 20 in app/Console/Commands/SandboxStatus.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxStatus.php#L20

Added line #L20 was not covered by tests

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

Check warning on line 22 in app/Console/Commands/SandboxStatus.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxStatus.php#L22

Added line #L22 was not covered by tests

return self::SUCCESS;

Check warning on line 24 in app/Console/Commands/SandboxStatus.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/SandboxStatus.php#L24

Added line #L24 was not covered by tests
}
}
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()

Check warning on line 13 in app/Features/Sandbox.php

View check run for this annotation

Codecov / codecov/patch

app/Features/Sandbox.php#L13

Added line #L13 was not covered by tests
{
$this->name = 'sandbox';

Check warning on line 15 in app/Features/Sandbox.php

View check run for this annotation

Codecov / codecov/patch

app/Features/Sandbox.php#L15

Added line #L15 was not covered by tests
}

public function resolve(mixed $scope): Lottery

Check warning on line 18 in app/Features/Sandbox.php

View check run for this annotation

Codecov / codecov/patch

app/Features/Sandbox.php#L18

Added line #L18 was not covered by tests
{
return Lottery::odds(1 / 2);

Check warning on line 20 in app/Features/Sandbox.php

View check run for this annotation

Codecov / codecov/patch

app/Features/Sandbox.php#L20

Added line #L20 was not covered by tests
}

public static function toList($scopes): Collection

Check warning on line 23 in app/Features/Sandbox.php

View check run for this annotation

Codecov / codecov/patch

app/Features/Sandbox.php#L23

Added line #L23 was not covered by tests
{
return Collection::make($scopes)->map(fn(string $scope) => [
$scope,
Feature::for($scope)->active('sandbox') ? 'ON' : 'OFF',
]);

Check warning on line 28 in app/Features/Sandbox.php

View check run for this annotation

Codecov / codecov/patch

app/Features/Sandbox.php#L25-L28

Added lines #L25 - L28 were not covered by tests
}
}
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

Check warning on line 9 in app/Http/Controllers/Feature/Activate.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Activate.php#L9

Added line #L9 was not covered by tests
{
$feature->for($for)->activate($name);

Check warning on line 11 in app/Http/Controllers/Feature/Activate.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Activate.php#L11

Added line #L11 was not covered by tests

return [
'active' => $feature->for($for)->active($name),
];

Check warning on line 15 in app/Http/Controllers/Feature/Activate.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Activate.php#L13-L15

Added lines #L13 - L15 were not covered by tests
}
}
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

Check warning on line 9 in app/Http/Controllers/Feature/Deactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Deactivate.php#L9

Added line #L9 was not covered by tests
{
$feature->for($for)->deactivate($name);

Check warning on line 11 in app/Http/Controllers/Feature/Deactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Deactivate.php#L11

Added line #L11 was not covered by tests

return [
'active' => $feature->for($for)->active($name),
];

Check warning on line 15 in app/Http/Controllers/Feature/Deactivate.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Deactivate.php#L13-L15

Added lines #L13 - L15 were not covered by tests
}
}
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

Check warning on line 9 in app/Http/Controllers/Feature/Status.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Status.php#L9

Added line #L9 was not covered by tests
{
return [
'active' => $feature->for($for)->active($name),
];

Check warning on line 13 in app/Http/Controllers/Feature/Status.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/Feature/Status.php#L11-L13

Added lines #L11 - L13 were not covered by tests
}
}
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);
Loading