PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / DI / FeatureServiceProvider.php
wp-staging / Framework / DI Last commit date
Container.php 1 year ago FeatureProviderInterface.php 4 years ago FeatureServiceProvider.php 4 years ago Resolver.php 1 year 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