Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CacheManager | |
0.00% |
0 / 24 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| classMap | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| registered_cache_types | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| enabled_cache_types | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| get_instances | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Cache; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Performance\Cache\Types\CacheBase; |
| 6 | use NewfoldLabs\WP\ModuleLoader\Container; |
| 7 | use WP_Forge\Collection\Collection; |
| 8 | |
| 9 | /** |
| 10 | * Cache manager. |
| 11 | */ |
| 12 | class CacheManager { |
| 13 | |
| 14 | /** |
| 15 | * The option name where the cache level is stored. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | public const OPTION_CACHE_LEVEL = 'newfold_cache_level'; |
| 20 | |
| 21 | /** |
| 22 | * Allowed cache level values. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | public const VALID_CACHE_LEVELS = array( 0, 1, 2, 3 ); |
| 27 | |
| 28 | /** |
| 29 | * Dependency injection container. |
| 30 | * |
| 31 | * @var Container |
| 32 | */ |
| 33 | protected $container; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | * |
| 38 | * @param Container $container the container |
| 39 | */ |
| 40 | public function __construct( Container $container ) { |
| 41 | $this->container = $container; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Map of cache types to class names. |
| 46 | * |
| 47 | * @return string[] |
| 48 | */ |
| 49 | protected function classMap() { |
| 50 | return array( |
| 51 | 'browser' => __NAMESPACE__ . '\\Types\\Browser', |
| 52 | 'cloudflare' => __NAMESPACE__ . '\\Types\\Cloudflare', |
| 53 | 'file' => __NAMESPACE__ . '\\Types\\File', |
| 54 | 'nginx' => __NAMESPACE__ . '\\Types\\Nginx', |
| 55 | 'sitelock' => __NAMESPACE__ . '\\Types\\Sitelock', |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get a list of registered cache types. |
| 61 | * |
| 62 | * @return string[] |
| 63 | */ |
| 64 | public function registered_cache_types() { |
| 65 | return array_keys( $this->classMap() ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get a list of enabled cache types. |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | public function enabled_cache_types() { |
| 74 | $default_cache_types = array( 'browser' ); |
| 75 | |
| 76 | if ( $this->container->has( 'cache_types' ) ) { |
| 77 | $provided_types = $this->container->get( 'cache_types' ); |
| 78 | } else { |
| 79 | $provided_types = $default_cache_types; |
| 80 | } |
| 81 | |
| 82 | return is_array( $provided_types ) |
| 83 | ? array_intersect( array_map( 'strtolower', $provided_types ), $this->registered_cache_types() ) |
| 84 | : $default_cache_types; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Get an array of page cache type instances based on the enabled cache types. |
| 90 | * |
| 91 | * @return CacheBase[] An array of cache type instances. |
| 92 | */ |
| 93 | public function get_instances() { |
| 94 | $instances = array(); |
| 95 | $collection = new Collection( $this->classMap() ); |
| 96 | $map = $collection->only( $this->enabled_cache_types() ); |
| 97 | foreach ( $map as $type => $class ) { |
| 98 | /** |
| 99 | * CacheBase instance. |
| 100 | * |
| 101 | * @var CacheBase $class |
| 102 | */ |
| 103 | if ( $class::should_enable( $this->container ) ) { |
| 104 | $instances[ $type ] = new $class(); |
| 105 | $instances[ $type ]->setContainer( $this->container ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $instances; |
| 110 | } |
| 111 | } |