-
Notifications
You must be signed in to change notification settings - Fork 17
/
Navigation.php
343 lines (300 loc) · 8.25 KB
/
Navigation.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
namespace SpiffyNavigation\Service;
use InvalidArgumentException;
use RecursiveIteratorIterator;
use RuntimeException;
use SpiffyNavigation\Container;
use SpiffyNavigation\Listener;
use SpiffyNavigation\NavigationEvent;
use SpiffyNavigation\Page\Page;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\RouteStackInterface;
class Navigation implements EventManagerAwareInterface
{
const EVENT_GET_HREF = 'getHref';
const EVENT_IS_ALLOWED = 'isAllowed';
/**
* Array of containers.
* @var array
*/
protected $containers = array();
/**
* @var EventManagerInterface
*/
protected $events;
/**
* Href cache using object hash.
* @var array
*/
protected $hrefCache = array();
/**
* Flag to set whether or not recursion should be enabled for isActive() checks.
* Warning: modifying this value will invalidate the isActive() cache.
* @var bool
*/
protected $isActiveRecursion = true;
/**
* Active cache using object hash.
* @var array
*/
protected $isActiveCache = array();
/**
* Router used when assmebling href's from MVC.
* @var RouteStackInterface
*/
protected $router;
/**
* Route match used for determining active status.
* @var RouteMatch
*/
protected $routeMatch;
/**
* Register the default events for this controller
*
* @return void
*/
protected function attachDefaultListeners()
{
$events = $this->getEventManager();
$events->attach(new Listener\HrefListener());
}
/**
* {@inheritDoc}
*/
public function setEventManager(EventManagerInterface $events)
{
$events->setIdentifiers(array(
'Zend\Stdlib\DispatchableInterface',
__CLASS__,
get_called_class()
));
$this->events = $events;
$this->attachDefaultListeners();
return $this;
}
/**
* {@inheritDoc}
*/
public function getEventManager()
{
if (!$this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
/**
* @param Page $page
* @trigger self::EVENT_IS_ALLOWED
* @return bool
*/
public function isAllowed(Page $page)
{
$event = new NavigationEvent();
$event->setNavigation($this)
->setTarget($page);
$results = $this->getEventManager()->trigger(self::EVENT_IS_ALLOWED, $event);
$result = $results->last();
return null === $result ? true : (bool) $results->last();
}
/**
* Check if a page is marked active.
*
* @param Page $page
* @return bool
*/
public function isActive(Page $page)
{
$hash = spl_object_hash($page);
if (isset($this->isActiveCache[$hash])) {
return $this->isActiveCache[$hash];
}
$active = false;
if ($this->getRouteMatch()) {
$name = $this->getRouteMatch()->getMatchedRouteName();
if ($page->getOption('route') == $name) {
$reqParams = array_merge($this->getRouteMatch()->getParams(), $_GET);
$pageParams = array_merge(
$page->getOption('params') ? $page->getOption('params') : array(),
$page->getOption('query_params') ? $page->getOption('query_params') : array()
);
$active = count(array_intersect_assoc($reqParams, $pageParams)) == count($pageParams);
} elseif ($this->getIsActiveRecursion()) {
$iterator = new RecursiveIteratorIterator($page, RecursiveIteratorIterator::CHILD_FIRST);
/** @var \SpiffyNavigation\Page\Page $page */
foreach ($iterator as $leaf) {
if (!$leaf instanceof Page) {
continue;
}
if ($this->isActive($leaf)) {
$active = true;
break;
}
}
}
}
$this->isActiveCache[$hash] = $active;
return $active;
}
/**
* Get the href for a page.
*
* @param \SpiffyNavigation\Page\Page $page
* @throws \RuntimeException when an href can not be generated.
* @trigger self::EVENT_GET_HREF
* @return \SpiffyNavigation\Page\Page
*/
public function getHref(Page $page)
{
$hash = spl_object_hash($page);
if (isset($this->hrefCache[$hash])) {
return $this->hrefCache[$hash];
}
$event = new NavigationEvent();
$event->setNavigation($this)
->setTarget($page);
$this->getEventManager()->trigger(self::EVENT_GET_HREF, $event);
$href = $event->getResult();
if (!$href) {
throw new RuntimeException('Unable to construct href');
}
$this->hrefCache[$hash] = $href;
return $href;
}
/**
* Add a container to the stack.
*
* @param string $name
* @param Container $container
* @return Navigation
* @throws InvalidArgumentException on duplicate container
*/
public function addContainer($name, Container $container)
{
if ($this->hasContainer($name)) {
throw new InvalidArgumentException(sprintf(
'A container with name "%s" already exists.',
$name
));
}
$this->containers[$name] = $container;
return $this;
}
/**
* Get a container by name.
*
* @param string $name
* @return Container
* @throws InvalidArgumentException on missing container
*/
public function getContainer($name)
{
if (!$this->hasContainer($name)) {
throw new InvalidArgumentException(sprintf(
'No container with name "%s" could be found.',
$name
));
}
return $this->containers[$name];
}
/**
* Check if a container with name exists.
*
* @param string $name
* @return bool
*/
public function hasContainer($name)
{
return isset($this->containers[$name]);
}
/**
* Get containers.
*
* @return array
*/
public function getContainers()
{
return $this->containers;
}
/**
* Remove a container from the stack.
*
* @param string $name
* @return Navigation
* @throws InvalidArgumentException on missing container
*/
public function removeContainer($name)
{
if (!$this->hasContainer($name)) {
throw new InvalidArgumentException(sprintf(
'No container with name "%s" could be found.',
$name
));
}
unset($this->containers[$name]);
return $this;
}
/**
* Clear containers.
*
* @return Navigation
*/
public function clearContainers()
{
$this->containers = array();
return $this;
}
/**
* @param RouteMatch $routeMatch
* @return Navigation
*/
public function setRouteMatch($routeMatch)
{
$this->routeMatch = $routeMatch;
return $this;
}
/**
* @return RouteMatch
*/
public function getRouteMatch()
{
return $this->routeMatch;
}
/**
* @param \Zend\Mvc\Router\RouteStackInterface $router
* @return $this
*/
public function setRouter($router)
{
$this->router = $router;
return $this;
}
/**
* @return \Zend\Mvc\Router\RouteStackInterface
*/
public function getRouter()
{
return $this->router;
}
/**
* @param boolean $isActiveRecursion
* @return Navigation
*/
public function setIsActiveRecursion($isActiveRecursion)
{
if ($isActiveRecursion != $this->isActiveRecursion) {
$this->isActiveRecursion = $isActiveRecursion;
$this->isActiveCache = array();
}
return $this;
}
/**
* @return boolean
*/
public function getIsActiveRecursion()
{
return $this->isActiveRecursion;
}
}