Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ConfigContainer
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 get
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 resolveKey
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 isResolvedByContainer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(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 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/config
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Config\Container;
20
21use FastForward\Config\ConfigInterface;
22use FastForward\Config\Exception\ContainerNotFoundException;
23use Psr\Container\ContainerInterface;
24
25/**
26 * Class ConfigContainer.
27 *
28 * Provides a PSR-11 compatible container interface for accessing configuration values.
29 *
30 * This container implementation SHALL resolve configuration keys by prefix using the alias "config".
31 * For example, a request for "config.db.host" will attempt to fetch the key "db.host" from the underlying ConfigInterface.
32 *
33 * Identifiers such as 'config', ConfigInterface::class, and the concrete config class MUST return the configuration instance itself.
34 * Requests for unknown or invalid identifiers MUST result in a ContainerNotFoundException.
35 */
36  class ConfigContainer implements ContainerInterface
37{
38    /**
39     * @var string the standard identifier for retrieving the configuration object
40     */
41    public const string ALIAS = 'config';
42
43    /**
44     * Constructs a new ConfigContainer instance.
45     *
46     * This constructor SHALL wrap an existing ConfigInterface instance and expose it
47     * through PSR-11 `get()` and `has()` methods with namespace-style key resolution.
48     *
49     * @param ConfigInterface $config the configuration instance to expose as a container
50     */
51    public function __construct(
52        private ConfigInterface $config,
53    ) {}
54
55    /**
56     * Determines whether the container can return an entry for the given identifier.
57     *
58     * This method SHALL return true if:
59     * - The identifier matches known internal bindings (alias, interface, or class).
60     * - The identifier is prefixed with 'config.' and corresponds to an existing key in the configuration.
61     *
62     * @param string $id identifier of the entry to look for
63     *
64     * @return bool true if the entry can be resolved; false otherwise
65     */
66    public function has(string $id): bool
67    {
68        if (self::class === $id || $this->isResolvedByContainer($id)) {
69            return true;
70        }
71
72        $key = $this->resolveKey($id);
73
74        return null !== $key && $this->config->has($key);
75    }
76
77    /**
78     * Retrieves an entry of the container by its identifier.
79     *
80     * This method SHALL resolve identifiers in the following order:
81     * - If the identifier matches 'config', ConfigInterface::class, or the concrete config class,
82     *   it SHALL return the ConfigInterface instance itself.
83     * - If the identifier is prefixed with 'config.', the suffix SHALL be used to query the configuration.
84     *   If the configuration key exists, its value SHALL be returned.
85     * - If the identifier cannot be resolved, a ContainerNotFoundException MUST be thrown.
86     *
87     * @param string $id identifier of the entry to retrieve
88     *
89     * @return mixed the value associated with the identifier
90     *
91     * @throws ContainerNotFoundException if the identifier cannot be resolved
92     */
93    public function get(string $id)
94    {
95        if (self::class === $id) {
96            return $this;
97        }
98
99        if ($this->isResolvedByContainer($id)) {
100            return $this->config;
101        }
102
103        $key = $this->resolveKey($id);
104
105        if (null !== $key && $this->config->has($key)) {
106            return $this->config->get($key);
107        }
108
109        throw ContainerNotFoundException::forKey($id);
110    }
111
112    /**
113     * Returns a nonempty configuration path for the documented service prefix.
114     *
115     * @param string $id
116     */
117    private function resolveKey(string $id): ?string
118    {
119        $prefix = self::ALIAS . '.';
120
121        if (! str_starts_with($id, $prefix)) {
122            return null;
123        }
124
125        $key = substr($id, \strlen($prefix));
126
127        return '' !== $key ? $key : null;
128    }
129
130    /**
131     * Determines whether the given identifier is resolved internally by the container itself.
132     *
133     * This method SHALL match the identifier against:
134     * - the alias "config"
135     * - the ConfigInterface::class string
136     * - the concrete class of the injected configuration instance
137     *
138     * @param string $id the identifier to check
139     *
140     * @return bool true if the identifier is resolved internally; false otherwise
141     */
142    private function isResolvedByContainer(string $id): bool
143    {
144        return \in_array($id, [self::ALIAS, ConfigInterface::class, $this->config::class], true);
145    }
146}