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.
feat: Make overriders definition more extendable
Adjust the way config works around overriders so that other overriders may be defined outside the package using the manager pattern
- Loading branch information
jeremynikolic
committed
Jun 12, 2024
1 parent
9eeb0bc
commit 4ab637e
Showing
9 changed files
with
273 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?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 | ||
{ | ||
/** @var bool|null $overrideAll */ | ||
$overrideAll = $this->config->get('feature-flags.overriders.array.overrider-all'); | ||
/** @var array<string, bool|null> $overrideAll */ | ||
$overrides = $this->config->get('feature-flags.overriders.array.overrides', []); | ||
|
||
return new InMemoryOverrider($overrides, $overrideAll); | ||
Check failure on line 27 in src/FeatureFlagsOverriderManager.php GitHub Actions / phpstan
|
||
} | ||
|
||
public function getDefaultDriver(): string | ||
{ | ||
return strval($this->config->get('feature-flags.overrider')); | ||
} | ||
} |
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,69 @@ | ||
<?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 | ||
{ | ||
|
||
/** | ||
* @param array<string, bool|null> $overrides | ||
* @param bool|null $overrideAll | ||
*/ | ||
public function __construct(private array $overrides = [], 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 setOverrideAll(bool|null $overrideAll = null): self | ||
{ | ||
$this->overrideAll = $overrideAll; | ||
return $this; | ||
} | ||
|
||
public function setKey(FeatureFlagEnum $key, mixed $value): self | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Tests\Feature; | ||
|
||
use Worksome\FeatureFlags\Overriders\InMemoryOverrider; | ||
use Worksome\FeatureFlags\Tests\Enums\TestFeatureFlag; | ||
|
||
beforeEach(function () { | ||
|
||
$this->inMemoryOverrider = new InMemoryOverrider(); | ||
}); | ||
|
||
test('has returns false if override key is not present', function () { | ||
expect($this->inMemoryOverrider->has(TestFeatureFlag::TestFlag))->toBeFalse(); | ||
}); | ||
|
||
test('has returns false if override key is present but null', function () { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, null); | ||
expect($this->inMemoryOverrider->has(TestFeatureFlag::TestFlag))->toBeFalse(); | ||
}); | ||
|
||
test('has returns true if override key is present with truthy value', function ($value) { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, $value); | ||
expect($this->inMemoryOverrider->has(TestFeatureFlag::TestFlag))->toBeTrue(); | ||
})->with([ | ||
true, | ||
1, | ||
1.0, | ||
'test', | ||
[1], | ||
]); | ||
|
||
test('has returns true if override key is present with falsy value', function ($value) { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, $value); | ||
expect($this->inMemoryOverrider->has(TestFeatureFlag::TestFlag))->toBeTrue(); | ||
})->with([ | ||
false, | ||
0, | ||
0.0, | ||
'', | ||
'0', | ||
[[]], | ||
]); | ||
|
||
test('get returns true if override key is present with truthy value', function ($value) { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, $value); | ||
expect($this->inMemoryOverrider->get(TestFeatureFlag::TestFlag))->toBeTrue(); | ||
})->with([ | ||
true, | ||
1, | ||
1.0, | ||
'test', | ||
[1], | ||
]); | ||
|
||
test('get returns false if override key is present with falsy value', function ($value) { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, $value); | ||
expect($this->inMemoryOverrider->get(TestFeatureFlag::TestFlag))->toBeFalse(); | ||
})->with([ | ||
null, | ||
false, | ||
0, | ||
0.0, | ||
'', | ||
'0', | ||
[[]], | ||
]); | ||
|
||
test('get returns false if override key is not present', function () { | ||
expect($this->inMemoryOverrider->get(TestFeatureFlag::TestFlag))->toBeFalse(); | ||
}); | ||
|
||
test('getAll returns true if override key is present with truthy value', function ($value) { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, $value); | ||
expect($this->inMemoryOverrider->get(TestFeatureFlag::TestFlag))->toBeTrue(); | ||
})->with([ | ||
true, | ||
1, | ||
1.0, | ||
'test', | ||
[1], | ||
]); | ||
|
||
test('getAll returns false if override key is present with falsy value', function ($value) { | ||
$this->inMemoryOverrider->setKey(TestFeatureFlag::TestFlag, $value); | ||
expect($this->inMemoryOverrider->get(TestFeatureFlag::TestFlag))->toBeFalse(); | ||
})->with([ | ||
null, | ||
false, | ||
0, | ||
0.0, | ||
'', | ||
'0', | ||
[[]], | ||
]); | ||
|
||
test('getAll returns false if override key is not present', function () { | ||
expect($this->inMemoryOverrider->get(TestFeatureFlag::TestFlag))->toBeFalse(); | ||
}); |
Oops, something went wrong.