generated from worksome/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from worksome/feat_overrider_refactor
Refactor feature flag overrider architecture to make use of the manager pattern
- Loading branch information
Showing
12 changed files
with
395 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ testbench.yaml | |
vendor | ||
node_modules | ||
.php-cs-fixer.cache | ||
|
||
.phpunit.result.cache |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags; | ||
|
||
use Illuminate\Support\Manager; | ||
use Worksome\FeatureFlags\Overriders\ConfigOverrider; | ||
use Worksome\FeatureFlags\Overriders\InMemoryOverrider; | ||
|
||
class FeatureFlagsOverriderManager extends Manager | ||
{ | ||
public function createConfigDriver(): ConfigOverrider | ||
{ | ||
return new ConfigOverrider( | ||
$this->config, | ||
); | ||
} | ||
|
||
public function createInMemoryDriver(): InMemoryOverrider | ||
{ | ||
return new InMemoryOverrider(); | ||
} | ||
|
||
public function getDefaultDriver(): string | ||
{ | ||
return strval($this->config->get('feature-flags.overrider')); // @phpstan-ignore-line | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Overriders; | ||
|
||
use Illuminate\Support\Arr; | ||
use Worksome\FeatureFlags\Contracts\FeatureFlagEnum; | ||
use Worksome\FeatureFlags\Contracts\FeatureFlagOverrider; | ||
|
||
class InMemoryOverrider implements FeatureFlagOverrider | ||
{ | ||
/** | ||
* @var array<string, bool|null> $overrides | ||
*/ | ||
private array $overrides = []; | ||
|
||
/** | ||
* @var bool|null $overrideAll | ||
*/ | ||
private bool|null $overrideAll = null; | ||
|
||
/** | ||
* Note: a flag key with null as value is considered not present, will return false | ||
*/ | ||
public function has(FeatureFlagEnum $key): bool | ||
{ | ||
return Arr::has($this->overrides, $key->value) | ||
&& Arr::get($this->overrides, $key->value) !== null; | ||
} | ||
|
||
public function get(FeatureFlagEnum $key): bool | ||
{ | ||
return (bool) Arr::get($this->overrides, $key->value, false); | ||
} | ||
|
||
/** | ||
* Note: null value is considered not present, will return false | ||
*/ | ||
public function hasAll(): bool | ||
{ | ||
return $this->overrideAll !== null; | ||
} | ||
|
||
public function getAll(): bool | ||
{ | ||
return (bool) $this->overrideAll; | ||
} | ||
|
||
public function setAll(bool|null $value = null): static | ||
{ | ||
$this->overrideAll = $value; | ||
return $this; | ||
} | ||
|
||
public function set(FeatureFlagEnum $key, mixed $value): static | ||
{ | ||
Arr::set($this->overrides, $key->value, $value); | ||
return $this; | ||
} | ||
|
||
public function overrides(array|null $overriders): array|self | ||
{ | ||
if ($overriders) { | ||
$this->overrides = $overriders; | ||
return $this; | ||
} | ||
return $this->overrides; | ||
} | ||
} |
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
Oops, something went wrong.