|
| 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\Container; |
| 17 | + |
| 18 | +use FastForward\Config\ConfigInterface; |
| 19 | +use FastForward\Config\Exception\ContainerNotFoundExceptionInterface; |
| 20 | +use Psr\Container\ContainerInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class ConfigContainer. |
| 24 | + * |
| 25 | + * Provides a PSR-11 compatible container interface for accessing configuration values. |
| 26 | + * This container MAY be used in dependency injection systems where configuration keys |
| 27 | + * should be resolvable via standard container access. |
| 28 | + */ |
| 29 | +final class ConfigContainer implements ContainerInterface |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @const string The standard identifier for retrieving the configuration object. |
| 33 | + */ |
| 34 | + public const ALIAS = 'config'; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructs a new ConfigContainer instance. |
| 38 | + * |
| 39 | + * This constructor SHALL wrap an existing ConfigInterface instance and expose it |
| 40 | + * through PSR-11 `get()` and `has()` methods. |
| 41 | + * |
| 42 | + * @param ConfigInterface $config the configuration instance to expose as a container |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + private ConfigInterface $config, |
| 46 | + ) {} |
| 47 | + |
| 48 | + /** |
| 49 | + * Determines if the container can return an entry for the given identifier. |
| 50 | + * |
| 51 | + * @param string $id identifier of the entry to look for |
| 52 | + * |
| 53 | + * @return bool TRUE if the entry is known or exists in the configuration, FALSE otherwise |
| 54 | + */ |
| 55 | + public function has(string $id): bool |
| 56 | + { |
| 57 | + return $this->isResolvedByContainer($id) |
| 58 | + || $this->config->has($id); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Retrieves an entry of the container by its identifier. |
| 63 | + * |
| 64 | + * If the ID matches the container itself or the config class, it SHALL return itself. |
| 65 | + * Otherwise, it SHALL retrieve the value from the configuration. |
| 66 | + * If the key is not found, it MUST throw a ContainerNotFoundExceptionInterface. |
| 67 | + * |
| 68 | + * @param string $id identifier of the entry to retrieve |
| 69 | + * |
| 70 | + * @return mixed the value associated with the identifier |
| 71 | + * |
| 72 | + * @throws ContainerNotFoundExceptionInterface if the identifier is not found |
| 73 | + */ |
| 74 | + public function get(string $id) |
| 75 | + { |
| 76 | + if ($this->isResolvedByContainer($id)) { |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + if ($this->config->has($id)) { |
| 81 | + return $this->config->get($id); |
| 82 | + } |
| 83 | + |
| 84 | + throw ContainerNotFoundExceptionInterface::forKey($id); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determines whether the given identifier is resolved internally by the container itself. |
| 89 | + * |
| 90 | + * This method SHALL be used to check if the requested identifier corresponds to: |
| 91 | + * - the container alias (e.g., 'config'), |
| 92 | + * - the Config interface (`FastForward\Config\ConfigInterface`), |
| 93 | + * - or the concrete configuration key. |
| 94 | + * |
| 95 | + * If any of these conditions match, the container MUST resolve the request by returning itself. |
| 96 | + * |
| 97 | + * @param string $id the identifier being checked for internal resolution |
| 98 | + * |
| 99 | + * @return bool TRUE if the container SHALL resolve the identifier itself; FALSE otherwise |
| 100 | + */ |
| 101 | + private function isResolvedByContainer(string $id): bool |
| 102 | + { |
| 103 | + return \in_array($id, [self::ALIAS, ConfigInterface::class, $this->config::class], true); |
| 104 | + } |
| 105 | +} |
0 commit comments