1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Navigation; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Navigation tree helper. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Bundle\CoreBundle\Navigation |
22
|
|
|
*/ |
23
|
|
|
class NavigationTreeHelper { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Actives the navigation nodes. |
27
|
|
|
* |
28
|
|
|
* @param Request $request The request. |
29
|
|
|
* @param AbstractNavigationNode[] $nodes The navigation nodes. |
30
|
|
|
* @param int $level The node level. |
31
|
|
|
* @return bool Returns true in case of success, false otherwise. |
32
|
|
|
*/ |
33
|
|
|
protected static function activeNodes(Request $request, array $nodes = [], $level = 0) { |
34
|
|
|
|
35
|
|
|
$result = false; |
36
|
|
|
|
37
|
|
|
/** @var AbstractNavigationNode $n */ |
38
|
|
|
foreach ($nodes as $n) { |
39
|
|
|
|
40
|
|
|
if (true === self::nodeMatch($n, $request)) { |
41
|
|
|
$current = true; |
42
|
|
|
} else { |
43
|
|
|
$current = self::activeNodes($request, $n->getNodes(), $level + 1); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (false === $current) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = $n->setActive(true)->getActive(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Active the tree. |
58
|
|
|
* |
59
|
|
|
* @param NavigationTree $tree The tree. |
60
|
|
|
* @param Request $request The request. |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public static function activeTree(NavigationTree $tree, Request $request) { |
64
|
|
|
self::activeNodes($request, $tree->getNodes()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the breadcrumbs. |
69
|
|
|
* |
70
|
|
|
* @param AbstractNavigationNode $node The navigation node. |
71
|
|
|
* @return AbstractNavigationNode[] Returns the breadcrumbs. |
72
|
|
|
*/ |
73
|
|
|
public static function getBreadcrumbs(AbstractNavigationNode $node) { |
74
|
|
|
|
75
|
|
|
$breadcrumbs = []; |
76
|
|
|
|
77
|
|
|
if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) { |
78
|
|
|
$breadcrumbs[] = $node; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($node->getNodes() as $current) { |
82
|
|
|
$breadcrumbs = array_merge($breadcrumbs, self::getBreadcrumbs($current)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $breadcrumbs; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Determines if a navigation node match an URL. |
90
|
|
|
* |
91
|
|
|
* @param AbstractNavigationNode $node The navigation node. |
92
|
|
|
* @param Request $request The request. |
93
|
|
|
* @return bool Returns true in case of success, false otherwise. |
94
|
|
|
*/ |
95
|
|
|
protected static function nodeMatch(AbstractNavigationNode $node, Request $request) { |
96
|
|
|
|
97
|
|
|
$result = false; |
98
|
|
|
|
99
|
|
|
switch ($node->getMatcher()) { |
100
|
|
|
|
101
|
|
|
case NavigationInterface::NAVIGATION_MATCHER_REGEXP: |
102
|
|
|
$result = preg_match("/" . $node->getUri() . "/", $request->getUri()); |
103
|
|
|
break; |
104
|
|
|
|
105
|
|
|
case NavigationInterface::NAVIGATION_MATCHER_ROUTER: |
106
|
|
|
$result = $request->get("_route") === $node->getUri() || $request->get("_forwarded", new ParameterBag())->get("_route") === $node->getUri(); |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case NavigationInterface::NAVIGATION_MATCHER_URL: |
110
|
|
|
$result = $request->getUri() === $node->getUri() || $request->getRequestUri() === $node->getUri(); |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return boolval($result); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|