1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gvera\Helpers\bootstrap; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Gvera\Cache\Cache; |
7
|
|
|
use Gvera\Cache\FilesCache; |
8
|
|
|
use Gvera\Exceptions\InvalidArgumentException; |
9
|
|
|
use Gvera\Helpers\config\Config; |
10
|
|
|
use Gvera\Helpers\dependencyInjection\DIContainer; |
11
|
|
|
use Gvera\Helpers\dependencyInjection\DIRegistry; |
12
|
|
|
use Gvera\Helpers\events\EventListenerRegistry; |
13
|
|
|
use Gvera\Helpers\http\JSONResponse; |
14
|
|
|
use Gvera\Helpers\http\Response; |
15
|
|
|
use Gvera\Helpers\locale\Locale; |
16
|
|
|
use Gvera\Helpers\routes\RouteManager; |
17
|
|
|
use ReflectionException; |
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
19
|
|
|
use Throwable; |
20
|
|
|
|
21
|
|
|
class Bootstrap |
22
|
|
|
{ |
23
|
|
|
const CONFIG_DEFAULT_FILE_PATH = CONFIG_ROOT . "config.yml"; |
24
|
|
|
const CONFIG_DEFAULT_IOC_PATH = CONFIG_ROOT . "ioc.yml"; |
25
|
|
|
const ROUTES_DEFAULT_FILE_PATH = CONFIG_ROOT . "routes.yml"; |
26
|
|
|
private DIContainer $diContainer; |
27
|
|
|
private Config $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return Config |
31
|
|
|
*/ |
32
|
|
|
public function getConfig(): Config |
33
|
|
|
{ |
34
|
|
|
return $this->config; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Bootstrap constructor. The order of things in this constructor is important |
39
|
|
|
* This can be only executed from a webserver as it will bootstrap |
40
|
|
|
* all that's needed for the requests and responses to be covered |
41
|
|
|
* @throws InvalidArgumentException |
42
|
|
|
* @throws ReflectionException |
43
|
|
|
* @throws Exception |
44
|
|
|
*/ |
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$this->config = new Config(self::CONFIG_DEFAULT_FILE_PATH); |
48
|
|
|
Locale::setLocalesDirectory(LOCALE_ROOT); |
49
|
|
|
Cache::setConfig($this->config); |
50
|
|
|
$this->initializeDIContainer(); |
51
|
|
|
$this->validateThrottling(); |
52
|
|
|
$this->initializeEventListenerRegistry(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return DIContainer |
57
|
|
|
*/ |
58
|
|
|
public function getDiContainer(): DIContainer |
59
|
|
|
{ |
60
|
|
|
return $this->diContainer; |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function isDevMode(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->config->getConfigItem('devmode'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
private function initializeDIContainer() |
74
|
|
|
{ |
75
|
|
|
$diContainer = new DIContainer(); |
76
|
|
|
$diRegistry = new DIRegistry($diContainer, self::CONFIG_DEFAULT_IOC_PATH); |
77
|
|
|
$diRegistry->registerObjects(); |
78
|
|
|
$this->diContainer = $diContainer; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @throws ReflectionException |
83
|
|
|
*/ |
84
|
|
|
private function validateThrottling() |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
|
|
if ($this->config->getConfigItem('throttling')) { |
88
|
|
|
if (!isset($_SERVER['REMOTE_ADDR'])) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$throttlingService = $this->diContainer->get('throttlingService'); |
93
|
|
|
$throttlingService->setIp($_SERVER['REMOTE_ADDR']); |
94
|
|
|
$throttlingService->setAllowedRequestsPerSecond( |
95
|
|
|
$this->config->getConfigItem('allowed_requests_per_second') |
96
|
|
|
); |
97
|
|
|
$throttlingService->validateRate(); |
98
|
|
|
} |
99
|
|
|
} catch (Throwable $exception) { |
100
|
|
|
$this->diContainer->get('logger')->err($exception->getMessage()); |
101
|
|
|
$httpResponse = $this->diContainer->get('httpResponse'); |
102
|
|
|
$content = ['message' => Locale::getLocale('something went wrong')]; |
103
|
|
|
|
104
|
|
|
$httpResponse->response(new JSONResponse($content, Response::HTTP_RESPONSE_BAD_REQUEST)); |
105
|
|
|
$httpResponse->terminate(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @throws ReflectionException |
111
|
|
|
*/ |
112
|
|
|
private function initializeEventListenerRegistry() |
113
|
|
|
{ |
114
|
|
|
$eventRegistry = new GvEventListenerRegistry($this->diContainer); |
115
|
|
|
$eventRegistry->registerEventListeners(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|