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