FeatureGating.php
3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Automattic\WooCommerce\Blocks\Domain\Services;
/**
* Service class that handles the feature flags.
*
* @internal
*/
class FeatureGating {
/**
* Current flag value.
*
* @var int
*/
private $flag;
const EXPERIMENTAL_FLAG = 3;
const FEATURE_PLUGIN_FLAG = 2;
const CORE_FLAG = 1;
/**
* Current environment
*
* @var string
*/
private $environment;
const PRODUCTION_ENVIRONMENT = 'production';
const DEVELOPMENT_ENVIRONMENT = 'development';
const TEST_ENVIRONMENT = 'test';
/**
* Constructor
*
* @param int $flag Hardcoded flag value. Useful for tests.
* @param string $environment Hardcoded environment value. Useful for tests.
*/
public function __construct( $flag = 0, $environment = 'unset' ) {
$this->flag = $flag;
$this->environment = $environment;
$this->load_flag();
$this->load_environment();
}
/**
* Set correct flag.
*/
public function load_flag() {
if ( 0 === $this->flag ) {
$default_flag = defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ? self::FEATURE_PLUGIN_FLAG : self::CORE_FLAG;
if ( file_exists( __DIR__ . '/../../../blocks.ini' ) ) {
$allowed_flags = [ self::EXPERIMENTAL_FLAG, self::FEATURE_PLUGIN_FLAG, self::CORE_FLAG ];
$woo_options = parse_ini_file( __DIR__ . '/../../../blocks.ini' );
$this->flag = is_array( $woo_options ) && in_array( intval( $woo_options['woocommerce_blocks_phase'] ), $allowed_flags, true ) ? $woo_options['woocommerce_blocks_phase'] : $default_flag;
} else {
$this->flag = $default_flag;
}
}
}
/**
* Set correct environment.
*/
public function load_environment() {
if ( 'unset' === $this->environment ) {
if ( file_exists( __DIR__ . '/../../../blocks.ini' ) ) {
$allowed_environments = [ self::PRODUCTION_ENVIRONMENT, self::DEVELOPMENT_ENVIRONMENT, self::TEST_ENVIRONMENT ];
$woo_options = parse_ini_file( __DIR__ . '/../../../blocks.ini' );
$this->environment = is_array( $woo_options ) && in_array( $woo_options['woocommerce_blocks_env'], $allowed_environments, true ) ? $woo_options['woocommerce_blocks_env'] : self::PRODUCTION_ENVIRONMENT;
} else {
$this->environment = self::PRODUCTION_ENVIRONMENT;
}
}
}
/**
* Returns the current flag value.
*
* @return int
*/
public function get_flag() {
return $this->flag;
}
/**
* Checks if we're executing the code in an experimental build mode.
*
* @return boolean
*/
public function is_experimental_build() {
return $this->flag >= self::EXPERIMENTAL_FLAG;
}
/**
* Checks if we're executing the code in an feature plugin or experimental build mode.
*
* @return boolean
*/
public function is_feature_plugin_build() {
return $this->flag >= self::FEATURE_PLUGIN_FLAG;
}
/**
* Returns the current environment value.
*
* @return string
*/
public function get_environment() {
return $this->environment;
}
/**
* Checks if we're executing the code in an development environment.
*
* @return boolean
*/
public function is_development_environment() {
return self::DEVELOPMENT_ENVIRONMENT === $this->environment;
}
/**
* Checks if we're executing the code in a production environment.
*
* @return boolean
*/
public function is_production_environment() {
return self::PRODUCTION_ENVIRONMENT === $this->environment;
}
/**
* Checks if we're executing the code in a test environment.
*
* @return boolean
*/
public function is_test_environment() {
return self::TEST_ENVIRONMENT === $this->environment;
}
/**
* Returns core flag value.
*
* @return number
*/
public static function get_core_flag() {
return self::CORE_FLAG;
}
/**
* Returns feature plugin flag value.
*
* @return number
*/
public static function get_feature_plugin_flag() {
return self::FEATURE_PLUGIN_FLAG;
}
/**
* Returns experimental flag value.
*
* @return number
*/
public static function get_experimental_flag() {
return self::EXPERIMENTAL_FLAG;
}
}