use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Router / Http / Part.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Mvc\Router\Http;
11
12 use ArrayObject;
13 use Traversable;
14 use Zend\Mvc\Router\Exception;
15 use Zend\Mvc\Router\PriorityList;
16 use Zend\Mvc\Router\RoutePluginManager;
17 use Zend\Stdlib\ArrayUtils;
18 use Zend\Stdlib\RequestInterface as Request;
19
20 /**
21 * Part route.
22 */
23 class Part extends TreeRouteStack implements RouteInterface
24 {
25 /**
26 * RouteInterface to match.
27 *
28 * @var RouteInterface
29 */
30 protected $route;
31
32 /**
33 * Whether the route may terminate.
34 *
35 * @var bool
36 */
37 protected $mayTerminate;
38
39 /**
40 * Child routes.
41 *
42 * @var mixed
43 */
44 protected $childRoutes;
45
46 /**
47 * Create a new part route.
48 *
49 * @param mixed $route
50 * @param bool $mayTerminate
51 * @param RoutePluginManager $routePlugins
52 * @param array|null $childRoutes
53 * @param ArrayObject|null $prototypes
54 * @throws Exception\InvalidArgumentException
55 */
56 public function __construct($route, $mayTerminate, RoutePluginManager $routePlugins, array $childRoutes = null, ArrayObject $prototypes = null)
57 {
58 $this->routePluginManager = $routePlugins;
59
60 if (!$route instanceof RouteInterface) {
61 $route = $this->routeFromArray($route);
62 }
63
64 if ($route instanceof self) {
65 throw new Exception\InvalidArgumentException('Base route may not be a part route');
66 }
67
68 $this->route = $route;
69 $this->mayTerminate = $mayTerminate;
70 $this->childRoutes = $childRoutes;
71 $this->prototypes = $prototypes;
72 $this->routes = new PriorityList();
73 }
74
75 /**
76 * factory(): defined by RouteInterface interface.
77 *
78 * @see \Zend\Mvc\Router\RouteInterface::factory()
79 * @param mixed $options
80 * @return Part
81 * @throws Exception\InvalidArgumentException
82 */
83 public static function factory($options = [])
84 {
85 if ($options instanceof Traversable) {
86 $options = ArrayUtils::iteratorToArray($options);
87 } elseif (!is_array($options)) {
88 throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
89 }
90
91 if (!isset($options['route'])) {
92 throw new Exception\InvalidArgumentException('Missing "route" in options array');
93 }
94
95 if (!isset($options['route_plugins'])) {
96 throw new Exception\InvalidArgumentException('Missing "route_plugins" in options array');
97 }
98
99 if (!isset($options['prototypes'])) {
100 $options['prototypes'] = null;
101 }
102
103 if (!isset($options['may_terminate'])) {
104 $options['may_terminate'] = false;
105 }
106
107 if (!isset($options['child_routes']) || !$options['child_routes']) {
108 $options['child_routes'] = null;
109 }
110
111 if ($options['child_routes'] instanceof Traversable) {
112 $options['child_routes'] = ArrayUtils::iteratorToArray($options['child_routes']);
113 }
114
115 return new static(
116 $options['route'],
117 $options['may_terminate'],
118 $options['route_plugins'],
119 $options['child_routes'],
120 $options['prototypes']
121 );
122 }
123
124 /**
125 * match(): defined by RouteInterface interface.
126 *
127 * @see \Zend\Mvc\Router\RouteInterface::match()
128 * @param Request $request
129 * @param integer|null $pathOffset
130 * @param array $options
131 * @return RouteMatch|null
132 */
133 public function match(Request $request, $pathOffset = null, array $options = [])
134 {
135 if ($pathOffset === null) {
136 $pathOffset = 0;
137 }
138
139 $match = $this->route->match($request, $pathOffset, $options);
140
141 if ($match !== null && method_exists($request, 'getUri')) {
142 if ($this->childRoutes !== null) {
143 $this->addRoutes($this->childRoutes);
144 $this->childRoutes = null;
145 }
146
147 $nextOffset = $pathOffset + $match->getLength();
148
149 $uri = $request->getUri();
150 $pathLength = strlen($uri->getPath());
151
152 if ($this->mayTerminate && $nextOffset === $pathLength) {
153 $query = $uri->getQuery();
154 if ('' == trim($query) || !$this->hasQueryChild()) {
155 return $match;
156 }
157 }
158
159 if (isset($options['translator']) && !isset($options['locale']) && null !== ($locale = $match->getParam('locale', null))) {
160 $options['locale'] = $locale;
161 }
162
163 foreach ($this->routes as $name => $route) {
164 if (($subMatch = $route->match($request, $nextOffset, $options)) instanceof RouteMatch) {
165 if ($match->getLength() + $subMatch->getLength() + $pathOffset === $pathLength) {
166 return $match->merge($subMatch)->setMatchedRouteName($name);
167 }
168 }
169 }
170 }
171
172 return;
173 }
174
175 /**
176 * assemble(): Defined by RouteInterface interface.
177 *
178 * @see \Zend\Mvc\Router\RouteInterface::assemble()
179 * @param array $params
180 * @param array $options
181 * @return mixed
182 * @throws Exception\RuntimeException
183 */
184 public function assemble(array $params = [], array $options = [])
185 {
186 if ($this->childRoutes !== null) {
187 $this->addRoutes($this->childRoutes);
188 $this->childRoutes = null;
189 }
190
191 $options['has_child'] = (isset($options['name']));
192
193 if (isset($options['translator']) && !isset($options['locale']) && isset($params['locale'])) {
194 $options['locale'] = $params['locale'];
195 }
196
197 $path = $this->route->assemble($params, $options);
198 $params = array_diff_key($params, array_flip($this->route->getAssembledParams()));
199
200 if (!isset($options['name'])) {
201 if (!$this->mayTerminate) {
202 throw new Exception\RuntimeException('Part route may not terminate');
203 } else {
204 return $path;
205 }
206 }
207
208 unset($options['has_child']);
209 $options['only_return_path'] = true;
210 $path .= parent::assemble($params, $options);
211
212 return $path;
213 }
214
215 /**
216 * getAssembledParams(): defined by RouteInterface interface.
217 *
218 * @see RouteInterface::getAssembledParams
219 * @return array
220 */
221 public function getAssembledParams()
222 {
223 // Part routes may not occur as base route of other part routes, so we
224 // don't have to return anything here.
225 return [];
226 }
227
228 /**
229 * Is one of the child routes a query route?
230 *
231 * @return bool
232 */
233 protected function hasQueryChild()
234 {
235 foreach ($this->routes as $route) {
236 if ($route instanceof Query) {
237 return true;
238 }
239 }
240 return false;
241 }
242 }