Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.14% |
8 / 14 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Registry | |
57.14% |
8 / 14 |
|
62.50% |
5 / 8 |
15.38 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| remove | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| keys | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| all | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Features; |
| 3 | |
| 4 | use WP_Forge\Options\Options; |
| 5 | |
| 6 | /** |
| 7 | * Registry of Feature instances |
| 8 | * For managing features within brand plugins. |
| 9 | */ |
| 10 | class Registry { |
| 11 | |
| 12 | /** |
| 13 | * Array of Features Instances |
| 14 | * |
| 15 | * @var Array ( $name => Instance ) |
| 16 | */ |
| 17 | private $features = array(); |
| 18 | |
| 19 | /** |
| 20 | * Options object |
| 21 | * See https://github.com/wp-forge/wp-options |
| 22 | * Key value pairs for feature name and enabled boolean |
| 23 | * This is saved to options table in database |
| 24 | * |
| 25 | * @var Options |
| 26 | */ |
| 27 | private $options; |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | $this->options = new Options( 'newfold_features' ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Checks if a feature is registered. |
| 38 | * |
| 39 | * @param string $name The feature name. |
| 40 | * @return bool True if registered, false otherwise. |
| 41 | */ |
| 42 | public function has( $name ) { |
| 43 | return isset( $this->features[ $name ] ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Registers a feature with the registry. |
| 48 | * |
| 49 | * @param string $className The feature class name. |
| 50 | */ |
| 51 | public function set( $className ) { |
| 52 | $instance = new $className( $this->options ); |
| 53 | $name = $instance->getName(); |
| 54 | $this->features[ $name ] = $instance; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Retrieves a feature instance by name. |
| 59 | * |
| 60 | * @param string $name The feature name. |
| 61 | * @return mixed|null The feature instance if found, null otherwise. |
| 62 | */ |
| 63 | public function get( $name ) { |
| 64 | if ( $this->has( $name ) ) { |
| 65 | return $this->features[ $name ]; |
| 66 | } else { |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Removes a feature from the registry. |
| 73 | * |
| 74 | * @param string $name The feature name. |
| 75 | */ |
| 76 | public function remove( $name ) { |
| 77 | unset( $this->features[ $name ] ); |
| 78 | $this->options->delete( $name ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns all registered feature names. |
| 83 | * |
| 84 | * @return array The list of feature names. |
| 85 | */ |
| 86 | public function keys() { |
| 87 | return array_keys( $this->features ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Retrieves all registered features and values from option |
| 92 | * |
| 93 | * @return array The list of features. |
| 94 | */ |
| 95 | public function all() { |
| 96 | return $this->options->all(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Resets the registry by clearing all features. |
| 101 | */ |
| 102 | public function reset() { |
| 103 | // populate with an empty array |
| 104 | $this->features = array(); |
| 105 | $this->options->populate( array() ); |
| 106 | } |
| 107 | } |