-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyLoadConfigTrait.php
More file actions
122 lines (107 loc) · 3.32 KB
/
Copy pathLazyLoadConfigTrait.php
File metadata and controls
122 lines (107 loc) · 3.32 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
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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;
/**
* Trait LazyLoadConfigTrait.
*
* Implements lazy-loading behavior for configuration access.
* This trait MUST be used in classes implementing ConfigInterface to defer configuration instantiation
* until first usage. It SHALL invoke the implementing class as a callable to obtain the actual config instance.
*/
trait LazyLoadConfigTrait
{
use ArrayAccessConfigTrait;
/**
* @var null|ConfigInterface holds the loaded configuration instance
*/
private ?ConfigInterface $config = null;
/**
* Implementing class MUST define the __invoke() method to return a ConfigInterface instance.
*
* @return ConfigInterface the actual configuration instance
*/
abstract public function __invoke(): ConfigInterface;
/**
* Retrieves a configuration value by key.
*
* @param string $key the configuration key to retrieve
* @param null|mixed $default the default value if the key is not found
*
* @return mixed the value of the configuration key or the default
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->getConfig()->get($key, $default);
}
/**
* Checks for existence of a configuration key.
*
* @param string $key the configuration key to check
*
* @return bool TRUE if the key exists, FALSE otherwise
*/
public function has(string $key): bool
{
return $this->getConfig()->has($key);
}
/**
* Sets configuration data.
*
* @param array|ConfigInterface|string $key the key or set of keys/values to set
* @param null|mixed $value the value to set if a single key is provided
*/
public function set(array|ConfigInterface|string $key, mixed $value = null): void
{
$this->getConfig()->set($key, $value);
}
/**
* Removes a configuration key.
*
* @param string $key the configuration key to remove
*/
public function remove(string $key): void
{
$this->getConfig()->remove($key);
}
/**
* Exports the entire configuration to an array.
*
* @return array the configuration as an associative array
*/
public function toArray(): array
{
return $this->getConfig()->toArray();
}
/**
* Retrieves an iterator for traversing the configuration data.
*
* @return \Traversable an iterator over the configuration
*/
public function getIterator(): \Traversable
{
return $this->getConfig()->getIterator();
}
/**
* Retrieves or initializes the configuration instance.
*
* @return ConfigInterface the lazily-loaded configuration object
*/
private function getConfig(): ConfigInterface
{
if ($this->config) {
return $this->config;
}
$this->config = \call_user_func($this);
return $this->config;
}
}