-
Notifications
You must be signed in to change notification settings - Fork 2
/
Kernel.php
111 lines (91 loc) · 3.31 KB
/
Kernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace ManaPHP;
use JetBrains\PhpStorm\NoReturn;
use ManaPHP\Di\ConfigInterface;
use ManaPHP\Di\Container;
use ManaPHP\Eventing\ListenersInterface;
use ManaPHP\Eventing\TracerInterface;
use function define;
use function defined;
use function extension_loaded;
class Kernel
{
protected string $root;
protected Container $container;
protected array $bootstrappers
= [
ListenersInterface::class,
TracerInterface::class,
];
public function __construct(string $root)
{
$this->root = $root;
if (!defined('MANAPHP_COROUTINE_ENABLED')) {
define('MANAPHP_COROUTINE_ENABLED', $this->detectCoroutineCanEnabled());
}
$this->container = new Container([
'Psr\SimpleCache\CacheInterface' => 'ManaPHP\Caching\SimpleCache',
'Psr\EventDispatcher\EventDispatcherInterface' => 'ManaPHP\Eventing\EventDispatcherInterface',
'Psr\EventDispatcher\ListenerProviderInterface' => 'ManaPHP\Eventing\ListenerProviderInterface',
'ManaPHP\AliasInterface' => [
'aliases' => [
'@manaphp' => __DIR__,
'@public' => "$root/public",
'@app' => "$root/app",
'@views' => "$root/app/Views",
'@root' => $root,
'@runtime' => "$root/runtime",
'@config' => "$root/config",
]],
]);
$GLOBALS['Psr\Container\ContainerInterface'] = $this->container;
}
public function detectCoroutineCanEnabled(): bool
{
return PHP_SAPI === 'cli' && extension_loaded('swoole');
}
protected function loadConfig(): ConfigInterface
{
$configs = [];
foreach (glob("$this->root/config/*.php") as $item) {
$configs += require $item;
}
foreach ($configs as $id => $definition) {
$this->container->set($id, $definition);
}
$config = $this->container->get(ConfigInterface::class);
foreach ($configs as $id => $definition) {
$config->set($id, $definition);
}
return $config;
}
protected function bootstrap(ConfigInterface $config): void
{
$bootstrappers = $config->get(static::class)['bootstrappers'] ?? $this->bootstrappers;
foreach ($bootstrappers as $name) {
if ($name !== '' && $name !== null) {
/** @var BootstrapperInterface $bootstrapper */
$bootstrapper = $this->container->get($name);
$bootstrapper->bootstrap();
}
}
}
#[NoReturn]
public function start(string $server): void
{
$this->container->get(EnvInterface::class)->load();
$config = $this->loadConfig();
if (($timezone = $config->get('timezone')) !== null) {
date_default_timezone_set($timezone);
}
foreach ($config->get('aliases', []) as $aliases) {
foreach ($aliases as $k => $v) {
$this->container->get(AliasInterface::class)->set($k, $v);
}
}
$this->bootstrap($config);
/** @var string|ServerInterface $server */
$this->container->get($server)->start();
}
}