This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
SimpleRouteStack.php
310 lines (268 loc) · 7.77 KB
/
SimpleRouteStack.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
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
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Mvc_Router
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\Router;
use ArrayAccess,
ArrayIterator,
Traversable,
Zend\Stdlib\ArrayUtils,
Zend\Stdlib\RequestInterface as Request;
/**
* Simple route stack implementation.
*
* @package Zend_Mvc_Router
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class SimpleRouteStack implements RouteStackInterface
{
/**
* Stack containing all routes.
*
* @var PriorityList
*/
protected $routes;
/**
* Plugin broker to load routes.
*
* @var RouteBroker
*/
protected $routeBroker;
/**
* Default parameters.
*
* @var array
*/
protected $defaultParams = array();
/**
* Create a new simple route stack.
*/
public function __construct()
{
$this->routes = new PriorityList();
$this->routeBroker = new RouteBroker();
$this->init();
}
/**
* factory(): defined by RouteInterface interface.
*
* @see Route::factory()
* @param array|\Traversable $options
* @return SimpleRouteStack
* @throws Exception\InvalidArgumentException
*/
public static function factory($options = array())
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
}
$instance = new static();
if (isset($options['route_broker'])) {
$instance->setRouteBroker($options['route_broker']);
}
if (isset($options['routes'])) {
$instance->addRoutes($options['routes']);
}
if (isset($options['default_params'])) {
$instance->setDefaultParams($options['default_params']);
}
return $instance;
}
/**
* Init method for extending classes.
*
* @return void
*/
protected function init()
{
}
/**
* Set the route broker.
*
* @param RouteBroker $broker
* @return SimpleRouteStack
*/
public function setRouteBroker(RouteBroker $broker)
{
$this->routeBroker = $broker;
return $this;
}
/**
* Get the route broker.
*
* @return RouteBroker
*/
public function routeBroker()
{
return $this->routeBroker;
}
/**
* addRoutes(): defined by RouteStackInterface interface.
*
* @see RouteStack::addRoutes()
* @param array|\Traversable $routes
* @return SimpleRouteStack
* @throws Exception\InvalidArgumentException
*/
public function addRoutes($routes)
{
if (!is_array($routes) && !$routes instanceof Traversable) {
throw new Exception\InvalidArgumentException('addRoutes expects an array or Traversable set of routes');
}
foreach ($routes as $name => $route) {
$this->addRoute($name, $route);
}
return $this;
}
/**
* addRoute(): defined by RouteStackInterface interface.
*
* @see RouteStack::addRoute()
* @param string $name
* @param mixed $route
* @param integer $priority
* @return SimpleRouteStack
*/
public function addRoute($name, $route, $priority = null)
{
if (!$route instanceof RouteInterface) {
$route = $this->routeFromArray($route);
}
if ($priority === null && isset($route->priority)) {
$priority = $route->priority;
}
$this->routes->insert($name, $route, $priority);
return $this;
}
/**
* removeRoute(): defined by RouteStackInterface interface.
*
* @see RouteStack::removeRoute()
* @param string $name
* @return SimpleRouteStack
*/
public function removeRoute($name)
{
$this->routes->remove($name);
return $this;
}
/**
* setRoutes(): defined by RouteStackInterface interface.
*
* @param array|\Traversable $routes
* @return SimpleRouteStack
*/
public function setRoutes($routes)
{
$this->routes->clear();
$this->addRoutes($routes);
return $this;
}
/**
* Set a default parameters.
*
* @param array $params
* @return SimpleRouteStack
*/
public function setDefaultParams(array $params)
{
$this->defaultParams = $params;
return $this;
}
/**
* Set a default parameter.
*
* @param string $name
* @param mixed $value
* @return SimpleRouteStack
*/
public function setDefaultParam($name, $value)
{
$this->defaultParams[$name] = $value;
return $this;
}
/**
* Create a route from array specifications.
*
* @param array|\Traversable $specs
* @return SimpleRouteStack
*/
protected function routeFromArray($specs)
{
if ($specs instanceof Traversable) {
$specs = ArrayUtils::iteratorToArray($specs);
} elseif (!is_array($specs)) {
throw new Exception\InvalidArgumentException('Route definition must be an array or Traversable object');
}
if (!isset($specs['type'])) {
throw new Exception\InvalidArgumentException('Missing "type" option');
} elseif (!isset($specs['options'])) {
$specs['options'] = array();
}
$route = $this->routeBroker()->load($specs['type'], $specs['options']);
if (isset($specs['priority'])) {
$route->priority = $specs['priority'];
}
return $route;
}
/**
* match(): defined by RouteInterface interface.
*
* @see Route::match()
* @param Request $request
* @return RouteMatch|null
*/
public function match(Request $request)
{
foreach ($this->routes as $name => $route) {
if (($match = $route->match($request)) instanceof RouteMatch) {
$match->setMatchedRouteName($name);
foreach ($this->defaultParams as $name => $value) {
if ($match->getParam($name) === null) {
$match->setParam($name, $value);
}
}
return $match;
}
}
return null;
}
/**
* assemble(): defined by RouteInterface interface.
*
* @see Route::assemble()
* @param array $params
* @param array $options
* @return mixed
*/
public function assemble(array $params = array(), array $options = array())
{
if (!isset($options['name'])) {
throw new Exception\InvalidArgumentException('Missing "name" option');
}
$route = $this->routes->get($options['name']);
if (!$route) {
throw new Exception\RuntimeException(sprintf('Route with name "%s" not found', $options['name']));
}
unset($options['name']);
return $route->assemble(array_merge($this->defaultParams, $params), $options);
}
}