Passed
Pull Request — master (#1218)
by butschster
11:03
created

MergeFileStrategyLoader::load()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 22
cp 0
rs 8.4444
cc 8
nc 20
nop 1
crap 72
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Config\Loader;
6
7
use Spiral\Config\Exception\LoaderException;
8
use Spiral\Config\LoaderInterface;
9
10
/**
11
 * @internal
12
 *
13
 * Load all files with the same name from all provided directories and merge them using the provided
14
 * merge strategy.
15
 */
16
final class MergeFileStrategyLoader implements LoaderInterface
17
{
18
    public function __construct(
19
        private readonly DirectoriesRepositoryInterface $directories,
20
        private readonly FileLoaderRegistry $fileLoader,
21
        private readonly ConfigsMergerInterface $configsMerger,
22
    ) {}
23
24
    public function has(string $section): bool
25
    {
26
        foreach ($this->fileLoader->getExtensions() as $extension) {
27
            if ($this->findFiles($section, $extension) !== []) {
28
                return true;
29
            }
30
        }
31
32
        return false;
33
    }
34
35
    public function load(string $section): array
36
    {
37
        $files = [];
38
39
        foreach ($this->fileLoader->getExtensions() as $extension) {
40
            $foundFiles = $this->findFiles($section, $extension);
41
42
            if ($foundFiles === []) {
43
                continue;
44
            }
45
46
            if (!isset($files[$extension])) {
47
                $files[$extension] = [];
48
            }
49
50
            $files[$extension] = [...$files[$extension], ...$foundFiles];
51
        }
52
53
        if ($files === []) {
54
            throw new LoaderException(\sprintf('Unable to load config `%s`: no suitable loader found.', $section));
55
        }
56
57
        $configs = [];
58
        foreach ($files as $ext => $_files) {
59
            foreach ($_files as $file) {
60
                try {
61
                    $configs[] = $this->fileLoader->getLoader($ext)->loadFile($section, $file);
62
                } catch (LoaderException $e) {
63
                    throw new LoaderException(
64
                        "Unable to load config `{$section}`: {$e->getMessage()}",
65
                        $e->getCode(),
66
                        $e,
67
                    );
68
                }
69
            }
70
        }
71
72
        return $this->configsMerger->merge(...$configs);
73
    }
74
75
    private function findFiles(string $section, string $extension): array
76
    {
77
        $files = [];
78
        foreach ($this->directories as $directory) {
79
            $filename = \sprintf('%s/%s.%s', $directory, $section, $extension);
80
            if (\file_exists($filename)) {
81
                $files[] = $filename;
82
            }
83
        }
84
85
        return $files;
86
    }
87
}
88