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

ConfigurationBootloader::configManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Boot\Bootloader;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Boot\DirectoriesInterface;
9
use Spiral\Boot\EnvironmentInterface;
10
use Spiral\Config\ConfigManager;
11
use Spiral\Config\ConfiguratorInterface;
12
use Spiral\Config\Loader\ConfigsMergerInterface;
13
use Spiral\Config\Loader\DirectoriesRepository;
14
use Spiral\Config\Loader\DirectoriesRepositoryInterface;
15
use Spiral\Config\Loader\FileLoaderInterface;
16
use Spiral\Config\Loader\FileLoaderRegistry;
17
use Spiral\Config\Loader\JsonLoader;
18
use Spiral\Config\Loader\MergeFileStrategyLoader;
19
use Spiral\Config\Loader\PhpLoader;
20
use Spiral\Config\Loader\RecursiveConfigMerger;
21
use Spiral\Config\Loader\SingleFileStrategyLoader;
22
use Spiral\Config\LoaderInterface;
23
use Spiral\Core\ConfigsInterface;
24
25
/**
26
 * Boot core service responsible for configuration loading and management.
27
 */
28
final class ConfigurationBootloader extends Bootloader
29
{
30 551
    public function __construct(
31
        private readonly ContainerInterface $container,
32 551
    ) {}
33
34 551
    public function defineSingletons(): array
35
    {
36 551
        return [
37 551
            ConfigsInterface::class => ConfiguratorInterface::class,
38 551
            ConfiguratorInterface::class => ConfigManager::class,
39 551
            FileLoaderRegistry::class => $this->createFileLoader(...),
40 551
            ConfigManager::class => $this->createConfigManager(...),
41 551
            ConfigsMergerInterface::class => RecursiveConfigMerger::class,
42 551
            LoaderInterface::class => $this->createConfigLoader(...),
43 551
            DirectoriesRepositoryInterface::class => $this->createDirectories(...),
44 551
        ];
45
    }
46
47 525
    private function createConfigLoader(EnvironmentInterface $env): LoaderInterface
0 ignored issues
show
Unused Code introduced by
The method createConfigLoader() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
48
    {
49 525
        return match ($env->get('CONFIG_STRATEGY', 'single')) {
50 525
            'merge' => $this->container->get(MergeFileStrategyLoader::class),
51 525
            default => $this->container->get(SingleFileStrategyLoader::class),
52 525
        };
53
    }
54
55 550
    private function createDirectories(DirectoriesInterface $directories): DirectoriesRepositoryInterface
0 ignored issues
show
Unused Code introduced by
The method createDirectories() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
56
    {
57 550
        return new DirectoriesRepository([
58 550
            $directories->get('config'),
59 550
        ]);
60
    }
61
62 550
    private function createFileLoader(PhpLoader $phpLoader, JsonLoader $jsonLoader): FileLoaderRegistry
0 ignored issues
show
Unused Code introduced by
The method createFileLoader() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
63
    {
64 550
        return new FileLoaderRegistry([
65 550
            'php' => $phpLoader,
66 550
            'json' => $jsonLoader,
67 550
        ]);
68
    }
69
70 29
    public function setDirectories(array $directories): void
71
    {
72 29
        $this->container->get(DirectoriesRepositoryInterface::class)->setDirectories($directories);
73
    }
74
75 29
    public function addLoader(string $ext, FileLoaderInterface $loader): void
76
    {
77 29
        $this->container->get(FileLoaderRegistry::class)->register($ext, $loader);
78
    }
79
80 525
    private function createConfigManager(LoaderInterface $loader): ConfigManager
0 ignored issues
show
Unused Code introduced by
The method createConfigManager() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
81
    {
82 525
        return new ConfigManager(
83 525
            loader: $loader,
84 525
            strict: true,
85 525
        );
86
    }
87
}
88