1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Veto. |
4
|
|
|
* PHP Microframework. |
5
|
|
|
* |
6
|
|
|
* @author Damien Walsh <[email protected]> |
7
|
|
|
* @copyright Damien Walsh 2013-2014 |
8
|
|
|
* @version 0.1 |
9
|
|
|
* @package veto |
10
|
|
|
*/ |
11
|
|
|
namespace Veto\Layer\Dispatcher; |
12
|
|
|
|
13
|
|
|
use Veto\DI\AbstractContainerAccessor; |
14
|
|
|
use Veto\DI\Container; |
15
|
|
|
use Veto\HTTP\Request; |
16
|
|
|
use Veto\HTTP\Response; |
17
|
|
|
use Veto\Layer\Dispatcher\Exception\DispatcherException; |
18
|
|
|
use Veto\Layer\InboundLayerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DispatcherLayer |
22
|
|
|
* |
23
|
|
|
* Dispatches a Request to a controller, obtaining a Response. |
24
|
|
|
*/ |
25
|
|
|
class DispatcherLayer extends AbstractContainerAccessor implements InboundLayerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Set the service container. |
29
|
|
|
* |
30
|
|
|
* @param Container $container |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Container $container) |
33
|
|
|
{ |
34
|
|
|
$this->container = $container; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Request $request |
39
|
|
|
* @return mixed |
40
|
|
|
* @throws DispatcherException |
41
|
|
|
*/ |
42
|
|
|
public function in(Request $request) |
43
|
|
|
{ |
44
|
|
|
// Get the controller |
45
|
|
|
$controllerSpec = $request->getParameter('_controller'); |
46
|
|
|
|
47
|
|
|
if (!$controllerSpec) { |
48
|
|
|
throw DispatcherException::notTagged( |
49
|
|
|
$request->getMethod(), |
50
|
|
|
$request->getUri() ? $request->getUri()->getPath() : '' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$controller = $this->container->get($controllerSpec['class']); |
55
|
|
|
|
56
|
|
|
if (!method_exists($controller, $controllerSpec['method'])) { |
57
|
|
|
throw DispatcherException::controllerActionDoesNotExist( |
58
|
|
|
$controllerSpec['method'], |
59
|
|
|
$controllerSpec['class'] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Prepare to run the action method |
64
|
|
|
$actionMethod = new \ReflectionMethod($controller, $controllerSpec['method']); |
65
|
|
|
$parameters = $actionMethod->getParameters(); |
66
|
|
|
$passedArgs = array(); |
67
|
|
|
|
68
|
|
|
foreach ($parameters as $parameter) { |
69
|
|
|
|
70
|
|
|
$hintedClass = $parameter->getClass(); |
71
|
|
|
$parameterName = $parameter->getName(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($hintedClass) { |
74
|
|
|
$hintedClass = $hintedClass->getName(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Special case - should the Request object be passed here? |
78
|
|
|
if ($parameterName == 'request' && $hintedClass == 'Veto\HTTP\Request') { |
79
|
|
|
$passedArgs[] = $request; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Should a request parameter be passed here? |
83
|
|
|
if ($request->hasParameter($parameterName)) { |
84
|
|
|
$passedArgs[] = $request->getParameter($parameterName); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$response = $actionMethod->invokeArgs($controller, $passedArgs); |
89
|
|
|
|
90
|
|
|
// By the end of the inbound layer list, a response should have been obtained |
91
|
|
|
if (!$response instanceof Response) { |
92
|
|
|
throw DispatcherException::controllerActionDidNotReturnResponse( |
93
|
|
|
$controllerSpec['method'], |
94
|
|
|
$controllerSpec['class'], |
95
|
|
|
gettype($response) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|