-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
532 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\FeatureFlags\Contracts; | ||
|
||
interface Maintenance | ||
{ | ||
public function active(): bool; | ||
|
||
public function parameters(): ?array; | ||
|
||
public function callActivation(array $properties): void; | ||
|
||
public function callDeactivation(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\FeatureFlags\Middlewares; | ||
|
||
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as BasePreventRequestsDuringMaintenance; | ||
|
||
class PreventRequestsDuringMaintenance extends BasePreventRequestsDuringMaintenance | ||
{ | ||
public function getExcludedPaths() | ||
{ | ||
return $this->app->maintenanceMode()->data()['except'] ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\FeatureFlags\Support; | ||
|
||
use Illuminate\Contracts\Foundation\MaintenanceMode; | ||
use YlsIdeas\FeatureFlags\Contracts\Maintenance as MaintenanceContract; | ||
|
||
class MaintenanceDriver implements MaintenanceMode | ||
{ | ||
public function __construct(protected MaintenanceContract $features) | ||
{ | ||
} | ||
|
||
public function activate(array $payload): void | ||
{ | ||
$this->features->callActivation($payload); | ||
} | ||
|
||
public function deactivate(): void | ||
{ | ||
$this->features->callDeactivation(); | ||
} | ||
|
||
public function active(): bool | ||
{ | ||
return $this->features->active(); | ||
} | ||
|
||
public function data(): array | ||
{ | ||
return $this->features->parameters() ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\FeatureFlags\Support; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
use YlsIdeas\FeatureFlags\Contracts\Features; | ||
use YlsIdeas\FeatureFlags\Contracts\Maintenance; | ||
|
||
class MaintenanceRepository implements Maintenance | ||
{ | ||
public array $scenarios = []; | ||
|
||
public ?MaintenanceScenario $foundScenario = null; | ||
protected \Closure $uponActivation; | ||
protected \Closure $uponDeactivation; | ||
|
||
public function __construct(protected Features $features, protected Container $container) | ||
{ | ||
} | ||
|
||
public function uponActivation(callable $callable): static | ||
{ | ||
$this->uponActivation = \Closure::fromCallable($callable); | ||
|
||
return $this; | ||
} | ||
|
||
public function uponDeactivation(callable $callable): static | ||
{ | ||
$this->uponDeactivation = \Closure::fromCallable($callable); | ||
|
||
return $this; | ||
} | ||
|
||
public function callActivation(array $properties): void | ||
{ | ||
$this->container->call($this->uponActivation, [ | ||
'properties' => $properties, 'features' => $this->features, | ||
]); | ||
} | ||
|
||
public function callDeactivation(): void | ||
{ | ||
$this->container->call($this->uponDeactivation, ['features' => $this->features]); | ||
} | ||
|
||
public function onEnabled($feature): MaintenanceScenario | ||
{ | ||
return tap((new MaintenanceScenario())->whenEnabled($feature), function (MaintenanceScenario $scenario) { | ||
$this->scenarios[] = $scenario; | ||
}); | ||
} | ||
|
||
public function onDisabled($feature): MaintenanceScenario | ||
{ | ||
return tap((new MaintenanceScenario())->whenDisabled($feature), function (MaintenanceScenario $scenario) { | ||
$this->scenarios[] = $scenario; | ||
}); | ||
} | ||
|
||
public function active(): bool | ||
{ | ||
return (bool) $this->findScenario(); | ||
} | ||
|
||
public function parameters(): ?array | ||
{ | ||
return $this->foundScenario?->toArray(); | ||
} | ||
|
||
protected function findScenario(): ?MaintenanceScenario | ||
{ | ||
return $this->foundScenario = collect($this->scenarios) | ||
->first(function (MaintenanceScenario $scenario) { | ||
if ($scenario->onEnabled && $this->features->accessible($scenario->feature)) { | ||
return true; | ||
} | ||
if (! $scenario->onEnabled && ! $this->features->accessible($scenario->feature)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\FeatureFlags\Support; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
|
||
class MaintenanceScenario implements Arrayable | ||
{ | ||
public string $feature; | ||
public bool $onEnabled; | ||
|
||
protected array $attributes = []; | ||
|
||
public function whenEnabled(string $feature): static | ||
{ | ||
$this->feature = $feature; | ||
$this->onEnabled = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function whenDisabled(string $feature): static | ||
{ | ||
$this->feature = $feature; | ||
$this->onEnabled = false; | ||
|
||
return $this; | ||
} | ||
|
||
public function refresh(int $seconds): static | ||
{ | ||
$this->attributes['refresh'] = (string) $seconds; | ||
|
||
return $this; | ||
} | ||
|
||
public function statusCode(int $status): static | ||
{ | ||
$this->attributes['status'] = $status; | ||
|
||
return $this; | ||
} | ||
|
||
public function retry(int $seconds): static | ||
{ | ||
$this->attributes['retry'] = $seconds; | ||
|
||
return $this; | ||
} | ||
|
||
public function secret(string $secret): static | ||
{ | ||
$this->attributes['secret'] = $secret; | ||
|
||
return $this; | ||
} | ||
|
||
public function redirect(string $url): static | ||
{ | ||
$this->attributes['redirect'] = $url; | ||
|
||
return $this; | ||
} | ||
|
||
public function template(string $html): static | ||
{ | ||
$this->attributes['template'] = $html; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string[] $urls | ||
*/ | ||
public function exceptPaths(array $urls): static | ||
{ | ||
$this->attributes['except'] = $urls; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use YlsIdeas\FeatureFlags\Middlewares\PreventRequestsDuringMaintenance as Middleware; | ||
|
||
class PreventRequestsDuringMaintenance extends Middleware | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\FeatureFlags\Tests; | ||
|
||
class Kernel extends \Orchestra\Testbench\Foundation\Http\Kernel | ||
{ | ||
protected $middleware = [ | ||
\YlsIdeas\FeatureFlags\Middlewares\PreventRequestsDuringMaintenance::class, | ||
]; | ||
} |
Oops, something went wrong.