1 | <?php |
||
11 | final class BreadCrumbBuilder |
||
12 | { |
||
13 | /** |
||
14 | * @var bool |
||
15 | */ |
||
16 | private $extractFromRoute = true; |
||
17 | |||
18 | /** |
||
19 | * @var EventDispatcherInterface |
||
20 | */ |
||
21 | private $eventDispatcher; |
||
22 | |||
23 | /** |
||
24 | * @var FactoryInterface |
||
25 | */ |
||
26 | private $factory; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $items = array(); |
||
32 | |||
33 | /** |
||
34 | * @param FactoryInterface $factory |
||
35 | * @param EventDispatcherInterface $eventDispatcher |
||
36 | */ |
||
37 | 15 | public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher) |
|
38 | { |
||
39 | 15 | $this->factory = $factory; |
|
40 | 15 | $this->eventDispatcher = $eventDispatcher; |
|
41 | 15 | } |
|
42 | |||
43 | /** |
||
44 | * @param RequestStack $requestStack |
||
45 | * @return \Knp\Menu\ItemInterface |
||
46 | */ |
||
47 | 15 | public function createBreadCrumb(RequestStack $requestStack) |
|
65 | |||
66 | /** |
||
67 | * @return BreadCrumbBuilder |
||
68 | */ |
||
69 | 15 | public function dontExtractFromTheRequest() |
|
70 | { |
||
71 | 15 | $this->extractFromRoute = false; |
|
72 | |||
73 | 15 | return $this; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Extract the items for the breadcrumb based on a uri |
||
78 | * |
||
79 | * @param string $uri |
||
80 | * @param string $locale |
||
81 | * @return BreadCrumbBuilder |
||
82 | */ |
||
83 | 12 | public function extractItemsBasedOnUri($uri, $locale) |
|
84 | { |
||
85 | 12 | $this->dontExtractFromTheRequest(); |
|
86 | |||
87 | 12 | $item = $this->findItemBasedOnUri( |
|
88 | 12 | $this->getTheMenu(), |
|
|
|||
89 | $uri |
||
90 | 12 | ); |
|
91 | |||
92 | 12 | if ($item !== null) { |
|
93 | 3 | $this->addItemsBasedOnTheChild($item, $locale); |
|
94 | 3 | } |
|
95 | |||
96 | 12 | return $this; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Add an item into the breadcrumb |
||
101 | * |
||
102 | * @param MenuItem $item |
||
103 | * @return BreadCrumbBuilder |
||
104 | */ |
||
105 | 1 | public function addItem(MenuItem $item) |
|
106 | { |
||
107 | 1 | $this->items[] = $item; |
|
108 | |||
109 | 1 | return $this; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Add a item into the breadcrumb by passing the label and an optional URI. |
||
114 | * |
||
115 | * @param string $label |
||
116 | * @param string $uri |
||
117 | * @return BreadCrumbBuilder |
||
118 | */ |
||
119 | 2 | public function addSimpleItem($label, $uri = null) |
|
120 | { |
||
121 | 2 | $item = new MenuItem($label, $this->factory); |
|
122 | 2 | $item->setLabel($label); |
|
123 | 2 | if ($uri !== null) { |
|
124 | 2 | $item->setUri($uri); |
|
125 | 2 | } |
|
126 | |||
127 | 2 | $this->items[] = $item; |
|
128 | |||
129 | 2 | return $this; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Reset the items and add the given items all at once |
||
134 | * |
||
135 | * @param array $items |
||
136 | * @return BreadCrumbBuilder |
||
137 | */ |
||
138 | 1 | public function overwriteItems(array $items) |
|
139 | { |
||
140 | 1 | $this->items = $items; |
|
141 | |||
142 | 1 | return $this; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Add items into the breadcrumb based on a given child. |
||
147 | * |
||
148 | * @param MenuItem $item |
||
149 | * @param string $locale |
||
150 | */ |
||
151 | 3 | private function addItemsBasedOnTheChild(MenuItem $item, $locale) |
|
152 | { |
||
153 | 3 | if ($item !== null) { |
|
154 | 3 | $items = array(); |
|
155 | 3 | $temporaryItem = $item; |
|
156 | |||
157 | 3 | while ($temporaryItem->getParent() !== null) { |
|
158 | $breadCrumb = new MenuItem($temporaryItem->getName(), $this->factory); |
||
159 | $breadCrumb->setLabel($temporaryItem->getLabel()); |
||
160 | |||
161 | if ($temporaryItem->getUri() !== '#' && $temporaryItem->getUri() !== null) { |
||
162 | $breadCrumb->setUri($temporaryItem->getUri()); |
||
163 | } |
||
164 | $items[] = $breadCrumb; |
||
165 | |||
166 | $temporaryItem = $temporaryItem->getParent(); |
||
167 | } |
||
168 | |||
169 | 3 | $home = new MenuItem('core.menu.home', $this->factory); |
|
170 | 3 | $home->setLabel('core.menu.home'); |
|
171 | 3 | $home->setUri('/' . $locale); |
|
172 | 3 | $this->items[] = $home; |
|
173 | |||
174 | 3 | $this->items = array_merge($this->items, array_reverse($items)); |
|
175 | 3 | } |
|
176 | 3 | } |
|
177 | |||
178 | /** |
||
179 | * Grab the menu |
||
180 | * |
||
181 | * This method will use the ConfigureMenuEvent to get all the items |
||
182 | * |
||
183 | * @return \Knp\Menu\ItemInterface |
||
184 | */ |
||
185 | 12 | private function getTheMenu() |
|
186 | { |
||
187 | 12 | $menu = $this->factory->createItem('root'); |
|
188 | |||
189 | 12 | $this->eventDispatcher->dispatch( |
|
190 | 12 | ConfigureMenuEvent::EVENT_NAME, |
|
191 | 12 | new ConfigureMenuEvent( |
|
192 | 12 | $this->factory, |
|
193 | $menu |
||
194 | 12 | ) |
|
195 | 12 | ); |
|
196 | |||
197 | 12 | return $menu; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Find an item in the menu based on its URI |
||
202 | * |
||
203 | * @param MenuItem $menuItem |
||
204 | * @param $uri |
||
205 | * @return MenuItem|null |
||
206 | */ |
||
207 | 12 | private function findItemBasedOnUri(MenuItem $menuItem, $uri) |
|
208 | { |
||
209 | 12 | if ($uri === $menuItem->getUri()) { |
|
210 | 3 | return $menuItem; |
|
211 | } |
||
212 | |||
213 | 9 | if (!$menuItem->hasChildren()) { |
|
214 | 9 | return null; |
|
215 | } |
||
216 | |||
217 | foreach ($menuItem->getChildren() as $child) { |
||
218 | $item = $this->findItemBasedOnUri( |
||
219 | $child, |
||
220 | $uri |
||
221 | ); |
||
222 | |||
223 | if ($item !== null) { |
||
224 | return $item; |
||
225 | } |
||
226 | } |
||
227 | |||
228 | return null; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Extract the items for the breadcrumb based on request |
||
233 | * |
||
234 | * @param RequestStack $requestStack |
||
235 | */ |
||
236 | 15 | private function extractItemsBasedOnTheRequest(RequestStack $requestStack) |
|
247 | } |
||
248 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.