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