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