forked from silexphp/Silex
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ControllerCollection.php
221 lines (197 loc) · 6.57 KB
/
ControllerCollection.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
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpFoundation\Request;
/**
* Builds Silex controllers.
*
* It acts as a staging area for routes. You are able to set the route name
* until flush() is called, at which point all controllers are frozen and
* converted to a RouteCollection.
*
* __call() forwards method-calls to Route, but returns instance of ControllerCollection
* listing Route's methods below, so that IDEs know they are valid
*
* @method ControllerCollection assert(string $variable, string $regexp)
* @method ControllerCollection value(string $variable, mixed $default)
* @method ControllerCollection convert(string $variable, mixed $callback)
* @method ControllerCollection method(string $method)
* @method ControllerCollection requireHttp()
* @method ControllerCollection requireHttps()
* @method ControllerCollection before(mixed $callback)
* @method ControllerCollection after(mixed $callback)
*
* @author Igor Wiedler <igor@wiedler.ch>
* @author Fabien Potencier <fabien@symfony.com>
*/
class ControllerCollection
{
protected $controllers = array();
protected $defaultRoute;
protected $defaultController;
protected $prefix;
public function __construct(Route $defaultRoute)
{
$this->defaultRoute = $defaultRoute;
$this->defaultController = function (Request $request) {
throw new \LogicException(sprintf('The "%s" route must have code to run when it matches.', $request->attributes->get('_route')));
};
}
/**
* Mounts controllers under the given route prefix.
*
* @param string $prefix The route prefix
* @param ControllerCollection $controllers A ControllerCollection instance
*/
public function mount($prefix, ControllerCollection $controllers)
{
$controllers->prefix = $prefix;
$this->controllers[] = $controllers;
}
/**
* Maps a pattern to a callable.
*
* You can optionally specify HTTP methods that should be matched.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function match($pattern, $to = null)
{
$route = clone $this->defaultRoute;
$route->setPath($pattern);
$this->controllers[] = $controller = new Controller($route);
$route->setDefault('_controller', null === $to ? $this->defaultController : $to);
return $controller;
}
/**
* Maps a GET request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function get($pattern, $to = null)
{
return $this->match($pattern, $to)->method('GET');
}
/**
* Maps a POST request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function post($pattern, $to = null)
{
return $this->match($pattern, $to)->method('POST');
}
/**
* Maps a PUT request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function put($pattern, $to = null)
{
return $this->match($pattern, $to)->method('PUT');
}
/**
* Maps a DELETE request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function delete($pattern, $to = null)
{
return $this->match($pattern, $to)->method('DELETE');
}
/**
* Maps an OPTIONS request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function options($pattern, $to = null)
{
return $this->match($pattern, $to)->method('OPTIONS');
}
/**
* Maps a PATCH request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function patch($pattern, $to = null)
{
return $this->match($pattern, $to)->method('PATCH');
}
public function __call($method, $arguments)
{
if (!method_exists($this->defaultRoute, $method)) {
throw new \BadMethodCallException(sprintf('Method "%s::%s" does not exist.', get_class($this->defaultRoute), $method));
}
call_user_func_array(array($this->defaultRoute, $method), $arguments);
foreach ($this->controllers as $controller) {
if ($controller instanceof Controller) {
call_user_func_array(array($controller, $method), $arguments);
}
}
return $this;
}
/**
* Persists and freezes staged controllers.
*
* @param string $prefix
*
* @return RouteCollection A RouteCollection instance
*/
public function flush($prefix = '')
{
return $this->doFlush($prefix, new RouteCollection());
}
private function doFlush($prefix, RouteCollection $routes)
{
if ($prefix !== '') {
$prefix = '/'.trim(trim($prefix), '/');
}
foreach ($this->controllers as $controller) {
if ($controller instanceof Controller) {
$controller->getRoute()->setPath($prefix.$controller->getRoute()->getPath());
if (!$name = $controller->getRouteName()) {
$name = $controller->generateRouteName('');
while ($routes->get($name)) {
$name .= '_';
}
$controller->bind($name);
}
$routes->add($name, $controller->getRoute());
$controller->freeze();
} else {
$controller->doFlush($prefix.$controller->prefix, $routes);
}
}
$this->controllers = array();
return $routes;
}
}