These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\MenuBundle\Twig; |
||
4 | |||
5 | use Kunstmaan\MenuBundle\Entity\MenuItem; |
||
6 | use Kunstmaan\MenuBundle\Repository\MenuItemRepositoryInterface; |
||
7 | use Kunstmaan\MenuBundle\Service\RenderService; |
||
8 | |||
9 | class MenuTwigExtension extends \Twig_Extension |
||
10 | { |
||
11 | /** |
||
12 | * @var RenderService |
||
13 | */ |
||
14 | private $renderService; |
||
15 | |||
16 | /** |
||
17 | * @var MenuItemRepositoryInterface |
||
18 | */ |
||
19 | private $repository; |
||
20 | |||
21 | /** |
||
22 | * @param MenuItemRepositoryInterface $repository |
||
23 | * @param RenderService $renderService |
||
24 | */ |
||
25 | public function __construct(MenuItemRepositoryInterface $repository, RenderService $renderService) |
||
26 | { |
||
27 | $this->renderService = $renderService; |
||
28 | $this->repository = $repository; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Returns a list of functions to add to the existing list. |
||
33 | * |
||
34 | * @return array An array of functions |
||
35 | */ |
||
36 | public function getFunctions() |
||
37 | { |
||
38 | return array( |
||
39 | new \Twig_SimpleFunction( |
||
0 ignored issues
–
show
|
|||
40 | 'get_menu', |
||
41 | array($this, 'getMenu'), |
||
42 | array( |
||
43 | 'is_safe' => array('html'), |
||
44 | 'needs_environment' => true, |
||
45 | ) |
||
46 | ), |
||
47 | new \Twig_SimpleFunction('get_menu_items', array($this, 'getMenuItems')), |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get a html representation of a menu. |
||
53 | * |
||
54 | * @param string $name |
||
55 | * @param string $lang |
||
56 | * @param array $options |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getMenu(\Twig_Environment $environment, $name, $lang, $options = array()) |
||
61 | { |
||
62 | $options = array_merge($this->getDefaultOptions(), $options); |
||
63 | |||
64 | $renderService = $this->renderService; |
||
65 | $options['nodeDecorator'] = function ($node) use ($environment, $renderService, $options) { |
||
66 | return $renderService->renderMenuItemTemplate($environment, $node, $options); |
||
67 | }; |
||
68 | |||
69 | $arrayResult = $this->getMenuItems($name, $lang); |
||
70 | |||
71 | return $this->repository->buildTree($arrayResult, $options); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get an array with menu items of a menu. |
||
76 | * |
||
77 | * @param string $name |
||
78 | * @param string $lang |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | public function getMenuItems($name, $lang) |
||
83 | { |
||
84 | /** @var MenuItem $menuRepo */ |
||
85 | $arrayResult = $this->repository->getMenuItemsForLanguage($name, $lang); |
||
86 | |||
87 | // Make sure the parent item is not offline |
||
88 | $foundIds = array(); |
||
89 | foreach ($arrayResult as $array) { |
||
90 | $foundIds[] = $array['id']; |
||
91 | } |
||
92 | foreach ($arrayResult as $key => $array) { |
||
93 | if (!\is_null($array['parent']) && !\in_array($array['parent']['id'], $foundIds)) { |
||
94 | unset($arrayResult[$key]); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $arrayResult; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get the default options to render the html. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | private function getDefaultOptions() |
||
107 | { |
||
108 | return array( |
||
109 | 'decorate' => true, |
||
110 | 'rootOpen' => '<ul>', |
||
111 | 'rootClose' => '</ul>', |
||
112 | 'childOpen' => '<li>', |
||
113 | 'childClose' => '</li>', |
||
114 | ); |
||
115 | } |
||
116 | } |
||
117 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.