-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApplication.php
More file actions
180 lines (138 loc) · 5.23 KB
/
Application.php
File metadata and controls
180 lines (138 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php declare(strict_types=1);
namespace Parable\Framework;
use Parable\Di\Container;
use Parable\Event\Events;
use Parable\Framework\Http\RouteDispatcher;
use Parable\Framework\Http\Tools;
use Parable\Framework\Plugins\PluginManager;
use Parable\Framework\Traits\BootableTrait;
use Parable\GetSet\GetCollection;
use Parable\Http\Request;
use Parable\Http\Response;
use Parable\Http\ResponseDispatcher;
use Parable\Routing\Route;
use Parable\Routing\Router;
class Application
{
use BootableTrait;
public const VERSION = '2.0.5';
public const PLUGIN_BEFORE_BOOT = 'plugin_before_boot';
public const PLUGIN_AFTER_BOOT = 'plugin_after_boot';
/* Replaceable instantiation */
protected ?ResponseDispatcher $responseDispatcher = null;
protected ?RouteDispatcher $routeDispatcher = null;
protected bool $hasBooted = false;
public function __construct(
protected Container $container,
protected Config $config,
protected Events $events,
protected GetCollection $get,
protected Path $path,
protected Request $request,
protected Response $response,
protected Router $router,
protected Tools $tools,
) {
if (Context::isCli()) {
throw new FrameworkException('Application cannot be used in CLI context.');
}
}
public function run(): void
{
$this->events->trigger(EventTriggers::APPLICATION_RUN_BEFORE, $this);
if (!$this->hasBooted) {
$this->boot();
}
$route = $this->matchRoute();
if ($route === null) {
$this->response->setStatusCode(404);
$this->response->setBody('404 - page not found');
} else {
$this->routeDispatcher->dispatch($route);
}
$this->dispatchResponse();
$this->events->trigger(EventTriggers::APPLICATION_RUN_AFTER, $this);
}
public function boot(): void
{
$this->events->trigger(EventTriggers::APPLICATION_BOOT_BEFORE, $this);
if ($this->hasBooted()) {
throw new FrameworkException('Application has already booted.');
}
$this->startPluginsBeforeBoot($this->events, $this->container);
if ($this->config->get('parable.debug.enabled') === true) {
$this->enableErrorReporting();
} else {
$this->disableErrorReporting();
}
$timezone = $this->config->get('parable.default-timezone');
if (is_string($timezone)) {
$this->setDefaultTimezone($timezone);
} else {
$this->setDefaultTimeZone('UTC');
}
if ($this->config->has('parable.database.type')) {
$this->setupDatabaseFromConfig($this->config);
}
if ($this->config->get('parable.session.enabled') !== false) {
$this->startSession();
}
$this->startPluginsAfterBoot($this->events, $this->container);
$this->instantiateDispatchers();
$this->hasBooted = true;
$this->events->trigger(EventTriggers::APPLICATION_BOOT_AFTER, $this);
}
public function hasBooted(): bool
{
return $this->hasBooted;
}
/**
* These dependencies have dependencies that cannot be changed upon instantiation.
* To allow plugins to do preemptive replacement, we need to delay instantiation.
*/
protected function instantiateDispatchers(): void
{
$this->responseDispatcher = $this->container->get(ResponseDispatcher::class);
$this->routeDispatcher = $this->container->get(RouteDispatcher::class);
}
protected function startSession(): void
{
$this->events->trigger(EventTriggers::APPLICATION_SESSION_START_BEFORE);
if (session_status() === PHP_SESSION_ACTIVE) {
session_destroy();
}
if ($this->config->get('parable.session.name') !== null) {
session_name((string)$this->config->get('parable.session.name'));
}
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$sessionName = session_name();
$this->events->trigger(
EventTriggers::APPLICATION_SESSION_START_AFTER,
$sessionName
);
}
protected function matchRoute(): ?Route
{
$currentRelativeUrl = $this->tools->getCurrentRelativeUrl();
$this->events->trigger(EventTriggers::APPLICATION_ROUTE_MATCH_BEFORE, $currentRelativeUrl);
$route = $this->router->match(
$this->request->getMethod(),
$currentRelativeUrl
);
if ($route instanceof Route) {
$this->events->trigger(EventTriggers::APPLICATION_ROUTE_MATCH_FOUND, $route);
} else {
$this->events->trigger(EventTriggers::APPLICATION_ROUTE_MATCH_NOT_FOUND, $currentRelativeUrl);
}
$this->events->trigger(EventTriggers::APPLICATION_ROUTE_MATCH_AFTER, $route);
return $route;
}
protected function dispatchResponse(): void
{
$this->events->trigger(EventTriggers::APPLICATION_RESPONSE_DISPATCH_BEFORE, $this->response);
$this->responseDispatcher->dispatchWithoutTerminate($this->response);
$this->events->trigger(EventTriggers::APPLICATION_RESPONSE_DISPATCH_AFTER, $this->response);
}
}