-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathControllerResolver.php
More file actions
213 lines (182 loc) · 6.83 KB
/
ControllerResolver.php
File metadata and controls
213 lines (182 loc) · 6.83 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
<?php
namespace Foolz\FoolFrame\Model;
use Foolz\FoolFrame\Controller\ControllerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ControllerResolver extends \Symfony\Component\HttpKernel\Controller\ControllerResolver
{
/**
* @var Context
*/
private $context;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* Holds parameters taken from the _suffix
*
* @var null|array
*/
private $parameters = null;
/**
* Constructor.
*
* @param Context $context
* @param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct(Context $context, LoggerInterface $logger = null)
{
$this->context = $context;
$this->logger = $logger;
}
/**
* Returns the Controller instance associated with a Request.
*
* This method looks for a '_controller' request attribute that represents
* the controller name (a string like ClassName::MethodName).
*
* @param Request $request A Request instance
*
* @return mixed|Boolean A PHP callable representing the Controller,
* or false if this resolver is not able to determine the controller
*
* @throws \InvalidArgumentException|\LogicException If the controller can't be found
*
* @api
*/
public function getController(\Symfony\Component\HttpFoundation\Request $request)
{
if (!$controller = $request->attributes->get('_controller')) {
if (null !== $this->logger) {
$this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing');
}
return false;
}
if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
return $controller;
}
if (false === strpos($controller, ':')) {
if (method_exists($controller, '__invoke')) {
return new $controller;
} elseif (function_exists($controller)) {
return $controller;
}
}
list($controller, $method) = $this->createController($controller);
$this->parameters = array();
if ($suffix = $request->attributes->get('_suffix')) {
$parameters_temp = explode('/', $suffix);
$parameters_temp = array_filter(
$parameters_temp,
function ($el) {
return $el !== '';
}
);
foreach ($parameters_temp as $p) {
$this->parameters[] = $p;
}
}
if ($method === '*') {
if (count($this->parameters) > 0) {
$method = array_shift($this->parameters);
} elseif ($default_suffix = $request->attributes->get('_default_suffix')) {
$method = $default_suffix;
} else {
$method = 'index';
}
}
/** @var $controller ControllerInterface */
$controller->setContext($this->context);
$controller->setRequest($request);
if (method_exists($controller, 'before')) {
$controller->before($method);
}
if (method_exists($controller, 'security')) {
if (!$controller->security()) {
return array($controller, 'redirectToLogin');
}
}
if (method_exists($controller, 'router')) {
list($controller, $method, $this->parameters) = $controller->router($method, $this->parameters);
} else {
$method = 'action_'.$method;
}
if (!method_exists($controller, $method)) {
throw new NotFoundHttpException;
//throw new \InvalidArgumentException(sprintf('Method "%s::%s" does not exist.', get_class($controller), $method));
}
return array($controller, $method);
}
/**
* Returns the arguments to pass to the controller.
*
* @param Request $request A Request instance
* @param mixed $controller A PHP callable
*
* @return array
*
* @throws \RuntimeException When value for argument given is not provided
*
* @api
*/
public function getArguments(\Symfony\Component\HttpFoundation\Request $request, $controller)
{
if ($this->parameters !== null) {
return $this->parameters;
}
if (is_array($controller)) {
$r = new \ReflectionMethod($controller[0], $controller[1]);
} elseif (is_object($controller) && !$controller instanceof \Closure) {
$r = new \ReflectionObject($controller);
$r = $r->getMethod('__invoke');
} else {
$r = new \ReflectionFunction($controller);
}
return $this->doGetArguments($request, $controller, $r->getParameters());
}
protected function doGetArguments(\Symfony\Component\HttpFoundation\Request $request, $controller, array $parameters)
{
$attributes = $request->attributes->all();
$arguments = array();
foreach ($parameters as $param) {
if (array_key_exists($param->name, $attributes)) {
$arguments[] = $attributes[$param->name];
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
$arguments[] = $param->getDefaultValue();
} else {
if (is_array($controller)) {
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
} elseif (is_object($controller)) {
$repr = get_class($controller);
} else {
$repr = $controller;
}
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name));
}
}
return $arguments;
}
/**
* Returns a callable for the given controller.
*
* @param string $controller A Controller string
*
* @return mixed A PHP callable
*
* @throws \InvalidArgumentException
*/
protected function createController($controller)
{
if (false === strpos($controller, '::')) {
throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
}
list($class, $method) = explode('::', $controller, 2);
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
return array(new $class(), $method);
}
}