-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
353 lines (302 loc) · 8.32 KB
/
Application.php
File metadata and controls
353 lines (302 loc) · 8.32 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
namespace Fwk\Core;
use Fwk\Events\Dispatcher;
use Symfony\Component\HttpFoundation\Request;
use Fwk\Di\Container;
use Fwk\Core\Events\RequestEvent;
use Fwk\Core\Events\DispatchEvent;
use Fwk\Core\Events\BeforeActionEvent;
use Fwk\Core\Events\AfterActionEvent;
use Fwk\Core\Events\EndEvent;
use Fwk\Core\Events\BootEvent;
use Fwk\Core\Events\ErrorEvent;
use Fwk\Core\Events\ResponseEvent;
use Symfony\Component\HttpFoundation\Response;
class Application extends Dispatcher implements \ArrayAccess
{
/**
* Application name
* @var string
*/
protected $name;
/**
* List of registered actions
* @var array
*/
protected $actions = array();
/**
* Services container (Di)
* @var Container
*/
protected $services;
/**
* The default action (i.e the "homepage")
* @var string
*/
protected $defaultAction;
/**
* Constructor
*
* @param string $name Application name
* @param Container $services Services Container (di)
*
* @return void
*/
public function __construct($name, Container $services = null)
{
$this->name = $name;
if (null === $services) {
$services = new Container();
}
$this->services = $services;
}
/**
* Registers an action
*
* @param string $actionName Name of the action
* @param ActionProxy $proxy Proxy instance to the action
*
* @return Application
*/
public function register($actionName, ActionProxy $proxy)
{
$this->actions[$actionName] = $proxy;
return $this;
}
/**
* Unregisters an action
*
* @param string $actionName Name of the action
*
* @return Application
* @throws Exception if action is not registered
*/
public function unregister($actionName)
{
if (!array_key_exists($actionName, $this->actions)) {
throw new Exception("$actionName is not a registered Action");
}
unset($this->actions[$actionName]);
return $this;
}
/**
* Returns the ActionProxy of a registered action
*
* @param string $actionName name of the action
*
* @return ActionProxy the proxy instance to the action
* @throws Exception if action is not registered
*/
public function get($actionName)
{
if (!array_key_exists($actionName, $this->actions)) {
throw new Exception("$actionName is not a registered Action");
}
return $this->actions[$actionName];
}
/**
* Tells if an action is registered
*
* @param string $actionName Name of the action
*
* @return boolean
*/
public function exists($actionName)
{
return array_key_exists($actionName, $this->actions);
}
/**
* Returns the list (array) of all registered actions (keys) and their
* according ActionProxy (values)
*
* @return array
*/
public function getActions()
{
return $this->actions;
}
/**
* Instanciates a new Application
* (useful for chaining)
*
* @param string $name Application name
* @param Container $services Services Container
*
* @return Application App instance
*/
public static function factory($name, Container $services = null)
{
return new self($name, $services);
}
/**
* Returns the Application name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns the Services Container
*
* @return Container
*/
public function getServices()
{
return $this->services;
}
/**
* Defines a Services Container
*
* @param Container $services Services Container (Di)
*
* @return Application
*/
public function setServices(Container $services)
{
$this->services = $services;
return $this;
}
/**
* Runs the Application for a defined (or new) HTTP request
*
* @param Request $request The HTTP request (optional)
*
* @return mixed
*/
public function run(Request $request = null)
{
$this->notify(new BootEvent($this));
if (null === $request) {
$request = Request::createFromGlobals();
}
$context = new Context($request);
try {
$this->notify(new RequestEvent($request, $this, $context));
if (!$context->isReady()) {
$this->notify(new DispatchEvent($this, $context));
}
if (!$context->isReady()) {
if (null === $this->defaultAction || $context->isError()) {
throw new Exception('No action found');
}
$context->setActionName($this->defaultAction);
}
$result = $this->runAction($context);
if (!$context->isDone()) {
if ($result instanceof Response) {
$context->setResponse($result);
} elseif (is_string($result)) {
$context->setResponse(new Response($result));
}
}
if ($context->getResponse() instanceof Response) {
$this->notify(
new ResponseEvent(
$context->getResponse(),
$this,
$context
)
);
}
} catch(\Exception $exp) {
$event = new ErrorEvent($exp, $this, $context);
$this->notify($event);
if (!$event->isStopped()) {
throw $exp;
} else {
return null;
}
}
$endEvent = new EndEvent($this, $context);
$this->notify($endEvent);
if ($endEvent->isStopped()) {
return null;
}
if ($context->getResponse() instanceof Response) {
return $context->getResponse();
}
return $context->getResult();
}
/**
*
* @param Context $context
*
* @return mixed
*/
public function runAction(Context $context)
{
if (!$context->isReady()) {
throw new Exception('Context is not ready (i.e. no action defined)');
}
if (!$this->exists($context->getActionName())) {
throw new Exception('Unregistered action "'. $context->getActionName() .'"');
}
$proxy = $this->get($context->getActionName());
$this->notify(new BeforeActionEvent($proxy, $this, $context));
if ($context->isDone()) {
return $context->getResult();
}
$result = $proxy->execute($this, $context);
$context->setResult($result);
$this->notify(new AfterActionEvent($proxy, $this, $context));
return $result;
}
/**
* Returns the default action name (if any)
*
* @return string
*/
public function getDefaultAction()
{
return $this->defaultAction;
}
/**
* Defines a default action. Basically the one which answers on /
*
* @param string $defaultAction Default action name
*
* @return Application
*/
public function setDefaultAction($defaultAction)
{
$this->defaultAction = $defaultAction;
return $this;
}
public function offsetExists($actionName)
{
return $this->exists($actionName);
}
public function offsetGet($actionName)
{
return $this->get($actionName);
}
public function offsetSet($actionName, $proxy)
{
if (!$proxy instanceof ActionProxy) {
$proxy = Action\ProxyFactory::factory($proxy);
}
return $this->register($actionName, $proxy);
}
public function offsetUnset($actionName)
{
return $this->unregister($actionName);
}
/**
* Adds a Plugin to the Application.
*
* @param Plugin $plugin The plugin to be loaded
*
* @return Application
*/
public function plugin(Plugin $plugin)
{
$services = $this->getServices();
// A Plugin can also be a listener of the Application Events
$this->addListener($plugin);
$plugin->loadServices($services);
$plugin->load($this);
return $this;
}
}