-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedConfig.php
More file actions
109 lines (97 loc) · 3.77 KB
/
Copy pathCachedConfig.php
File metadata and controls
109 lines (97 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
/**
* This file is part of php-fast-forward/config.
*
* This source file is subject to the license bundled
* with this source code in the file LICENSE.
*
* @link https://github.com/php-fast-forward/config
* @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace FastForward\Config;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
/**
* Class CachedConfig.
*
* Provides a cached implementation of a configuration source.
* This class MUST cache the configuration output of the decorated ConfigInterface instance.
* It SHALL lazily initialize and retrieve cached configuration data upon invocation.
*/
final class CachedConfig implements ConfigInterface
{
use LazyLoadConfigTrait;
/**
* Constructs a CachedConfig wrapper.
*
* This constructor SHALL accept a PSR-16 cache implementation and a configuration instance
* to be cached. It MUST defer reading and writing the configuration until invoked.
*
* @param CacheInterface $cache the cache implementation used for storing configuration data
* @param ConfigInterface $defaultConfig the configuration source to be cached
* @param bool $persistent whether the cache should be persistent or not
* @param null|string $cacheKey the cache key to use for storing the configuration data
*/
public function __construct(
private readonly CacheInterface $cache,
private readonly ConfigInterface $defaultConfig,
private readonly bool $persistent = false,
private ?string $cacheKey = null,
) {
$this->cacheKey ??= $this->defaultConfig::class;
}
/**
* Invokes the configuration and returns the cached configuration data.
*
* If the configuration has not yet been cached, it MUST be stored in the cache upon first invocation.
* This method MUST return a ConfigInterface implementation containing the cached configuration data.
*
* @return ConfigInterface a ConfigInterface implementation containing the cached configuration data
*
* @throws InvalidArgumentException if the cache key is invalid
*/
public function __invoke(): ConfigInterface
{
if (!$this->cache->has($this->cacheKey)) {
$this->cache->set($this->cacheKey, $this->defaultConfig->toArray());
}
return new ArrayConfig($this->cache->get($this->cacheKey));
}
/**
* Sets configuration data.
*
* This method MUST update the cached configuration data in the cache if the persistent flag is set to true.
*
* @param array|ConfigInterface|string $key the configuration key or an array of key-value pairs to set
* @param mixed $value the value to set for the specified key
*
* @throws InvalidArgumentException if the key is invalid
*/
public function set(array|ConfigInterface|string $key, mixed $value = null): void
{
$config = $this->getConfig();
$config->set($key, $value);
if ($this->persistent) {
$this->cache->set($this->cacheKey, $config->toArray());
}
}
/**
* Retrieves a configuration value by key.
*
* This method MUST return the cached value if it exists, or the default value if not found.
*
* @param string $key the configuration key to retrieve
*
* @return mixed the configuration value or the default value
*/
public function remove(mixed $key): void
{
$config = $this->getConfig();
$config->remove($key);
if ($this->persistent) {
$this->cache->set($this->cacheKey, $config->toArray());
}
}
}