Skip to content

Commit 40d6c9c

Browse files
authored
Merge pull request #24 from RVxLab/main
Allow features to override the default value
2 parents a1faec8 + ba72998 commit 40d6c9c

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,49 @@ class WalletFunding
8484
}
8585
```
8686

87+
## Overriding Default Values per Feature
88+
89+
If you need more control over the default value of a feature, you can either add a `defaultValue` property.
90+
91+
```php
92+
<?php
93+
94+
namespace App\Features;
95+
96+
use Stephenjude\FilamentFeatureFlag\Traits\WithFeatureResolver;
97+
98+
class WalletFunding
99+
{
100+
use WithFeatureResolver;
101+
102+
protected bool $defaultValue = false;
103+
}
104+
```
105+
106+
Or a `defineValue` method.
107+
108+
```php
109+
<?php
110+
111+
namespace App\Features;
112+
113+
use Stephenjude\FilamentFeatureFlag\Traits\WithFeatureResolver;
114+
115+
class WalletFunding
116+
{
117+
use WithFeatureResolver;
118+
119+
protected function defaultValue(mixed $scope): bool
120+
{
121+
return false;
122+
}
123+
}
124+
```
125+
126+
The result of these methods will get cast to a boolean.
127+
128+
If neither are defined, the default value gets fetched from the `filament-feature-flags.default` config entry.
129+
87130
## Feature Segmentation
88131
By default, this package resolves scope using the `App\Models\User` model and the default segment applies features for individual or group of users by email.
89132

src/Traits/WithFeatureResolver.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait WithFeatureResolver
1313
public function resolve(mixed $scope): bool
1414
{
1515
if (! is_a($scope, config('filament-feature-flags.scope'))) {
16-
return config('filament-feature-flags.default');
16+
return $this->resolveDefaultValue($scope);
1717
}
1818

1919
/*
@@ -24,13 +24,29 @@ public function resolve(mixed $scope): bool
2424
return FeatureSegment::where('feature', get_class($this))
2525
->get()
2626
->whenEmpty(
27-
fn () => config('filament-feature-flags.default'),
27+
fn () => $this->resolveDefaultValue($scope),
2828
fn ($segments) => $segments->sortBy('active')
2929
->map(fn (FeatureSegment $segment) => $segment->resolve($scope))
3030
->contains(true)
3131
);
3232
}
3333

34+
/**
35+
* Resolve the default value from the feature flag if possible.
36+
*/
37+
protected function resolveDefaultValue(mixed $scope): bool
38+
{
39+
if (property_exists($this, 'defaultValue')) {
40+
return (bool) $this->defaultValue;
41+
}
42+
43+
if (method_exists($this, 'defaultValue')) {
44+
return (bool) $this->defaultValue($scope);
45+
}
46+
47+
return config('filament-feature-flags.default');
48+
}
49+
3450
public static function title(): string
3551
{
3652
return str(class_basename(self::class))->headline()->toString();

0 commit comments

Comments
 (0)