# solid-performance/1.1.0/src/Performance/Config/Config.php

Solid Performance – Your No-Code Caching, Performance, &amp; Page Speed Solution, version 1.1.0. 131 lines.

- Page: https://pluginprobe.com/plugins/solid-performance/1.1.0/code/src/Performance/Config/Config.php
- Raw: https://pluginprobe.com/plugins/solid-performance/1.1.0/raw/src/Performance/Config/Config.php
- Modified: 2024-08-12T20:38:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/solid-performance/1.1.0/code/src/Performance/Config/Config.php#L10-L20`.

```php
<?php
/**
 * A class to save and retrieve settings.
 *
 * @since 0.1.0
 *
 * @package SolidWP\Performance
 */

namespace SolidWP\Performance\Config;

use SolidWP\Performance\Config\Contracts\Strategy;
use SolidWP\Performance\StellarWP\Arrays\Arr;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * A class to save and retrieve settings.
 *
 * @since 0.1.0
 *
 * @package SolidWP\Performance
 */
class Config {

	public const OPTION_KEY = 'solid_performance_settings';

	/**
	 * The strategy used in determining how config values should be stored and retrieved.
	 *
	 * @since 0.1.0
	 *
	 * @var Strategy
	 */
	private Strategy $strategy;

	/**
	 * A set of default configuration values.
	 *
	 * @since 0.1.0
	 *
	 * @var array<string,array<string,mixed>>
	 */
	private static array $defaults = [
		'page_cache' => [
			'cache_dir'  => WP_CONTENT_DIR . '/cache',
			'debug'      => false,
			'enabled'    => true,
			'expiration' => 86400, // One day in seconds.
			'exclusions' => [],
		],
	];

	/**
	 * An array of changes that will be made to the config when saved.
	 *
	 * @since 0.1.0
	 *
	 * @var array<string,mixed>
	 */
	private array $changes = [];

	/**
	 * The class constructor.
	 *
	 * @param Strategy $strategy The strategy used in determining how config values should be stored and retrieved.
	 *
	 * @since 0.1.0
	 */
	public function __construct( Strategy $strategy ) {
		$this->strategy = $strategy;
	}

	/**
	 * Gets a specific value from the configuration determined by the registered strategy (or strategy pipeline).
	 * If no value is found, the default value will be returned.
	 *
	 * @since 0.1.0
	 *
	 * @param string $key The key of the value to receive.
	 *
	 * @return mixed
	 */
	public function get( string $key ) {
		$value = $this->strategy->get( $key );

		if ( $value === null ) {
			return Arr::get( self::$defaults, explode( '.', $key ) );
		}

		return $value;
	}

	/**
	 * Saves configuration values via the registered strategy.
	 *
	 * @since 0.1.0
	 *
	 * @return void
	 */
	public function save() {
		$settings = Arr::merge_recursive( self::$defaults, $this->changes );
		$this->strategy->save( Arr::dot( $settings ) );
	}

	/**
	 * Sets a key and value change in the config.
	 *
	 * @since 0.1.0
	 *
	 * @param string $key   The key of the change.
	 * @param mixed  $value The value of the change.
	 *
	 * @return void
	 */
	public function set( string $key, $value ) {
		$this->changes = Arr::set( $this->changes, explode( '.', $key ), $value );
	}

	/**
	 * Get the default values.
	 *
	 * @return array
	 */
	public static function get_defaults(): array {
		return self::$defaults;
	}
}

```
