PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.4.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.4.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.4.0, at src/Performance/Config/Config.php

154 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The configuration class to all and set configuration/settings, as well as fire off
4 * save events.
5 *
6 * @package SolidWP\Performance
7 */
8
9 declare( strict_types=1 );
10
11 namespace SolidWP\Performance\Config;
12
13 use InvalidArgumentException;
14 use SolidWP\Performance\Shutdown\Contracts\Terminable;
15 use SolidWP\Performance\StellarWP\Arrays\Arr;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * The configuration class to all and set configuration/settings, as well as fire off
23 * save events.
24 *
25 * This object is loaded as a singleton, so its values when accessed will
26 * match the current state.
27 *
28 * @package SolidWP\Performance
29 */
30 class Config implements Terminable {
31
32 /**
33 * All configuration items.
34 *
35 * @var array<string, mixed>
36 */
37 private array $items;
38
39 /**
40 * @var Config_Loader
41 */
42 private Config_Loader $loader;
43
44 /**
45 * Whether we're queued up to save on shutdown.
46 *
47 * @var bool
48 */
49 private bool $will_save = false;
50
51 /**
52 * @param Config_Loader $loader The configuration loader.
53 */
54 public function __construct( Config_Loader $loader ) {
55 $this->loader = $loader;
56 $this->items = $this->loader->load();
57 }
58
59 /**
60 * Get all the current configuration items.
61 *
62 * @return array<string, mixed>
63 */
64 public function all(): array {
65 return $this->items;
66 }
67
68 /**
69 * Get a configuration value.
70 *
71 * @param string $key The config key.
72 *
73 * @return mixed
74 */
75 public function get( string $key ) {
76 return Arr::get( $this->items, explode( '.', $key ) );
77 }
78
79 /**
80 * Sets a config item in memory.
81 *
82 * @param string $key The key to set.
83 * @param mixed $value The value to set.
84 *
85 * @throws InvalidArgumentException If you try to set a key that does not exist in the defaults.
86 *
87 * @return Config
88 */
89 public function set( string $key, $value ): Config {
90 if ( ! Arr::exists( $this->items, $key ) ) {
91 throw new InvalidArgumentException( sprintf( 'Invalid config key: "%s"', $key ) );
92 }
93
94 $this->items = Arr::set( $this->items, explode( '.', $key ), $value );
95
96 return $this;
97 }
98
99 /**
100 * Queue up config to save its state to the database on shutdown.
101 *
102 * This prevents writing to the database / config file multiple times.
103 *
104 * @return Config
105 */
106 public function queue(): Config {
107 $this->will_save = true;
108
109 return $this;
110 }
111
112 /**
113 * Manually call hooks that perform config database saves.
114 *
115 * @return void
116 */
117 public function save(): void {
118 do_action( 'solidwp/performance/config/save' );
119 }
120
121 /**
122 * Save all changes on shutdown.
123 *
124 * @see \SolidWP\Performance\Shutdown\Provider::register()
125 *
126 * @action shutdown
127 * @action solidwp/performance/terminate
128 *
129 * @return void
130 */
131 public function terminate(): void {
132 if ( ! $this->will_save ) {
133 return;
134 }
135
136 $this->save();
137
138 // Shutdown terminable tasks run twice, we only need to save once.
139 $this->will_save = false;
140 }
141
142 /**
143 * Refresh the configuration state, as the database values may have changed
144 * based on where we are in the request lifecycle.
145 *
146 * @return Config
147 */
148 public function refresh(): Config {
149 $this->items = $this->loader->load();
150
151 return $this;
152 }
153 }
154