Passed
Pull Request — master (#389)
by Valentin
04:47
created

ConfigurationBootloader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addLoader() 0 5 3
A __construct() 0 10 1
A createConfigManager() 0 5 1
A configManager() 0 3 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Boot\Bootloader;
13
14
use Spiral\Boot\DirectoriesInterface;
15
use Spiral\Config\ConfigManager;
16
use Spiral\Config\ConfiguratorInterface;
17
use Spiral\Config\Loader\DirectoryLoader;
18
use Spiral\Config\Loader\FileLoaderInterface;
19
use Spiral\Config\Loader\JsonLoader;
20
use Spiral\Config\Loader\PhpLoader;
21
use Spiral\Core\ConfigsInterface;
22
use Spiral\Core\Container;
23
24
/**
25
 * Bootloads core services.
26
 */
27
final class ConfigurationBootloader extends Bootloader
28
{
29
    protected const SINGLETONS = [
30
        // configuration
31
        ConfigsInterface::class      => ConfiguratorInterface::class,
32
        ConfiguratorInterface::class => ConfigManager::class,
33
        ConfigManager::class         => [self::class, 'configManager'],
34
    ];
35
36
    /** @var ConfiguratorInterface */
37
    private $configurator;
38
39
    /** @var FileLoaderInterface[] */
40
    private $loaders;
41
42
    /** @var DirectoriesInterface */
43
    private $directories;
44
45
    /** @var Container */
46
    private $container;
47
48
    public function __construct(DirectoriesInterface $directories, Container $container)
49
    {
50
        $this->loaders = [
51
            'php' => $container->get(PhpLoader::class),
52
            'json' => $container->get(JsonLoader::class),
53
        ];
54
55
        $this->directories = $directories;
56
        $this->container = $container;
57
        $this->configurator = $this->createConfigManager();
58
    }
59
60
    public function addLoader(string $ext, FileLoaderInterface $loader): void
61
    {
62
        if (!isset($this->loaders[$ext]) || get_class($this->loaders[$ext]) !== get_class($loader)) {
63
            $this->loaders[$ext] = $loader;
64
            $this->container->bindSingleton(ConfigManager::class, $this->createConfigManager());
0 ignored issues
show
Bug introduced by
$this->createConfigManager() of type Spiral\Config\ConfigManager is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bindSingleton(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $this->container->bindSingleton(ConfigManager::class, /** @scrutinizer ignore-type */ $this->createConfigManager());
Loading history...
65
        }
66
    }
67
68
    private function createConfigManager(): ConfiguratorInterface
69
    {
70
        return new ConfigManager(
71
            new DirectoryLoader($this->directories->get('config'), $this->loaders),
72
            true
73
        );
74
    }
75
76
    /**
77
     * @return ConfiguratorInterface
78
     */
79
    private function configManager(): ConfiguratorInterface
80
    {
81
        return $this->configurator;
82
    }
83
}
84