Skip to content

Commit

Permalink
improve feature resolve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenjude committed Aug 9, 2024
1 parent dae9d7e commit bf810de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Traits/WithFeatureResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ trait WithFeatureResolver
*/
public function resolve(mixed $scope): bool
{
if (! is_a($scope, config('filament-feature-flags.scope'))) {
if (!is_a($scope, config('filament-feature-flags.scope'))) {
return config('filament-feature-flags.default');
}

/*
* This resolution loop iterates through all segmentations associated with this feature
* and ensures that all resolved segments are true. If any resolved segment returns
* false, this feature resolution will be false.
*/
* This resolution loop iterates through all segmentations associated with this feature
* by checking deactivated segments first, then checking activated segments;
* and if any resolve is True, this feature resolution will be True.
*/
return FeatureSegment::where('feature', get_class($this))
->get()
->whenEmpty(
fn () => config('filament-feature-flags.default'),
fn ($segments) => $segments->map(fn (FeatureSegment $segment) => $segment->resolve($scope))
->doesntContain(
false
) // Makes sure that multiple segmentations are all true, if not resolve as false.
fn() => config('filament-feature-flags.default'),
fn($segments) => $segments->sortBy('active')
->map(fn(FeatureSegment $segment) => $segment->resolve($scope))
->contains(true)
);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

config()->set('pennant.default', 'array');

/*
$migration = include __DIR__.'/../database/migrations/create_filament-feature-flags_table.php.stub';
$migration->up();
Expand Down

0 comments on commit bf810de

Please sign in to comment.