| Total Complexity | 4 |
| Total Lines | 54 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class MenuExtension extends AbstractExtension |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var MenuFactory |
||
| 15 | */ |
||
| 16 | private $menuFactory; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Environment |
||
| 20 | */ |
||
| 21 | private $environment; |
||
| 22 | |||
| 23 | public function __construct(MenuFactory $menuFactory, Environment $environment) |
||
| 24 | { |
||
| 25 | $this->menuFactory = $menuFactory; |
||
| 26 | $this->environment = $environment; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function getFunctions(): array |
||
| 30 | { |
||
| 31 | return [ |
||
| 32 | new TwigFunction('admin_menu', [$this, 'getMenu'], ['is_safe' => ['html']]), |
||
| 33 | new TwigFunction('admin_has_menu', [$this, 'hasMenu']), |
||
| 34 | ]; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Render a menu according to given name. |
||
| 39 | * |
||
| 40 | * @param string $name |
||
| 41 | * @param ViewInterface|null $view |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function getMenu(string $name, ViewInterface $view = null) |
||
| 46 | { |
||
| 47 | $menu = $this->menuFactory->getMenu($name); |
||
| 48 | |||
| 49 | return $this->environment->render($menu->get('template'), [ |
||
| 50 | 'menu' => $menu, |
||
| 51 | 'admin' => $view, |
||
| 52 | ]); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Return true if a menu with the given name exists. |
||
| 57 | * |
||
| 58 | * @param string $name |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public function hasMenu(string $name): bool |
||
| 65 | } |
||
| 66 | } |
||
| 67 |