ExtensionLoader::initiateConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Leonidas\Framework;
4
5
use Leonidas\Contracts\Extension\ExtensionBootProcessInterface;
6
use Leonidas\Contracts\Extension\ExtensionLoaderInterface;
7
use Leonidas\Contracts\Extension\WpExtensionInterface;
8
use Leonidas\Framework\Exception\ExtensionInitiationException;
9
use Leonidas\Framework\Exception\PluginInitiationException;
10
use Leonidas\Framework\Exception\ThemeInitiationException;
11
use Panamax\Contracts\BootableProviderContainerInterface;
12
use Panamax\Contracts\ContainerAdapterInterface;
13
use Panamax\Contracts\ProviderContainerInterface;
14
use Panamax\Contracts\ServiceContainerInterface;
15
use Panamax\Contracts\ServiceCreatorInterface;
16
use WebTheory\Config\Config;
17
use WebTheory\Config\Interfaces\ConfigInterface;
18
19
class ExtensionLoader implements ExtensionLoaderInterface
20
{
21
    protected string $type;
22
23
    protected string $path;
24
25
    protected string $url;
26
27
    protected ServiceContainerInterface $container;
28
29
    protected WpExtensionInterface $extension;
30
31
    protected ConfigInterface $config;
32
33
    public function __construct(string $type, string $path, string $url)
34
    {
35
        $this->type = $type;
36
        $this->path = $path;
37
        $this->url = $url;
38
        $this->container = $this->defineContainer();
39
        $this->extension = $this->defineExtension();
40
    }
41
42
    public function getContainer(): ServiceContainerInterface
43
    {
44
        return $this->container;
45
    }
46
47
    public function getExtension(): WpExtensionInterface
48
    {
49
        return $this->extension;
50
    }
51
52
    public function bootstrap(): void
53
    {
54
        $this
55
            ->initiateConfig()
56
            ->bindServicesToContainer()
57
            ->loadBootProcesses()
58
            ->maybeBootProviders()
59
            ->initializeModules();
60
    }
61
62
    public function error(): void
63
    {
64
        $backtrace = debug_backtrace();
65
        $caller = $backtrace[1];
66
        $method = $caller['class'] . $caller['type'] . $caller['function'];
67
68
        switch ($this->extension->getType()) {
69
            case 'plugin':
70
                throw new PluginInitiationException($this->extension, $method);
71
72
            case 'theme':
73
                throw new ThemeInitiationException($this->extension, $method);
74
75
            default:
76
                throw new ExtensionInitiationException($this->extension, $method);
77
        }
78
    }
79
80
    protected function defineContainer(): ServiceContainerInterface
81
    {
82
        return require $this->path . '/boot/container.php';
83
    }
84
85
    protected function defineExtension(): WpExtensionInterface
86
    {
87
        $container = $this->container instanceof ContainerAdapterInterface
88
            ? $this->container->getAdaptedContainer()
0 ignored issues
show
Bug introduced by
The method getAdaptedContainer() does not exist on Panamax\Contracts\ServiceContainerInterface. It seems like you code against a sub-type of Panamax\Contracts\ServiceContainerInterface such as Panamax\Contracts\ContainerAdapterInterface or Panamax\Adapters\League\LeagueAdapter. ( Ignorable by Annotation )

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

88
            ? $this->container->/** @scrutinizer ignore-call */ getAdaptedContainer()
Loading history...
89
            : $this->container;
90
91
        return new WpExtension($this->type, $this->path, $this->url, $container);
92
    }
93
94
    /**
95
     * @return $this
96
     */
97
    protected function initiateConfig(): ExtensionLoader
98
    {
99
        $this->config = new Config($this->path . '/config');
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return $this
106
     */
107
    protected function bindServicesToContainer(): ExtensionLoader
108
    {
109
        $container = $this->container;
110
111
        $container->share('root', $this->path);
112
        $container->share('url', $this->url);
113
        $container->share('config', $this->config);
114
115
        if ($container instanceof ProviderContainerInterface) {
116
            $container->addServiceProviders(
117
                $this->config->get('app.providers', [])
118
            );
119
        }
120
121
        if ($container instanceof ServiceCreatorInterface) {
122
            $container->createServices($this->config->get('app.services', []));
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return $this
130
     */
131
    protected function loadBootProcesses(): ExtensionLoader
132
    {
133
        foreach ($this->config->get('app.bootstrap', []) as $bootProcess) {
134
            /** @var ExtensionBootProcessInterface $bootProcess */
135
            $bootProcess = new $bootProcess();
136
            $bootProcess->boot($this->extension, $this->container);
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return $this
144
     */
145
    protected function maybeBootProviders(): ExtensionLoader
146
    {
147
        if ($this->container instanceof BootableProviderContainerInterface) {
148
            $this->container->bootServiceProviders();
0 ignored issues
show
Bug introduced by
The method bootServiceProviders() does not exist on Panamax\Contracts\ServiceContainerInterface. It seems like you code against a sub-type of Panamax\Contracts\ServiceContainerInterface such as Panamax\Contracts\Bootab...viderContainerInterface. ( Ignorable by Annotation )

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

148
            $this->container->/** @scrutinizer ignore-call */ 
149
                              bootServiceProviders();
Loading history...
149
        }
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return $this
156
     */
157
    protected function initializeModules(): ExtensionLoader
158
    {
159
        (new ModuleInitializer(
160
            $this->extension,
161
            $this->config->get('app.modules', [])
162
        ))->init();
163
164
        return $this;
165
    }
166
}
167