forked from amphp/http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArtax-Framework.php
259 lines (206 loc) · 9.19 KB
/
Artax-Framework.php
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Artax Framework Bootstrap File
*
* To use, specify a valid Artax configuration file and require
* the Artax-Framework.php bootstrap file in your front controller:
*
* ```php
* define('ARTAX_DEBUG_MODE', 1); // Optional debug mode flag (turned off by default)
* define('ARTAX_CONFIG_FILE', '/hard/path/to/config.php');
* require '/hard/path/to/Artax-Framework.php';
* ```
*
* @category Artax
* @author Daniel Lowrey <[email protected]>
* @license All code subject to the terms of the LICENSE file in the project root
* @version ${project.version}
*/
use Artax\Http\StatusCodes,
Artax\Http\StdRequestDetector,
Artax\Http\StdRequestFactory,
Artax\Http\StdResponse,
Artax\Http\SuperglobalUriDetector,
Artax\Events\Mediator,
Artax\Framework\UnifiedErrorHandler,
Artax\Framework\Configuration\AppConfig,
Artax\Framework\Configuration\Configurator,
Artax\Framework\Configuration\Parsers\PhpConfigParser,
Artax\Framework\Configuration\PluginLoader,
Artax\Framework\Configuration\PluginManifest,
Artax\Framework\Configuration\PluginManifestFactory,
Artax\Framework\Events\ProvisionedNotifier,
Artax\Framework\Http\Exceptions\HttpStatusException,
Artax\Framework\Http\Exceptions\MethodNotAllowedException,
Artax\Framework\Http\Exceptions\NotFoundException,
Artax\Framework\Events\SystemEventDeltaException,
Artax\Framework\Routing\BadResourceMethodException,
Artax\Framework\Routing\ClassResourceMapper,
Artax\Framework\Routing\ObservableResourceFactory,
Artax\Framework\Routing\ObservableRouteFactory,
Artax\Framework\Routing\ObservableRoutePool,
Artax\Injection\Provider,
Artax\Injection\ReflectionPool,
Artax\Negotiation\NotAcceptableException;
define('ARTAX_SYSTEM_VERSION', 0);
require __DIR__ . '/Artax.php';
/*
* -------------------------------------------------------------------------------------------------
* Instantiate the injection container; instantiate/share the event mediator and reflection pool.
* -------------------------------------------------------------------------------------------------
*/
$reflPool = new ReflectionPool;
$injector = new Provider($reflPool);
$mediator = new ProvisionedNotifier($injector);
$injector->share($mediator);
$injector->share($reflPool);
/*
* -------------------------------------------------------------------------------------------------
* Define the error handling environment; register error, exception and shutdown handlers.
* -------------------------------------------------------------------------------------------------
*/
if (!defined('ARTAX_DEBUG_MODE')) {
define('ARTAX_DEBUG_MODE', 0);
}
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', false);
$unifiedHandler = new UnifiedErrorHandler(new StdResponse, $mediator, ARTAX_DEBUG_MODE);
$unifiedHandler->register();
/*
* -------------------------------------------------------------------------------------------------
* Define system class injection parameters.
* -------------------------------------------------------------------------------------------------
*/
$mediatorDefinition = array(
':mediator' => $mediator
);
$httpStatusHandlerDefinition = array(
':mediator' => $mediator,
'request' => 'Artax\\Http\\StdRequest',
'response' => 'Artax\\Framework\\Http\\ObservableResponse'
);
$http500HandlerDefinition = array(
':mediator' => $mediator,
'request' => 'Artax\\Http\\StdRequest',
'response' => 'Artax\\Http\\StdResponse'
);
$observableRoutePoolDefinition = array(
':mediator' => $mediator,
'routeFactory' => 'Artax\\Framework\\Routing\\ObservableRouteFactory'
);
$injector->defineAll(array(
'Artax\\Framework\\Http\\ObservableResponse' => $mediatorDefinition,
'Artax\\Framework\\Routing\\ObservableResource' => $mediatorDefinition,
'Artax\\Framework\\Routing\\ObservableResourceFactory' => $mediatorDefinition,
'Artax\\Framework\\Routing\\ObservableRoute' => $mediatorDefinition,
'Artax\\Framework\\Routing\\ObservableRouteFactory' => $mediatorDefinition,
'Artax\\Framework\\Routing\\ObservableRoutePool' => $observableRoutePoolDefinition,
'Artax\\Framework\\Routing\\ObservableRouter' => $mediatorDefinition,
'Artax\\Framework\\Http\\StatusHandlers\\Http404' => $httpStatusHandlerDefinition,
'Artax\\Framework\\Http\\StatusHandlers\\Http405' => $httpStatusHandlerDefinition,
'Artax\\Framework\\Http\\StatusHandlers\\Http406' => $httpStatusHandlerDefinition,
'Artax\\Framework\\Http\\StatusHandlers\\Http500' => $http500HandlerDefinition,
'Artax\\Framework\\Http\\StatusHandlers\\HttpGeneral' => $httpStatusHandlerDefinition,
));
$injector->implementAll(array(
'Artax\\Routing\\RouteMatcher' => 'Artax\\Framework\\Routing\\ObservableRouter',
'Artax\\Routing\\RouteStorage' => 'Artax\\Framework\\Routing\\ObservableRoutePool'
));
/*
* -------------------------------------------------------------------------------------------------
* Load user-defined configuration.
* -------------------------------------------------------------------------------------------------
*/
if (!defined('ARTAX_CONFIG_FILE')) {
die('The ARTAX_CONFIG_FILE constant must be defined to continue' . PHP_EOL);
}
$configParser = new PhpConfigParser();
$appConfig = new AppConfig();
$appConfig->populate($configParser->parse(ARTAX_CONFIG_FILE));
$injector->share($appConfig);
$configurator = new Configurator($injector, $mediator);
$configurator->apply($appConfig);
/*
* -------------------------------------------------------------------------------------------------
* Load plugins.
* -------------------------------------------------------------------------------------------------
*/
if ($appConfig->has('plugins')) {
if (!($appConfig->has('pluginDir') && $pluginDir = $appConfig->get('pluginDir'))) {
$pluginDir = __DIR__ . '/plugins';
}
$pluginLoader = new PluginLoader(
$configurator,
$configParser,
new PluginManifestFactory,
$pluginDir,
ARTAX_SYSTEM_VERSION
);
$pluginLoader->load($appConfig->get('plugins'));
}
/*
* -------------------------------------------------------------------------------------------------
* Prevent changes to protected system event queues.
* -------------------------------------------------------------------------------------------------
*/
$mediator->unshift('__sys.http-404', 'Artax\\Framework\\Http\\StatusHandlers\\Http404');
$mediator->unshift('__sys.http-405', 'Artax\\Framework\\Http\\StatusHandlers\\Http405');
$mediator->unshift('__sys.http-406', 'Artax\\Framework\\Http\\StatusHandlers\\Http406');
$mediator->unshift('__sys.exception', 'Artax\\Framework\\Http\\StatusHandlers\\Http500');
$mediator->unshift('__mediator.delta', function(Mediator $mediator) {
list($eventName, $deltaType) = $mediator->getLastQueueDelta();
$protectedQueues = array(
'__sys.http-404',
'__sys.http-405',
'__sys.http-406',
'__sys.exception',
'__mediator.delta'
);
if (in_array($eventName, $protectedQueues)) {
throw new SystemEventDeltaException(
"Protected event listener queue may not be modified after boot: $eventName"
);
}
});
/*
* -------------------------------------------------------------------------------------------------
* Create and share a request; notify interested listeners that the system is ready.
* -------------------------------------------------------------------------------------------------
*/
$requestFactory = new StdRequestFactory(new StdRequestDetector(new SuperglobalUriDetector));
$request = $requestFactory->make($_SERVER);
$injector->share($request);
$mediator->notify('__sys.ready');
/*
* -------------------------------------------------------------------------------------------------
* Route the request and invoke the resulting resource.
* -------------------------------------------------------------------------------------------------
*/
$router = $injector->make('Artax\\Routing\\RouteMatcher');
$routePool = $injector->make('Artax\\Routing\\RouteStorage');
if (!count($routePool)) {
$routePool->addAllRoutes($appConfig->get('routes'));
}
try {
if (!$router->match($request->getPath(), $routePool)) {
throw new NotFoundException;
} else {
$resourceClass = $router->getMatchedResource();
$resourceMethod = strtolower($request->getMethod());
$routeArgs = $router->getMatchedArgs();
$resourceFactory = new ObservableResourceFactory($mediator);
$resourceMapper = new ClassResourceMapper($injector, $reflPool, $resourceFactory);
$resource = $resourceMapper->make($resourceClass, $resourceMethod, $routeArgs);
$resource();
}
} catch (NotFoundException $e) {
$mediator->notify('__sys.http-' . StatusCodes::HTTP_NOT_FOUND, $e);
} catch (BadResourceMethodException $e) {
$mediator->notify('__sys.http-' . StatusCodes::HTTP_METHOD_NOT_ALLOWED,
new MethodNotAllowedException($e->getAvailableMethods())
);
} catch (NotAcceptableException $e) {
$mediator->notify('__sys.http-' . StatusCodes::HTTP_NOT_ACCEPTABLE, $e);
} catch (HttpStatusException $e) {
$mediator->notify('__sys.http-general', $e);
}