This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Chain.php
191 lines (161 loc) · 5.24 KB
/
Chain.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
<?php
/**
* @link http://github.com/zendframework/zend-router for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Router\Http;
use ArrayObject;
use Traversable;
use Zend\Router\Exception;
use Zend\Router\PriorityList;
use Zend\Router\RoutePluginManager;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
/**
* Chain route.
*/
class Chain extends TreeRouteStack implements RouteInterface
{
/**
* Chain routes.
*
* @var array
*/
protected $chainRoutes;
/**
* List of assembled parameters.
*
* @var array
*/
protected $assembledParams = [];
/**
* Create a new part route.
*
* @param array $routes
* @param RoutePluginManager $routePlugins
* @param ArrayObject|null $prototypes
*/
public function __construct(array $routes, RoutePluginManager $routePlugins, ArrayObject $prototypes = null)
{
$this->chainRoutes = array_reverse($routes);
$this->routePluginManager = $routePlugins;
$this->routes = new PriorityList();
$this->prototypes = $prototypes;
}
/**
* factory(): defined by RouteInterface interface.
*
* @see \Zend\Router\RouteInterface::factory()
* @param mixed $options
* @throws Exception\InvalidArgumentException
* @return Part
*/
public static function factory($options = [])
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable set of options',
__METHOD__
));
}
if (!isset($options['routes'])) {
throw new Exception\InvalidArgumentException('Missing "routes" in options array');
}
if (!isset($options['prototypes'])) {
$options['prototypes'] = null;
}
if ($options['routes'] instanceof Traversable) {
$options['routes'] = ArrayUtils::iteratorToArray($options['child_routes']);
}
if (!isset($options['route_plugins'])) {
throw new Exception\InvalidArgumentException('Missing "route_plugins" in options array');
}
return new static(
$options['routes'],
$options['route_plugins'],
$options['prototypes']
);
}
/**
* match(): defined by RouteInterface interface.
*
* @see \Zend\Router\RouteInterface::match()
* @param Request $request
* @param int|null $pathOffset
* @param array $options
* @return RouteMatch|null
*/
public function match(Request $request, $pathOffset = null, array $options = [])
{
if (!method_exists($request, 'getUri')) {
return;
}
if ($pathOffset === null) {
$mustTerminate = true;
$pathOffset = 0;
} else {
$mustTerminate = false;
}
if ($this->chainRoutes !== null) {
$this->addRoutes($this->chainRoutes);
$this->chainRoutes = null;
}
$match = new RouteMatch([]);
$uri = $request->getUri();
$pathLength = strlen($uri->getPath());
foreach ($this->routes as $route) {
$subMatch = $route->match($request, $pathOffset, $options);
if ($subMatch === null) {
return;
}
$match->merge($subMatch);
$pathOffset += $subMatch->getLength();
}
if ($mustTerminate && $pathOffset !== $pathLength) {
return;
}
return $match;
}
/**
* assemble(): Defined by RouteInterface interface.
*
* @see \Zend\Router\RouteInterface::assemble()
* @param array $params
* @param array $options
* @return mixed
*/
public function assemble(array $params = [], array $options = [])
{
if ($this->chainRoutes !== null) {
$this->addRoutes($this->chainRoutes);
$this->chainRoutes = null;
}
$this->assembledParams = [];
$routes = ArrayUtils::iteratorToArray($this->routes);
end($routes);
$lastRouteKey = key($routes);
$path = '';
foreach ($routes as $key => $route) {
$chainOptions = $options;
$hasChild = isset($options['has_child']) ? $options['has_child'] : false;
$chainOptions['has_child'] = ($hasChild || $key !== $lastRouteKey);
$path .= $route->assemble($params, $chainOptions);
$params = array_diff_key($params, array_flip($route->getAssembledParams()));
$this->assembledParams += $route->getAssembledParams();
}
return $path;
}
/**
* getAssembledParams(): defined by RouteInterface interface.
*
* @see RouteInterface::getAssembledParams
* @return array
*/
public function getAssembledParams()
{
return $this->assembledParams;
}
}