|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2016: Luis Ramón López López |
|
6
|
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU Affero General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppBundle\Service; |
|
22
|
|
|
|
|
23
|
|
|
use AppBundle\Menu\MenuItem; |
|
24
|
|
|
|
|
25
|
|
|
class MenuBuilderChain |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var MenuBuilderInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $menuBuilders; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var MenuItem[]|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private $menuCache; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->menuBuilders = []; |
|
40
|
|
|
$this->menuCache = null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function addMenuBuilder(MenuBuilderInterface $menuBuilder) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->menuBuilders[$menuBuilder->getMenuPriority()] = $menuBuilder; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getChain() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->menuBuilders; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getMenu() |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->menuCache) { |
|
56
|
|
|
return $this->menuCache; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$menu = []; |
|
60
|
|
|
|
|
61
|
|
|
$mainItem = new MenuItem(); |
|
62
|
|
|
$mainItem |
|
63
|
|
|
->setName('frontpage') |
|
64
|
|
|
->setRouteName('frontpage') |
|
65
|
|
|
->setCaption('menu.frontpage') |
|
66
|
|
|
->setDescription('menu.frontpage.detail') |
|
67
|
|
|
->setColor('white') |
|
68
|
|
|
->setIcon('home'); |
|
69
|
|
|
|
|
70
|
|
|
$menu[] = $mainItem; |
|
71
|
|
|
|
|
72
|
|
|
ksort($this->menuBuilders); |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->menuBuilders as $menuBuilder) { |
|
75
|
|
|
$menuBuilder->updateMenu($menu); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->menuCache = $menu; |
|
79
|
|
|
|
|
80
|
|
|
return $menu; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getSubmenuItemByRouteName(MenuItem $submenu, $name) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($submenu->getRouteName() === $name) { |
|
86
|
|
|
return $submenu; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$children = $submenu->getChildren(); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($children as $item) { |
|
92
|
|
|
$result = $this->getSubmenuItemByRouteName($item, $name); |
|
93
|
|
|
if (null !== $result) { |
|
94
|
|
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getMenuItemByRouteName($name) |
|
102
|
|
|
{ |
|
103
|
|
|
$menu = $this->getMenu(); |
|
104
|
|
|
$root = reset($menu); |
|
105
|
|
|
|
|
106
|
|
|
return $this->getSubmenuItemByRouteName($root, $name); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function clearCache() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->menuCache = null; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.