Test Failed
Push — develop ( acdfb2...2de2bf )
by nguereza
02:53
created

AbstractApplication   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 194
rs 10
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguredServiceProviders() 0 9 2
A boot() 0 11 3
A __construct() 0 12 1
A registerServiceProvider() 0 22 5
A version() 0 3 1
A loadCoreServiceProviders() 0 6 1
A markProviderAsRegistered() 0 3 1
A bootServiceProvider() 0 3 1
A createServiceProvider() 0 3 1
A getServiceProvider() 0 7 2
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file AbstractApplication.php
34
 *
35
 *  The Platine Base Application class
36
 *
37
 *  @package    Platine\Framework\App
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\App;
49
50
use Platine\Config\Config;
51
use Platine\Container\Container;
52
use Platine\Framework\Service\Provider\BaseServiceProvider;
53
use Platine\Framework\Service\Provider\EventServiceProvider;
54
use Platine\Framework\Service\Provider\LoggerServiceProvider;
55
use Platine\Framework\Service\Provider\RoutingServiceProvider;
56
use Platine\Framework\Service\ServiceProvider;
57
use Platine\Logger\LoggerInterface;
58
59
/**
60
 * class AbstractApplication
61
 * @package Platine\Framework\App
62
 */
63
abstract class AbstractApplication extends Container
64
{
65
66
    /**
67
     * The application version
68
     */
69
    public const VERSION = '1.0.0-dev';
70
71
    /**
72
     * The base path for this application
73
     * @var string
74
     */
75
    protected string $basePath;
76
77
    /**
78
     * The configuration instance
79
     * @var Config
80
     */
81
    protected Config $config;
82
83
    /**
84
     * The logger instance to use
85
     * @var LoggerInterface
86
     */
87
    protected LoggerInterface $logger;
88
89
    /**
90
     * The list of service providers
91
     * @var array<string, ServiceProvider>
92
     */
93
    protected array $providers = [];
94
95
    /**
96
     * Whether the system already booted
97
     * @var bool
98
     */
99
    protected bool $booted = false;
100
101
    /**
102
     * The application environment
103
     * @var string
104
     */
105
    protected string $env = 'dev';
106
107
    /**
108
     * Create new instance
109
     * @param string|null $basePath
110
     */
111
    public function __construct(?string $basePath = null)
112
    {
113
        parent::__construct();
114
        $this->loadCoreServiceProviders();
115
        $this->loadConfiguredServiceProviders();
116
117
        $this->boot();
118
119
        $this->config = $this->get(Config::class);
120
        $this->logger = $this->get(LoggerInterface::class);
121
122
        $this->basePath = $basePath ?? $this->config->get('app.base_path', '/');
123
    }
124
125
    /**
126
     * Return the application version
127
     * @return string
128
     */
129
    public function version(): string
130
    {
131
        return self::VERSION;
132
    }
133
134
    /**
135
     * Boot the application
136
     * @return void
137
     */
138
    public function boot(): void
139
    {
140
        if ($this->booted) {
141
            return;
142
        }
143
144
        foreach ($this->providers as $provider) {
145
            $this->bootServiceProvider($provider);
146
        }
147
148
        $this->booted = true;
149
    }
150
151
    /**
152
     * Register the service provider
153
     * @param string|ServiceProvider $provider
154
     * @param bool $force whether to force registration of provider
155
     * if already loaded
156
     * @return ServiceProvider
157
     */
158
    public function registerServiceProvider(
159
        $provider,
160
        bool $force = false
161
    ): ServiceProvider {
162
        $registered = $this->getServiceProvider($provider);
163
        if ($registered && !$force) {
164
            return $registered;
165
        }
166
167
        if (is_string($provider)) {
168
            $provider = $this->createServiceProvider($provider);
169
        }
170
171
        $provider->register();
172
173
        $this->markProviderAsRegistered($provider);
174
175
        if ($this->booted) {
176
            $this->bootServiceProvider($provider);
177
        }
178
179
        return $provider;
180
    }
181
182
    /**
183
     * Return the registered service provider if exist
184
     * @param string|ServiceProvider $provider
185
     * @return ServiceProvider|null
186
     */
187
    public function getServiceProvider($provider): ?ServiceProvider
188
    {
189
        $name = is_string($provider)
190
                ? $provider
191
                : get_class($provider);
192
193
        return $this->providers[$name] ?? null;
194
    }
195
196
    /**
197
     * Execute the application
198
     * @return void
199
     */
200
    abstract public function execute(): void;
201
202
    /**
203
     * Create service provider
204
     * @param string $provider
205
     * @return ServiceProvider
206
     */
207
    protected function createServiceProvider(string $provider): ServiceProvider
208
    {
209
        return new $provider($this);
210
    }
211
212
    /**
213
     * Boot the given service provider
214
     * @param ServiceProvider $provider
215
     * @return void
216
     */
217
    protected function bootServiceProvider(ServiceProvider $provider): void
218
    {
219
        $provider->boot();
220
    }
221
222
    /**
223
     * Set the given service provider as registered
224
     * @param ServiceProvider $provider
225
     * @return void
226
     */
227
    protected function markProviderAsRegistered(ServiceProvider $provider): void
228
    {
229
        $this->providers[get_class($provider)] = $provider;
230
    }
231
232
    /**
233
     * Load framework core service providers
234
     * @return void
235
     */
236
    protected function loadCoreServiceProviders(): void
237
    {
238
        $this->registerServiceProvider(new BaseServiceProvider($this));
239
        $this->registerServiceProvider(new LoggerServiceProvider($this));
240
        $this->registerServiceProvider(new EventServiceProvider($this));
241
        $this->registerServiceProvider(new RoutingServiceProvider($this));
242
    }
243
244
    /**
245
     * Load configured service providers
246
     * @return void
247
     */
248
    protected function loadConfiguredServiceProviders(): void
249
    {
250
        /** @var Config $config */
251
        $config = $this->get(Config::class);
252
253
        /** @var string[] $providers */
254
        $providers = $config->get('providers', []);
255
        foreach ($providers as $provider) {
256
            $this->registerServiceProvider($provider);
257
        }
258
    }
259
}
260