Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DirectoryConfig
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
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 * @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
16namespace FastForward\Config;
17
18use FastForward\Config\Exception\InvalidArgumentException;
19use Laminas\ConfigAggregator\PhpFileProvider;
20
21/**
22 * Class DirectoryConfig.
23 *
24 * Loads and aggregates configuration from a specified directory containing PHP files.
25 * This class MUST validate the target directory and use the Laminas PhpFileProvider to collect configurations.
26 * It MAY cache the aggregated result if a cached config file path is provided.
27 *
28 * @extends LamiasConfigAggregatorConfig
29 */
30class DirectoryConfig extends LamiasConfigAggregatorConfig implements ConfigInterface
31{
32    use LazyLoadConfigTrait;
33
34    /**
35     * @const string File matching pattern for PHP configuration files.
36     */
37    protected const PATTERN = '{,*}.php';
38
39    /**
40     * Constructs a DirectoryConfig instance.
41     *
42     * This constructor SHALL validate the specified directory, and initialize
43     * the Laminas config aggregator with a PHP file provider using the defined pattern.
44     * If a cache file is provided, it SHALL be used to store the aggregated configuration.
45     *
46     * @param string      $directory        the directory path from which to load configuration files
47     * @param null|string $cachedConfigFile optional path to a cache file for the aggregated configuration
48     *
49     * @throws InvalidArgumentException if the directory is not valid or not readable
50     */
51    public function __construct(
52        private readonly string $directory,
53        private readonly ?string $cachedConfigFile = null,
54    ) {
55        if (!is_dir($this->directory) || !is_readable($this->directory)) {
56            throw InvalidArgumentException::forUnreadableDirectory($this->directory);
57        }
58
59        $pattern = mb_rtrim($this->directory, \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR . static::PATTERN;
60
61        parent::__construct(
62            providers: [new PhpFileProvider($pattern)],
63            cachedConfigFile: $this->cachedConfigFile,
64        );
65    }
66}