/
Factory.php
82 lines (70 loc) · 2.57 KB
/
Factory.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
<?php namespace Engine\Application;
use Engine\Exception\NullPointerException;
use Phalcon\Mvc\Application;
use Phalcon\Http\ResponseInterface;
use Phalcon\Mvc\ViewBaseInterface as View;
class Factory extends Application
{
/**
* Handles a MVC request
*
* @param string uri
* @return \Phalcon\Http\ResponseInterface|boolean
* @throws NullPointerException
*/
public function handle($uri = null)
{
$di = di();
if (is_null($di)) {
throw new NullPointerException('Dependency Injector must be defined');
}
$em = $this->getEventsManager();
if (is_null($em)) {
throw new NullPointerException('EventsManager must be defined');
}
// Fire application:boot event
if ($em->fire('application:boot', $this) === false) {
return false;
}
// Fire application:ready event
if ($em->fire('application:ready', $this) === false) {
return false;
}
// Handle Router
$router = $di->getShared('router');
$router->handle($uri);
// Handle Request
$request = $di->getShared('request');
$request->setRouter($router);
// Pass the processed router parameters to the dispatcher
$dispatcher = $di->getShared('dispatcher');
$dispatcher->setModuleName($router->getModuleName());
$dispatcher->setNamespaceName($router->getNamespaceName());
$dispatcher->setControllerName($router->getControllerName());
$dispatcher->setActionName($router->getActionName());
$dispatcher->setParams($router->getParams());
// Fire application:beforeHandleRequest event
if ($em->fire('application:beforeHandleRequest', $this, $dispatcher) === false) {
return false;
}
// Dispatch the request
if ($dispatcher->dispatch()) {
// There should be a response
$response = null;
$returnedValue = $dispatcher->getReturnedValue();
if ($returnedValue instanceof View) {
$response = $di->get(ResponseInterface::class);
$response->setContent($returnedValue->getContent());
} elseif ($returnedValue instanceof ResponseInterface) {
$response = $returnedValue;
}
if ($response instanceof ResponseInterface) {
$response->send();
}
}
// Fire application:afterDispatch event
if ($em->fire('application:afterHandleRequest', $this, $dispatcher) === false) {
return false;
}
}
}