1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the bootstrap-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2017 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\BootstrapBundle\Navigation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Navigation tree. |
16
|
|
|
* |
17
|
|
|
* @author webeweb <https://github.com/webeweb/> |
18
|
|
|
* @package WBW\Bundle\BootstrapBundle\Navigation |
19
|
|
|
*/ |
20
|
|
|
class NavigationTree extends AbstractNavigationNode { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param $name The name. |
26
|
|
|
*/ |
27
|
|
|
public function __construct($name) { |
28
|
|
|
parent::__construct($name); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Actives the nodes. |
33
|
|
|
* |
34
|
|
|
* @param string $url The active URL. |
35
|
|
|
* @param array $nodes The nodes. |
36
|
|
|
* @param integer $level The node level. |
37
|
|
|
* @return boolean Returns true in case of success, false othewise. |
38
|
|
|
*/ |
39
|
|
|
public function activeNodes($url, array $nodes = [], $level = 0) { |
40
|
|
|
|
41
|
|
|
// Initialize. |
42
|
|
|
$parent = false; |
43
|
|
|
$current = false; |
|
|
|
|
44
|
|
|
$delete = false; |
45
|
|
|
|
46
|
|
|
// Handle each node. |
47
|
|
|
foreach ($nodes as $n) { |
48
|
|
|
|
49
|
|
|
// Check the node. |
50
|
|
|
if (false === ($n instanceOf AbstractNavigationNode)) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Determines if the current node matches the URL. |
55
|
|
|
if ($url === $n->getRoute()) { |
56
|
|
|
$current = true; |
57
|
|
|
$delete = true; |
58
|
|
|
} else { |
59
|
|
|
$current = $this->activeNodes($url, $n->getNodes(), $level + 1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Handle next node. |
63
|
|
|
if (false === $current) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Mark the node as active. |
68
|
|
|
$n->setActive(true); |
69
|
|
|
|
70
|
|
|
// Remove the icon only on the last level. |
71
|
|
|
if (true === $delete && 0 < $level) { |
72
|
|
|
$n->setIcon(null); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Reset. |
76
|
|
|
$parent = true; |
77
|
|
|
$current = false; |
|
|
|
|
78
|
|
|
$delete = false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Return. |
82
|
|
|
return $parent; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the breadcrumbs. |
87
|
|
|
* |
88
|
|
|
* @param AbstractNavigationNode $node The navigation node. |
89
|
|
|
* @return AsbtractNavigationNode[] Returns the navigation nodes. |
90
|
|
|
*/ |
91
|
|
|
public function getBreadcrumbs(AbstractNavigationNode $node = null) { |
92
|
|
|
|
93
|
|
|
// Create the breadcrumbs. |
94
|
|
|
$breadcrumbs = []; |
95
|
|
|
|
96
|
|
|
// Correct the parameter if necessary. |
97
|
|
|
if (null === $node) { |
98
|
|
|
$node = $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Check the instance. |
102
|
|
|
if (true === ($node instanceof NavigationNode || $node instanceof BreadcrumbNode) && true === $node->getActive()) { |
103
|
|
|
$breadcrumbs[] = $node; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Handle each node. |
107
|
|
|
foreach ($node->getNodes() as $current) { |
108
|
|
|
if (false === ($current instanceof AbstractNavigationNode)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
$breadcrumbs = array_merge($breadcrumbs, $this->getBreadcrumbs($current)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Return the breadcrumbs. |
115
|
|
|
return $breadcrumbs; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.