-
-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Feature flags. (#2282)
- Loading branch information
Showing
14 changed files
with
216 additions
and
42 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
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,140 @@ | ||
<?php | ||
|
||
namespace App\Assets; | ||
|
||
use App\Exceptions\Internal\FeaturesDoesNotExistsException; | ||
|
||
/** | ||
* Manage enabled and disabled features. | ||
*/ | ||
final class Features | ||
{ | ||
/** | ||
* Determine whether a feature is active. | ||
* | ||
* @param string $featureName to check | ||
* | ||
* @return bool is active | ||
*/ | ||
public static function active(string $featureName): bool | ||
{ | ||
self::exists($featureName); | ||
|
||
return config('features.' . $featureName) === true; | ||
} | ||
|
||
/** | ||
* Determine whether a feature is inactive. | ||
* | ||
* @param string $featureName to check | ||
* | ||
* @return bool is inactive | ||
*/ | ||
public static function inactive(string $featureName): bool | ||
{ | ||
self::exists($featureName); | ||
|
||
return config('features.' . $featureName) === false; | ||
} | ||
|
||
/** | ||
* Determine if all of the given features are active. | ||
* | ||
* @param array<int,string> $featureNames to check | ||
* | ||
* @return bool is inactive | ||
*/ | ||
public static function allAreActive(array $featureNames): bool | ||
{ | ||
return array_reduce( | ||
$featureNames, | ||
fn ($bool, $featureName) => $bool && self::active($featureName), | ||
true); | ||
} | ||
|
||
/** | ||
* Determine if any of the given features are active. | ||
* | ||
* @param array<int,string> $featureNames to check | ||
* | ||
* @return bool is inactive | ||
*/ | ||
public static function someAreActive(array $featureNames): bool | ||
{ | ||
return array_reduce( | ||
$featureNames, | ||
fn (bool $bool, string $featureName) => $bool || self::active($featureName), | ||
false); | ||
} | ||
|
||
/** | ||
* Determine if all of the given features are inactive. | ||
* | ||
* @param array<int,string> $featureNames to check | ||
* | ||
* @return bool is inactive | ||
*/ | ||
public static function allAreInactive(array $featureNames): bool | ||
{ | ||
return array_reduce( | ||
$featureNames, | ||
fn (bool $bool, string $featureName) => $bool && self::inactive($featureName), | ||
true); | ||
} | ||
|
||
/** | ||
* Determine if any of the given features are inactive. | ||
* | ||
* @param array<int,string> $featureNames to check | ||
* | ||
* @return bool is inactive | ||
*/ | ||
public static function someAreInactive(array $featureNames): bool | ||
{ | ||
return array_reduce( | ||
$featureNames, | ||
fn (bool $bool, string $featureName) => $bool || self::inactive($featureName), | ||
false); | ||
} | ||
|
||
/** | ||
* Determine whether a feature is active. | ||
* | ||
* @template T | ||
* | ||
* @param string|array<int,string> $featureNames to check | ||
* @param T|\Closure(): T $valIfTrue what happens or Value if we features are enabled | ||
* @param T|\Closure(): T $valIfFalse what happens or Value if we features are disabled | ||
* | ||
* @return T | ||
*/ | ||
public static function when(string|array $featureNames, mixed $valIfTrue, mixed $valIfFalse): mixed | ||
{ | ||
// Sadly phpstan does not do the type inference as it would do in a if statement. | ||
$retValue = match (is_array($featureNames)) { | ||
true => self::allAreActive($featureNames) ? $valIfTrue : $valIfFalse, // @phpstan-ignore-line | ||
// Parameter #1 $featureNames of static method App\Assets\Features::allAreActive() expects array<int, string>, array<int, string>|string given. | ||
false => self::active($featureNames) ? $valIfTrue : $valIfFalse, // @phpstan-ignore-line | ||
// Parameter #1 $featureName of static method App\Assets\Features::active() expects string, array<int, string>|string given. | ||
}; | ||
|
||
return is_callable($retValue) ? $retValue() : $retValue; | ||
} | ||
|
||
/** | ||
* Assert whether the feature exists or not. | ||
* Throws an exception if not. | ||
* | ||
* @param string $featureName name of the feature to check | ||
* | ||
* @return void | ||
* | ||
* @throws FeaturesDoesNotExistsException | ||
*/ | ||
private static function exists(string $featureName): void | ||
{ | ||
if (!is_bool(config('features.' . $featureName))) { | ||
throw new FeaturesDoesNotExistsException(sprintf('No feature with name %s found.', $featureName)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/Exceptions/Internal/FeaturesDoesNotExistsException.php
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,11 @@ | ||
<?php | ||
|
||
namespace App\Exceptions\Internal; | ||
|
||
class FeaturesDoesNotExistsException extends LycheeLogicException | ||
{ | ||
public function __construct(string $msg) | ||
{ | ||
parent::__construct($msg); | ||
} | ||
} |
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
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,44 @@ | ||
<?php | ||
|
||
return [ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Use Livewire Front-end | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This value determines whether livewire front-end is enabled as it is | ||
| currently under development. | ||
| | ||
*/ | ||
'livewire' => (bool) env('LIVEWIRE_ENABLED', true), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Force HTTPS | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When running behind a proxy, it may be necessary for the urls to be | ||
| set as https for the reverse translation. You should set this if you | ||
| want to force the https scheme. | ||
*/ | ||
'force_https' => (bool) env('APP_FORCE_HTTPS', false), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Enable v4 redirections | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When using new front-end old links to /#albumID/PhotoID are broken. | ||
| This provides here a way to avoid those. | ||
*/ | ||
'legacy_v4_redirect' => (bool) env('LEGACY_V4_REDIRECT', false), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Log Viewer | ||
|-------------------------------------------------------------------------- | ||
| Log Viewer can be disabled, so it's no longer accessible via browser. | ||
| | ||
*/ | ||
'log-viewer' => (bool) env('LOG_VIEWER_ENABLED', true), | ||
]; |
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
Oops, something went wrong.