Passed
Pull Request — master (#407)
by Kirill
09:14 queued 02:20
created

DistributionBootloader::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader\Distribution;
13
14
use Spiral\Boot\Bootloader\Bootloader;
15
use Spiral\Bootloader\Storage\StorageConfig;
16
use Spiral\Config\ConfiguratorInterface;
17
use Spiral\Core\Container;
18
use Spiral\Distribution\Manager;
19
use Spiral\Distribution\ManagerInterface;
20
use Spiral\Distribution\MutableManagerInterface;
21
use Spiral\Distribution\Resolver\Resolver;
22
use Spiral\Distribution\ResolverInterface;
23
24
class DistributionBootloader extends Bootloader
25
{
26
    /**
27
     * @param ConfiguratorInterface $config
28
     * @param Container $app
29
     */
30
    public function boot(ConfiguratorInterface $config, Container $app): void
31
    {
32
        $config->setDefaults(StorageConfig::CONFIG, [
33
            'default' => Manager::DEFAULT_RESOLVER,
34
            'servers' => [],
35
            'buckets' => [],
36
        ]);
37
        $this->registerConfig($app);
38
39
        $this->registerManager($app);
40
        $this->registerResolver($app);
41
    }
42
43
    /**
44
     * @param Container $app
45
     */
46
    private function registerConfig(Container $app): void
47
    {
48
        $app->bindInjector(DistributionConfig::class, ConfiguratorInterface::class);
49
    }
50
51
    /**
52
     * @param Container $app
53
     */
54
    private function registerResolver(Container $app): void
55
    {
56
        $app->bindSingleton(ResolverInterface::class, static function (ManagerInterface $manager) {
57
            return $manager->resolver();
58
        });
59
60
        $app->bindSingleton(Resolver::class, static function (Container $app) {
61
            return $app->get(ResolverInterface::class);
62
        });
63
    }
64
65
    /**
66
     * @param Container $app
67
     */
68
    private function registerManager(Container $app): void
69
    {
70
        $app->bindSingleton(ManagerInterface::class, static function (DistributionConfig $config) {
71
            $manager = new Manager($config->getDefaultDriver());
72
73
            foreach ($config->getResolvers() as $name => $resolver) {
74
                $manager->add($name, $resolver);
75
            }
76
77
            return $manager;
78
        });
79
80
        $app->bindSingleton(MutableManagerInterface::class, static function (Container $app) {
81
            return $app->get(ManagerInterface::class);
82
        });
83
84
        $app->bindSingleton(Manager::class, static function (Container $app) {
85
            return $app->get(ManagerInterface::class);
86
        });
87
    }
88
}
89