Completed
Pull Request — master (#9)
by Jérémy
07:49 queued 03:54
created

Kernel::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Thunder micro CLI framework.
7
 * (c) Jérémy Marodon <[email protected]>
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RxThunder\Core\Kernel;
13
14
use RxThunder\Core\Kernel\Exception\KernelNotBootedException;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
18
use Symfony\Component\Dotenv\Dotenv;
19
20
class Kernel implements KernelInterface
21
{
22
    public const NAME            = 'Thunder';
23
    public const VERSION         = '1.0.0-dev';
24
    public const VERSION_ID      = 10000;
25
    public const MAJOR_VERSION   = 1;
26
    public const MINOR_VERSION   = 0;
27
    public const RELEASE_VERSION = 0;
28
    public const EXTRA_VERSION   = 'dev';
29
30
    protected string $environment;
31
    protected ContainerBuilder $container;
32
    protected bool $booted = false;
33
    protected bool $debug;
34
    protected string $project_dir;
35
36 5
    public function __construct(string $environment, bool $debug = false)
37
    {
38 5
        $this->environment = $environment;
39 5
        $this->debug       = $debug;
40 5
        $this->project_dir = $this->initiateProjectDir();
41 5
    }
42
43 2
    public function boot(): void
44
    {
45 2
        if ($this->booted) {
46 1
            return;
47
        }
48
49 2
        $this->initializeContainer();
50
51 2
        $this->booted = true;
52 2
    }
53
54 2
    public function initializeContainer(): void
55
    {
56 2
        $container = new ContainerBuilder();
57
58 2
        $this->loadEnvironment();
59 2
        $this->loadDefinitions($container);
60
61 2
        $container->compile();
62 2
        $this->container = $container;
63 2
    }
64
65 2
    protected function loadDefinitions(ContainerBuilder $container): void
66
    {
67 2
        $container->setParameter('thunder.environment', $this->getEnvironment());
68 2
        $container->setParameter('thunder.debug', $this->debugActivated());
69 2
        $container->setParameter('thunder.project_dir', $this->getProjectDir());
70 2
        $container->setParameter('thunder.config_dir', $this->getConfigDir());
71
72
        $internal_loader =
73 2
            new PhpFileLoader(
74 2
                $container,
75 2
                new FileLocator(__DIR__ . '/../../config')
76
            );
77
78
        $personal_loader =
79 2
            new PhpFileLoader(
80 2
                $container,
81 2
                new FileLocator($this->getConfigDir())
82
            );
83
84 2
        $internal_loader->load('console.php');
85 2
        $internal_loader->load('container.php');
86 2
        $internal_loader->load('router.php');
87 2
        $internal_loader->load('logger.php');
88
89 2
        $personal_loader->load('parameters.php');
90 2
        $personal_loader->load('services.php');
91 2
    }
92
93 2
    protected function loadEnvironment(): void
94
    {
95 2
        (new Dotenv())->populate(['APP_ENV' => $this->getEnvironment()]);
96 2
        (new Dotenv())->loadEnv($this->getProjectDir() . '/.env');
97 2
    }
98
99 5
    protected function initiateProjectDir(): string
100
    {
101 5
        if (isset($this->project_dir)) {
102 1
            throw new \LogicException('Project directory is already defined');
103
        }
104
105 5
        $reflected = new \ReflectionObject($this);
106 5
        if (!$dir = $reflected->getFileName()) {
107
            throw new \LogicException(sprintf('Cannot get filename of class "%s".', $reflected->name));
108
        }
109
110 5
        if (!file_exists($dir)) {
111
            throw new \LogicException(sprintf('File of class donʼt exists "%s".', $reflected->name));
112
        }
113
114 5
        $dir = $root_dir = \dirname($dir);
115 5
        while (!file_exists($dir . '/composer.lock')) {
116 5
            if ($dir === \dirname($dir)) {
117
                return $root_dir;
118
            }
119
120 5
            $dir = \dirname($dir);
121
        }
122
123 5
        return $dir;
124
    }
125
126 1
    public function getProjectDir(): string
127
    {
128 1
        return $this->project_dir;
129
    }
130
131 1
    public function getConfigDir(): string
132
    {
133 1
        return $this->project_dir . '/config';
134
    }
135
136 2
    public function getEnvironment(): string
137
    {
138 2
        return $this->environment;
139
    }
140
141
    /**
142
     * @throws KernelNotBootedException
143
     */
144 2
    public function getContainer(): ContainerBuilder
145
    {
146 2
        if (!$this->booted) {
147 1
            throw new KernelNotBootedException();
148
        }
149
150 1
        return $this->container;
151
    }
152
153 2
    public function debugActivated(): bool
154
    {
155 2
        return $this->debug;
156
    }
157
158 1
    public function booted(): bool
159
    {
160 1
        return $this->booted;
161
    }
162
}
163