Container.php
8 months ago
FeatureProviderInterface.php
4 years ago
FeatureServiceProvider.php
6 days ago
Resolver.php
8 months ago
ServiceProvider.php
3 years ago
FeatureServiceProvider.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The base class that models a Service Provider that will completely provide a feature or not. |
| 5 | * |
| 6 | * Feature Service Providers will differ from normal providers in that they completely manage |
| 7 | * a feature and all the required bindings and hooks. Feature Providers follow the concept of a |
| 8 | * truthy constant to enable them, and a falsy value environment variable to disable them if activated. |
| 9 | * This allows Feature Providers to launch a feature "darkly" by simply not setting their trigger constant, |
| 10 | * once launched, users can disable the feature either by setting en environment variable with the same |
| 11 | * name as the constant to a falsy value or by setting the constant to `false` in their `wp-config.php` file. |
| 12 | * |
| 13 | * @package WPStaging\Framework\DI |
| 14 | */ |
| 15 | |
| 16 | namespace WPStaging\Framework\DI; |
| 17 | |
| 18 | use WPStaging\Framework\Exceptions\WPStagingException; |
| 19 | use WPStaging\Framework\Utils\Env; |
| 20 | |
| 21 | /** |
| 22 | * Class FeatureServiceProvider |
| 23 | * |
| 24 | * @package WPStaging\Framework\DI |
| 25 | */ |
| 26 | abstract class FeatureServiceProvider extends ServiceProvider implements FeatureProviderInterface |
| 27 | { |
| 28 | /** |
| 29 | * Returns the constant, or environment variables, that will trigger the feature provider |
| 30 | * registration when set to truthy values. |
| 31 | * |
| 32 | * Note: if set, the constant MUST override the value of the environment variable. The provider |
| 33 | * registration should still honor its requirements. |
| 34 | * If the constant is defined AND truthy, then the feature MUST still be disabled setting an environment variable |
| 35 | * by the same name as the constant to a falsy value. |
| 36 | * |
| 37 | * @return string The name of the constant, or environment variable, that will trigger the |
| 38 | * feature provider registration when set to truthy values. |
| 39 | * |
| 40 | * @throws WPStagingException If the method is not overridden by the extending Service Provider class. |
| 41 | */ |
| 42 | public static function getFeatureTrigger() |
| 43 | { |
| 44 | die('As I should not be invoked.'); |
| 45 | throw new WPStagingException('Every Feature Service Provider MUST define a feature trigger.'); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns whether the feature provided by the provider is enabled or not. |
| 50 | * This is used if a feature is ready and activated in production. |
| 51 | * |
| 52 | * The check will happen on the feature provider trigger by checking if |
| 53 | * the feature is available (the trigger constant is defined and true) and, if so, |
| 54 | * if the environment var by the same name is not set to falsy value. |
| 55 | * |
| 56 | * @return bool Whether the feature provided is enabled or not. |
| 57 | */ |
| 58 | public static function isEnabledInProduction() |
| 59 | { |
| 60 | $trigger = static::getFeatureTrigger(); |
| 61 | |
| 62 | if (defined($trigger) && constant($trigger) === false) { |
| 63 | // The feature will be disabled if the constant is set and `false`. |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | $triggerValue = Env::get($trigger); |
| 68 | if ($triggerValue !== false && (bool)$triggerValue === false) { |
| 69 | // The feature can be disabled by setting an environment variable by the trigger name to a falsy value. |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns whether the feature provided by the provider is enabled or not. |
| 78 | * |
| 79 | * This is used if a feature is still in development and not released in production. |
| 80 | * The default value is false and a feature must be explicitely activated by defining the feature constant and setting it to true |
| 81 | * |
| 82 | * If the feature constant is not set the feature stays disabled. |
| 83 | * |
| 84 | * @return bool Whether the feature provided is enabled or not. |
| 85 | */ |
| 86 | public static function isEnabledInDevelopment() |
| 87 | { |
| 88 | $trigger = static::getFeatureTrigger(); |
| 89 | |
| 90 | if (!defined($trigger)) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if (defined($trigger) && constant($trigger) === false) { |
| 95 | // The feature will be disabled if the constant is set and `false`. |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | $triggerValue = Env::get($trigger); |
| 100 | if ($triggerValue !== false && (bool)$triggerValue === false) { |
| 101 | // The feature can be disabled by setting an environment variable by the trigger name to a falsy value. |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 |