|
6 | 6 |
|
7 | 7 | namespace DefStudio\EnumFeatures\Concerns;
|
8 | 8 |
|
9 |
| -use DefStudio\EnumFeatures\Exceptions\FeatureException; |
| 9 | +use BackedEnum; |
| 10 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 11 | +use Laravel\Pennant\Feature as Pennant; |
| 12 | +use Laravel\Pennant\Middleware\EnsureFeaturesAreActive; |
10 | 13 |
|
11 | 14 | trait DefinesFeatures
|
12 | 15 | {
|
13 |
| - public static function enabledFeatures(): array |
| 16 | + public function define(): void |
14 | 17 | {
|
15 |
| - return config('app.features', []); |
| 18 | + Pennant::define($this->featureName(), $this->resolve(...)); |
16 | 19 | }
|
17 | 20 |
|
18 |
| - public function enabled(): bool |
| 21 | + protected function featureName(): string |
19 | 22 | {
|
20 |
| - return in_array($this, self::enabledFeatures()); |
| 23 | + return $this instanceof BackedEnum ? $this->value : $this->name; |
21 | 24 | }
|
22 | 25 |
|
23 |
| - public function enforce(): void |
| 26 | + public function active(?Authenticatable $scope = null): bool |
24 | 27 | {
|
25 |
| - throw_if(! $this->enabled(), FeatureException::notEnabled($this)); |
| 28 | + if ($scope) { |
| 29 | + return Pennant::for($scope)->active($this->featureName()); |
| 30 | + } |
| 31 | + |
| 32 | + return Pennant::active($this->featureName()); |
| 33 | + } |
| 34 | + |
| 35 | + public function enabled(?Authenticatable $scope = null): bool |
| 36 | + { |
| 37 | + return $this->active($scope); |
| 38 | + } |
| 39 | + |
| 40 | + public function inactive(?Authenticatable $scope = null): bool |
| 41 | + { |
| 42 | + return ! $this->active($scope); |
| 43 | + } |
| 44 | + |
| 45 | + public function disabled(?Authenticatable $scope = null): bool |
| 46 | + { |
| 47 | + return $this->inactive($scope); |
| 48 | + } |
| 49 | + |
| 50 | + public function middleware(): string |
| 51 | + { |
| 52 | + return EnsureFeaturesAreActive::using($this->featureName()); |
| 53 | + } |
| 54 | + |
| 55 | + public function purge(): void |
| 56 | + { |
| 57 | + Pennant::purge($this->featureName()); |
| 58 | + } |
| 59 | + |
| 60 | + protected function resolve(?Authenticatable $scope = null): bool |
| 61 | + { |
| 62 | + $featureName = $this->featureName(); |
| 63 | + $camelFeatureName = str($this->featureName())->camel()->ucfirst(); |
| 64 | + |
| 65 | + $try_methods = [ |
| 66 | + "check_$featureName", |
| 67 | + "check_{$featureName}_feature", |
| 68 | + "has_$featureName", |
| 69 | + "has_{$featureName}Feature", |
| 70 | + "check$camelFeatureName", |
| 71 | + "check{$camelFeatureName}Feature", |
| 72 | + "has$camelFeatureName", |
| 73 | + "has{$camelFeatureName}Feature", |
| 74 | + ]; |
| 75 | + |
| 76 | + foreach ($try_methods as $method) { |
| 77 | + if (method_exists($this, $method)) { |
| 78 | + return $this->{$method}($scope); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + public function enforce(?Authenticatable $scope = null): void |
| 86 | + { |
| 87 | + if (! $this->active($scope)) { |
| 88 | + abort(400); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public function activate(?Authenticatable $scope = null): void |
| 93 | + { |
| 94 | + if ($scope) { |
| 95 | + Pennant::for($scope)->activate($this->featureName()); |
| 96 | + |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + Pennant::activate($this->featureName()); |
| 101 | + } |
| 102 | + |
| 103 | + public function enable(?Authenticatable $scope = null): void |
| 104 | + { |
| 105 | + $this->activate($scope); |
| 106 | + } |
| 107 | + |
| 108 | + public function deactivate(?Authenticatable $scope = null): void |
| 109 | + { |
| 110 | + if ($scope) { |
| 111 | + Pennant::for($scope)->deactivate($this->featureName()); |
| 112 | + |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + Pennant::deactivate($this->featureName()); |
| 117 | + } |
| 118 | + |
| 119 | + public function disable(?Authenticatable $scope = null): void |
| 120 | + { |
| 121 | + $this->deactivate($scope); |
| 122 | + } |
| 123 | + |
| 124 | + public function forget(?Authenticatable $scope = null): void |
| 125 | + { |
| 126 | + if ($scope) { |
| 127 | + Pennant::for($scope)->forget($this->featureName()); |
| 128 | + |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + Pennant::forget($this->featureName()); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param array<DefinesFeatures> $features |
| 137 | + */ |
| 138 | + public static function areAllActive(array $features): bool |
| 139 | + { |
| 140 | + return Pennant::allAreInactive(collect($features) |
| 141 | + ->map(fn (self $feature) => $feature->featureName()) |
| 142 | + ->toArray()); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param array<DefinesFeatures> $features |
| 147 | + */ |
| 148 | + public static function someAreActive(array $features): bool |
| 149 | + { |
| 150 | + return Pennant::someAreActive(collect($features) |
| 151 | + ->map(fn (self $feature) => $feature->featureName()) |
| 152 | + ->toArray()); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param array<DefinesFeatures> $features |
| 157 | + */ |
| 158 | + public static function areAllEnabled(array $features): bool |
| 159 | + { |
| 160 | + return self::areAllActive($features); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @param array<DefinesFeatures> $features |
| 165 | + */ |
| 166 | + public static function someAreEnabled(array $features): bool |
| 167 | + { |
| 168 | + return self::someAreActive($features); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @param array<DefinesFeatures> $features |
| 173 | + */ |
| 174 | + public static function areAllInactive(array $features): bool |
| 175 | + { |
| 176 | + return Pennant::allAreInactive(collect($features) |
| 177 | + ->map(fn (self $feature) => $feature->featureName()) |
| 178 | + ->toArray()); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @param array<DefinesFeatures> $features |
| 183 | + */ |
| 184 | + public static function someAreInactive(array $features): bool |
| 185 | + { |
| 186 | + return Pennant::someAreInactive(collect($features) |
| 187 | + ->map(fn (self $feature) => $feature->featureName()) |
| 188 | + ->toArray()); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @param array<DefinesFeatures> $features |
| 193 | + */ |
| 194 | + public static function areAllDisabled(array $features): bool |
| 195 | + { |
| 196 | + return self::areAllInactive($features); |
26 | 197 | }
|
27 | 198 |
|
28 |
| - public function disabled(): bool |
| 199 | + /** |
| 200 | + * @param array<DefinesFeatures> $features |
| 201 | + */ |
| 202 | + public static function someAreDisabled(array $features): bool |
29 | 203 | {
|
30 |
| - return ! $this->enabled(); |
| 204 | + return self::someAreInactive($features); |
31 | 205 | }
|
32 | 206 | }
|
0 commit comments