1
|
|
|
<?php namespace Gvera; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use Gvera\Cache\Cache; |
5
|
|
|
use Gvera\Events\ThrowableFiredEvent; |
6
|
|
|
use Gvera\Helpers\dependencyInjection\DIContainer; |
7
|
|
|
use Gvera\Helpers\events\EventDispatcher; |
8
|
|
|
use Gvera\Helpers\routes\RouteManager; |
9
|
|
|
use Gvera\Services\ControllerService; |
10
|
|
|
use ReflectionException; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Application Class Doc Comment |
15
|
|
|
* |
16
|
|
|
* @category Class |
17
|
|
|
* @package src |
18
|
|
|
* @author Guido Vera |
19
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
20
|
|
|
* @link http://www.github.com/veraguido/gv |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class Gv |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
private RouteManager $routeManager; |
27
|
|
|
private DIContainer $diContainer; |
28
|
|
|
private ControllerService $controllerService; |
29
|
|
|
|
30
|
|
|
public function __construct(DIContainer $diContainer) |
31
|
|
|
{ |
32
|
|
|
$this->diContainer = $diContainer; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws ReflectionException |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
|
|
public function run() |
40
|
|
|
{ |
41
|
|
|
$this->initializeApp(); |
42
|
|
|
|
43
|
|
|
$controllerAutoloadingNames = $this->controllerService |
44
|
|
|
->autoloadControllers(__DIR__ . '/Controllers/'); |
45
|
|
|
$this->controllerService->setControllerAutoloadingNames($controllerAutoloadingNames); |
46
|
|
|
$this->initializeController($this->routeManager->getRoute($_SERVER['REQUEST_URI'])); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Throwable $e |
51
|
|
|
* @param $isDevMode |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
|
|
public function handleThrowable(Throwable $e, $isDevMode) |
55
|
|
|
{ |
56
|
|
|
EventDispatcher::dispatchEvent( |
57
|
|
|
ThrowableFiredEvent::THROWABLE_FIRED_EVENT, |
58
|
|
|
new ThrowableFiredEvent( |
59
|
|
|
$e, |
60
|
|
|
$isDevMode, |
61
|
|
|
$this->diContainer->get('httpResponse') |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws ReflectionException |
68
|
|
|
*/ |
69
|
|
|
private function initializeApp() |
70
|
|
|
{ |
71
|
|
|
$this->routeManager = $this->diContainer->get("routeManager"); |
72
|
|
|
$this->controllerService = $this->diContainer->get('controllerService'); |
73
|
|
|
$this->controllerService->setDiContainer($this->diContainer); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param bool|string $action |
78
|
|
|
* @return void |
79
|
|
|
* If the route was already defined in the routes.yml file then that one will take precedence over the |
80
|
|
|
* convention over configuration strategy (host.com/Controller/Method) |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
|
|
private function initializeController(bool|string $action):void |
84
|
|
|
{ |
85
|
|
|
if ($this->isRouteOverwritten($action)) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$uriData = strtok($_SERVER['REQUEST_URI'], '?'); |
90
|
|
|
|
91
|
|
|
$this->controllerService->startControllerLifecycle( |
92
|
|
|
$this->diContainer, |
93
|
|
|
$uriData |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $action |
99
|
|
|
* @return bool |
100
|
|
|
* @throws Exception |
101
|
|
|
*/ |
102
|
|
|
private function isRouteOverwritten($action):bool |
103
|
|
|
{ |
104
|
|
|
if (!$action) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$actionArr = explode('->', $action); |
109
|
|
|
$this->controllerService->generateSpecificControllerLifecycle( |
110
|
|
|
$actionArr[0], |
111
|
|
|
$actionArr[1] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|