Completed
Push — master ( 7381e1...0d723c )
by Korvin
02:16
created

ServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
ccs 0
cts 0
cp 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Buttress\Concrete\Provider\LeagueContainer;
4
5
use Buttress\Concrete\Client\Adapter\AdapterFactory;
6
use Buttress\Concrete\Client\Concrete5;
7
use Buttress\Concrete\Client\Connection\Connection;
8
use Buttress\Concrete\Client\Connection\LegacyConnection;
9
use Buttress\Concrete\Client\Connection\ModernConnection;
10
use Buttress\Concrete\CommandBus\Command\HandlerLocator;
11
use Buttress\Concrete\Client\Adapter\Adapter;
12
use Buttress\Concrete\Client\Adapter\LegacyAdapter;
13
use Buttress\Concrete\Client\Adapter\ModernAdapter;
14
use Buttress\Concrete\Console\Command\Collection\Collection;
15
use Buttress\Concrete\Console\Console;
16
use Buttress\Concrete\Locator\Locator;
17
use Buttress\Concrete\Locator\Site;
18
use Buttress\Concrete\Log\Logger;
19
use Concrete\Core\Application\Application;
20
use League\CLImate\CLImate;
21
use League\Container\ServiceProvider\AbstractServiceProvider;
22
use League\Container\ServiceProvider\BootableServiceProviderInterface;
23
use League\Tactician\CommandBus;
24
use League\Tactician\Handler\CommandHandlerMiddleware;
25
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
26
use League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector;
27
use Psr\Container\ContainerInterface;
28
use Psr\Log\LoggerInterface;
29
30
class ServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
31
{
32
33
    protected $serviceProviders = [
34
        \Buttress\Concrete\Client\ServiceProvider::class,
35
        \Buttress\Concrete\Service\Package\ServiceProvider::class
36
    ];
37
38
    protected $provides = [
39
        LoggerInterface::class,
40
        Console::class,
41
        CLImate::class,
42
        CommandBus::class,
43
        ContainerInterface::class,
44
        HandlerLocator::class,
45
        Site::class,
46
        Application::class
47
    ];
48
49
    public function register()
50
    {
51
        $container = $this->getContainer();
52
53
        // Share the CLImate object
54
        $container->share(CLImate::class, function () use ($container) {
55
            return new CLImate();
56
        });
57
58
        // Share the command bus handler locator
59
        $container->share(HandlerLocator::class)->withArgument($container);
60
61
        // Add the command bus
62
        $container->add(
63
            CommandBus::class,
64
            function (ClassNameExtractor $extractor, HandlerLocator $locator, HandleClassNameInflector $inflector) {
65
                $handlerMiddleware = new CommandHandlerMiddleware($extractor, $locator, $inflector);
66
67
                return new CommandBus([$handlerMiddleware]);
68
            }
69
        )->withArguments([ClassNameExtractor::class, HandlerLocator::class, HandleClassNameInflector::class]);
70
71
        // Share the console object
72
        $container->share(Console::class)
73
            ->withArgument($container)
74
            ->withArgument(Collection::class)
75
            ->withArgument(Site::class);
76
77
        $container->share(Site::class, function () use ($container) {
78
            $site = $container->get(Locator::class)->getLocation();
79
            if (!$site) {
80
                $site = new Site();
81
            }
82
83
            if ($site->getVersion())
84
85
            return $site;
86
        });
87
88
        // Share the container
89
        $container->share(ContainerInterface::class, $container);
90
91
        // Share the logger class
92
        $container->share(LoggerInterface::class, Logger::class)->withArgument(CLImate::class);
93
    }
94
95
    /**
96
     * Method will be invoked on registration of a service provider implementing
97
     * this interface. Provides ability for eager loading of Service Providers.
98
     *
99
     * @return void
100
     */
101
    public function boot()
102
    {
103
        $container = $this->getContainer();
104
        foreach ($this->serviceProviders as $provider) {
105
            $container->addServiceProvider($provider);
106
        }
107
    }
108
}
109