Passed
Pull Request — master (#407)
by Kirill
08:54
created

DistributionBootloader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 20
c 1
b 0
f 1
dl 0
loc 54
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerManager() 0 18 2
A boot() 0 11 1
A registerResolver() 0 8 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\Config\ConfiguratorInterface;
16
use Spiral\Core\Container;
17
use Spiral\Distribution\Manager;
18
use Spiral\Distribution\DistributionInterface;
19
use Spiral\Distribution\MutableDistributionInterface;
20
use Spiral\Distribution\Resolver\UriResolver;
21
use Spiral\Distribution\UriResolverInterface;
22
23
class DistributionBootloader extends Bootloader
24
{
25
    /**
26
     * @param ConfiguratorInterface $config
27
     * @param Container $app
28
     */
29
    public function boot(ConfiguratorInterface $config, Container $app): void
30
    {
31
        $config->setDefaults(DistributionConfig::CONFIG, [
32
            'default' => Manager::DEFAULT_RESOLVER,
33
            'resolvers' => [],
34
        ]);
35
36
        $app->bindInjector(DistributionConfig::class, ConfiguratorInterface::class);
37
38
        $this->registerManager($app);
39
        $this->registerResolver($app);
40
    }
41
42
    /**
43
     * @param Container $app
44
     */
45
    private function registerResolver(Container $app): void
46
    {
47
        $app->bindSingleton(UriResolverInterface::class, static function (DistributionInterface $manager) {
48
            return $manager->resolver();
49
        });
50
51
        $app->bindSingleton(UriResolver::class, static function (Container $app) {
52
            return $app->get(UriResolverInterface::class);
53
        });
54
    }
55
56
    /**
57
     * @param Container $app
58
     */
59
    private function registerManager(Container $app): void
60
    {
61
        $app->bindSingleton(DistributionInterface::class, static function (DistributionConfig $config) {
62
            $manager = new Manager($config->getDefaultDriver());
63
64
            foreach ($config->getResolvers() as $name => $resolver) {
65
                $manager->add($name, $resolver);
66
            }
67
68
            return $manager;
69
        });
70
71
        $app->bindSingleton(MutableDistributionInterface::class, static function (Container $app) {
72
            return $app->get(DistributionInterface::class);
73
        });
74
75
        $app->bindSingleton(Manager::class, static function (Container $app) {
76
            return $app->get(DistributionInterface::class);
77
        });
78
    }
79
}
80