1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cervo package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* @package Cervo |
9
|
|
|
* @author Marc André Audet <[email protected]> |
10
|
|
|
* @copyright 2010 - 2018 Nevraxe inc. & Marc André Audet |
11
|
|
|
* @license See LICENSE.md BSD-2-Clauses |
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\Router\InvalidProviderException; |
22
|
|
|
use Cervo\Interfaces\ProviderInterface; |
23
|
|
|
use Cervo\Utils\ClassUtils; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Context class for Cervo. |
27
|
|
|
* |
28
|
|
|
* @author Marc André Audet <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class Context |
31
|
|
|
{ |
32
|
|
|
private $config; |
33
|
|
|
private $autoloader; |
34
|
|
|
private $modulesManager; |
35
|
|
|
private $singletons; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Context constructor. |
39
|
|
|
* |
40
|
|
|
* @param BaseConfig|null $config |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
?BaseConfig $config = null |
44
|
|
|
) { |
45
|
|
|
$this->config = $config ?? new BaseConfig(); |
46
|
|
|
|
47
|
|
|
$this->modulesManager = new ModulesManager(); |
48
|
|
|
$this->autoloader = new Autoloader($this); |
49
|
|
|
$this->singletons = new Singletons($this); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Register and run a Provider class |
54
|
|
|
* |
55
|
|
|
* @param string $providerClass The Provider class |
56
|
|
|
* |
57
|
|
|
* @return Context |
58
|
|
|
*/ |
59
|
|
|
public function register(string $providerClass): self |
60
|
|
|
{ |
61
|
|
|
if (!ClassUtils::implements($providerClass, ProviderInterface::class)) { |
62
|
|
|
throw new InvalidProviderException; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
(new $providerClass($this))(); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function route(): self |
71
|
|
|
{ |
72
|
|
|
/** @var Events $events */ |
73
|
|
|
$events = $this->getSingletons()->get(Events::class); |
74
|
|
|
|
75
|
|
|
/** @var Router $router */ |
76
|
|
|
$router = $this->getSingletons()->get(Router::class); |
77
|
|
|
|
78
|
|
|
foreach ($this->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) { |
79
|
|
|
$events->loadPath($path); |
80
|
|
|
$router->loadPath($path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$events->fire('Cervo/System/Before'); |
84
|
|
|
|
85
|
|
|
$route = $router->dispatch(); |
86
|
|
|
|
87
|
|
|
$events->fire('Cervo/Route/Before'); |
88
|
|
|
(new ControllerReflection($this, $route))(); |
89
|
|
|
$events->fire('Cervo/Route/After'); |
90
|
|
|
|
91
|
|
|
$events->fire('Cervo/System/After'); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getConfig(): BaseConfig |
97
|
|
|
{ |
98
|
|
|
return $this->config; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getModulesManager(): ModulesManager |
102
|
|
|
{ |
103
|
|
|
return $this->modulesManager; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getAutoloader(): Autoloader |
107
|
|
|
{ |
108
|
|
|
return $this->autoloader; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getSingletons(): Singletons |
112
|
|
|
{ |
113
|
|
|
return $this->singletons; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|