Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| LazyLoadConfigTrait | |
100.00% |
10 / 10 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| __invoke | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| remove | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConfig | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/config. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @link https://github.com/php-fast-forward/config |
| 12 | * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 13 | * @license https://opensource.org/licenses/MIT MIT License |
| 14 | */ |
| 15 | |
| 16 | namespace FastForward\Config; |
| 17 | |
| 18 | /** |
| 19 | * Trait LazyLoadConfigTrait. |
| 20 | * |
| 21 | * Implements lazy-loading behavior for configuration access. |
| 22 | * This trait MUST be used in classes implementing ConfigInterface to defer configuration instantiation |
| 23 | * until first usage. It SHALL invoke the implementing class as a callable to obtain the actual config instance. |
| 24 | */ |
| 25 | trait LazyLoadConfigTrait |
| 26 | { |
| 27 | use ArrayAccessConfigTrait; |
| 28 | |
| 29 | /** |
| 30 | * @var null|ConfigInterface holds the loaded configuration instance |
| 31 | */ |
| 32 | private ?ConfigInterface $config = null; |
| 33 | |
| 34 | /** |
| 35 | * Implementing class MUST define the __invoke() method to return a ConfigInterface instance. |
| 36 | * |
| 37 | * @return ConfigInterface the actual configuration instance |
| 38 | */ |
| 39 | abstract public function __invoke(): ConfigInterface; |
| 40 | |
| 41 | /** |
| 42 | * Retrieves a configuration value by key. |
| 43 | * |
| 44 | * @param string $key the configuration key to retrieve |
| 45 | * @param null|mixed $default the default value if the key is not found |
| 46 | * |
| 47 | * @return mixed the value of the configuration key or the default |
| 48 | */ |
| 49 | public function get(string $key, mixed $default = null): mixed |
| 50 | { |
| 51 | return $this->getConfig()->get($key, $default); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Checks for existence of a configuration key. |
| 56 | * |
| 57 | * @param string $key the configuration key to check |
| 58 | * |
| 59 | * @return bool TRUE if the key exists, FALSE otherwise |
| 60 | */ |
| 61 | public function has(string $key): bool |
| 62 | { |
| 63 | return $this->getConfig()->has($key); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Sets configuration data. |
| 68 | * |
| 69 | * @param array|ConfigInterface|string $key the key or set of keys/values to set |
| 70 | * @param null|mixed $value the value to set if a single key is provided |
| 71 | */ |
| 72 | public function set(array|ConfigInterface|string $key, mixed $value = null): void |
| 73 | { |
| 74 | $this->getConfig()->set($key, $value); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Removes a configuration key. |
| 79 | * |
| 80 | * @param string $key the configuration key to remove |
| 81 | */ |
| 82 | public function remove(string $key): void |
| 83 | { |
| 84 | $this->getConfig()->remove($key); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Exports the entire configuration to an array. |
| 89 | * |
| 90 | * @return array the configuration as an associative array |
| 91 | */ |
| 92 | public function toArray(): array |
| 93 | { |
| 94 | return $this->getConfig()->toArray(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Retrieves an iterator for traversing the configuration data. |
| 99 | * |
| 100 | * @return \Traversable an iterator over the configuration |
| 101 | */ |
| 102 | public function getIterator(): \Traversable |
| 103 | { |
| 104 | return $this->getConfig()->getIterator(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Retrieves or initializes the configuration instance. |
| 109 | * |
| 110 | * @return ConfigInterface the lazily-loaded configuration object |
| 111 | */ |
| 112 | private function getConfig(): ConfigInterface |
| 113 | { |
| 114 | if ($this->config) { |
| 115 | return $this->config; |
| 116 | } |
| 117 | |
| 118 | $this->config = \call_user_func($this); |
| 119 | |
| 120 | return $this->config; |
| 121 | } |
| 122 | } |