|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A constants manager for Jetpack. |
|
4
|
|
|
* |
|
5
|
|
|
* @package jetpack-constants |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Constants; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Automattic\Jetpack\Constants\Manager |
|
12
|
|
|
* |
|
13
|
|
|
* Testing constants is hard. Once you define a constant, it's defined. Constants Manager is an |
|
14
|
|
|
* abstraction layer so that unit tests can set "constants" for tests. |
|
15
|
|
|
* |
|
16
|
|
|
* To test your code, you'll need to swap out `defined( 'CONSTANT' )` with `Automattic\Jetpack\Constants\Manager::is_defined( 'CONSTANT' )` |
|
17
|
|
|
* and replace `CONSTANT` with `Automattic\Jetpack\Constants\Manager::get_constant( 'CONSTANT' )`. Then in the unit test, you can set the |
|
18
|
|
|
* constant with `Automattic\Jetpack\Constants\Manager::set_constant( 'CONSTANT', $value )` and then clean up after each test with something like |
|
19
|
|
|
* this: |
|
20
|
|
|
* |
|
21
|
|
|
* function tearDown() { |
|
22
|
|
|
* Automattic\Jetpack\Constants\Manager::clear_constants(); |
|
23
|
|
|
* } |
|
24
|
|
|
*/ |
|
25
|
|
|
class Manager { |
|
26
|
|
|
/** |
|
27
|
|
|
* A container for all defined constants. |
|
28
|
|
|
* |
|
29
|
|
|
* @access public |
|
30
|
|
|
* @static |
|
31
|
|
|
* |
|
32
|
|
|
* @var array. |
|
33
|
|
|
*/ |
|
34
|
|
|
public static $set_constants = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Checks if a "constant" has been set in constants Manager, and if not, |
|
38
|
|
|
* checks if the constant was defined with define( 'name', 'value ). |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $name The name of the constant. |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function is_defined( $name ) { |
|
45
|
|
|
return array_key_exists( $name, self::$set_constants ) |
|
46
|
|
|
? true |
|
47
|
|
|
: defined( $name ); |
|
48
|
|
|
} |
|
49
|
|
|
/** |
|
50
|
|
|
* Checks if a "constant" has been set in constants Manager |
|
51
|
|
|
* and has the value of true |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $name The name of the constant. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function is_true( $name ) { |
|
58
|
|
|
return self::is_defined( $name ) && self::get_constant( $name ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Attempts to retrieve the "constant" from constants Manager, and if it hasn't been set, |
|
63
|
|
|
* then attempts to get the constant with the constant() function. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name The name of the constant. |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed null if the constant does not exist or the value of the constant. |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function get_constant( $name ) { |
|
70
|
|
|
if ( array_key_exists( $name, self::$set_constants ) ) { |
|
71
|
|
|
return self::$set_constants[ $name ]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return defined( $name ) ? constant( $name ) : null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets the value of the "constant" within constants Manager. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $name The name of the constant. |
|
81
|
|
|
* @param string $value The value of the constant. |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function set_constant( $name, $value ) { |
|
84
|
|
|
self::$set_constants[ $name ] = $value; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Will unset a "constant" from constants Manager if the constant exists. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name The name of the constant. |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool Whether the constant was removed. |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function clear_single_constant( $name ) { |
|
95
|
|
|
if ( ! array_key_exists( $name, self::$set_constants ) ) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
unset( self::$set_constants[ $name ] ); |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Resets all of the constants within constants Manager. |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function clear_constants() { |
|
107
|
|
|
self::$set_constants = array(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|