PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.1.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.1.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Config / Config.php

Config.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.1.0, at src/Performance/Config/Config.php

131 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class to save and retrieve settings.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 namespace SolidWP\Performance\Config;
11
12 use SolidWP\Performance\Config\Contracts\Strategy;
13 use SolidWP\Performance\StellarWP\Arrays\Arr;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * A class to save and retrieve settings.
21 *
22 * @since 0.1.0
23 *
24 * @package SolidWP\Performance
25 */
26 class Config {
27
28 public const OPTION_KEY = 'solid_performance_settings';
29
30 /**
31 * The strategy used in determining how config values should be stored and retrieved.
32 *
33 * @since 0.1.0
34 *
35 * @var Strategy
36 */
37 private Strategy $strategy;
38
39 /**
40 * A set of default configuration values.
41 *
42 * @since 0.1.0
43 *
44 * @var array<string,array<string,mixed>>
45 */
46 private static array $defaults = [
47 'page_cache' => [
48 'cache_dir' => WP_CONTENT_DIR . '/cache',
49 'debug' => false,
50 'enabled' => true,
51 'expiration' => 86400, // One day in seconds.
52 'exclusions' => [],
53 ],
54 ];
55
56 /**
57 * An array of changes that will be made to the config when saved.
58 *
59 * @since 0.1.0
60 *
61 * @var array<string,mixed>
62 */
63 private array $changes = [];
64
65 /**
66 * The class constructor.
67 *
68 * @param Strategy $strategy The strategy used in determining how config values should be stored and retrieved.
69 *
70 * @since 0.1.0
71 */
72 public function __construct( Strategy $strategy ) {
73 $this->strategy = $strategy;
74 }
75
76 /**
77 * Gets a specific value from the configuration determined by the registered strategy (or strategy pipeline).
78 * If no value is found, the default value will be returned.
79 *
80 * @since 0.1.0
81 *
82 * @param string $key The key of the value to receive.
83 *
84 * @return mixed
85 */
86 public function get( string $key ) {
87 $value = $this->strategy->get( $key );
88
89 if ( $value === null ) {
90 return Arr::get( self::$defaults, explode( '.', $key ) );
91 }
92
93 return $value;
94 }
95
96 /**
97 * Saves configuration values via the registered strategy.
98 *
99 * @since 0.1.0
100 *
101 * @return void
102 */
103 public function save() {
104 $settings = Arr::merge_recursive( self::$defaults, $this->changes );
105 $this->strategy->save( Arr::dot( $settings ) );
106 }
107
108 /**
109 * Sets a key and value change in the config.
110 *
111 * @since 0.1.0
112 *
113 * @param string $key The key of the change.
114 * @param mixed $value The value of the change.
115 *
116 * @return void
117 */
118 public function set( string $key, $value ) {
119 $this->changes = Arr::set( $this->changes, explode( '.', $key ), $value );
120 }
121
122 /**
123 * Get the default values.
124 *
125 * @return array
126 */
127 public static function get_defaults(): array {
128 return self::$defaults;
129 }
130 }
131