Container.php
2 years ago
FeatureProviderInterface.php
4 years ago
FeatureServiceProvider.php
4 years ago
Resolver.php
3 years ago
ServiceProvider.php
3 years ago
FeatureServiceProvider.php
105 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 | |
| 20 | /** |
| 21 | * Class FeatureServiceProvider |
| 22 | * |
| 23 | * @package WPStaging\Framework\DI |
| 24 | */ |
| 25 | abstract class FeatureServiceProvider extends ServiceProvider implements FeatureProviderInterface |
| 26 | { |
| 27 | /** |
| 28 | * Returns the constant, or environment variables, that will trigger the feature provider |
| 29 | * registration when set to truthy values. |
| 30 | * |
| 31 | * Note: if set, the constant MUST override the value of the environment variable. The provider |
| 32 | * registration should still honor its requirements. |
| 33 | * If the constant is defined AND truthy, then the feature MUST still be disabled setting an environment variable |
| 34 | * by the same name as the constant to a falsy value. |
| 35 | * |
| 36 | * @return string The name of the constant, or environment variable, that will trigger the |
| 37 | * feature provider registration when set to truthy values. |
| 38 | * |
| 39 | * @throws WPStagingException If the method is not overridden by the extending Service Provider class. |
| 40 | */ |
| 41 | public static function getFeatureTrigger() |
| 42 | { |
| 43 | die('As I should not be invoked.'); |
| 44 | throw new WPStagingException('Every Feature Service Provider MUST define a feature trigger.'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns whether the feature provided by the provider is enabled or not. |
| 49 | * This is used if a feature is ready and activated in production. |
| 50 | * |
| 51 | * The check will happen on the feature provider trigger by checking if |
| 52 | * the feature is available (the trigger constant is defined and true) and, if so, |
| 53 | * if the environment var by the same name is not set to falsy value. |
| 54 | * |
| 55 | * @return bool Whether the feature provided is enabled or not. |
| 56 | */ |
| 57 | public static function isEnabledInProduction() |
| 58 | { |
| 59 | $trigger = static::getFeatureTrigger(); |
| 60 | |
| 61 | if (defined($trigger) && constant($trigger) === false) { |
| 62 | // The feature will be disabled if the constant is set and `false`. |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (getenv($trigger) !== false && (bool)getenv($trigger) === false) { |
| 67 | // The feature can be disabled by setting an environment variable by the trigger name to a falsy value. |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns whether the feature provided by the provider is enabled or not. |
| 76 | * |
| 77 | * This is used if a feature is still in development and not released in production. |
| 78 | * The default value is false and a feature must be explicitely activated by defining the feature constant and setting it to true |
| 79 | * |
| 80 | * If the feature constant is not set the feature stays disabled. |
| 81 | * |
| 82 | * @return bool Whether the feature provided is enabled or not. |
| 83 | */ |
| 84 | public static function isEnabledInDevelopment() |
| 85 | { |
| 86 | $trigger = static::getFeatureTrigger(); |
| 87 | |
| 88 | if (!defined($trigger)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (defined($trigger) && constant($trigger) === false) { |
| 93 | // The feature will be disabled if the constant is set and `false`. |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if (getenv($trigger) !== false && (bool)getenv($trigger) === false) { |
| 98 | // The feature can be disabled by setting an environment variable by the trigger name to a falsy value. |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 |