1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cervo package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2010-2019 Nevraxe inc. & Marc André Audet <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* @package Cervo |
9
|
|
|
* @author Marc André Audet <[email protected]> |
10
|
|
|
* @copyright 2010 - 2019 Nevraxe inc. & Marc André Audet |
11
|
|
|
* @license See LICENSE.md MIT |
12
|
|
|
* @link https://github.com/Nevraxe/Cervo |
13
|
|
|
* @since 5.0.0 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Cervo; |
19
|
|
|
|
20
|
|
|
use Cervo\Config\BaseConfig; |
21
|
|
|
use Cervo\Exceptions\ControllerReflection\AlreadyInitialisedException; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Core class for Cervo. |
26
|
|
|
* |
27
|
|
|
* @author Marc André Audet <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class Core |
30
|
|
|
{ |
31
|
|
|
/** @var bool */ |
32
|
|
|
private $isInit = false; |
33
|
|
|
|
34
|
|
|
/** @var Context|null */ |
35
|
|
|
private $context = null; |
36
|
|
|
|
37
|
|
|
/** @var Core|null */ |
38
|
|
|
private static $core = null; |
39
|
|
|
|
40
|
|
|
public function __construct(?BaseConfig $config = null) |
41
|
|
|
{ |
42
|
|
|
$this->context = new Context($config); |
43
|
|
|
self::$core = $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function get(): ?Core |
47
|
|
|
{ |
48
|
|
|
return self::$core; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function start() |
52
|
|
|
{ |
53
|
|
|
if ($this->isInit === true) { |
54
|
|
|
throw new AlreadyInitialisedException; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->isInit = true; |
58
|
|
|
|
59
|
|
|
/** @var Events $events */ |
60
|
|
|
$events = $this->getContext()->getSingletons()->get(Events::class); |
61
|
|
|
|
62
|
|
|
/** @var Router $router */ |
63
|
|
|
$router = $this->getContext()->getSingletons()->get(Router::class); |
64
|
|
|
|
65
|
|
|
foreach ($this->getContext()->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) { |
66
|
|
|
$events->loadPath($path); |
67
|
|
|
$router->loadPath($path); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$events->fire('Cervo/System/Before'); |
71
|
|
|
|
72
|
|
|
$route = $router->dispatch(); |
73
|
|
|
|
74
|
|
|
$events->fire('Cervo/Route/Before'); |
75
|
|
|
(new ControllerReflection($this->getContext(), $route))(); |
|
|
|
|
76
|
|
|
$events->fire('Cervo/Route/After'); |
77
|
|
|
|
78
|
|
|
$events->fire('Cervo/System/After'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getContext(): ?Context |
82
|
|
|
{ |
83
|
|
|
return $this->context; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|