1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Backend\Template\Components;
17
18use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
19use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21/**
22 * MenuRegistry
23 */
24class MenuRegistry
25{
26    /**
27     * Internal array that stores all registered menus
28     *
29     * @var array
30     */
31    protected $menus = [];
32
33    /**
34     * Adds a menu to the registry
35     *
36     * @param Menu $menu Menu object to add to the menuRegistry
37     *
38     * @throws \InvalidArgumentException In case a menu is not valid
39     */
40    public function addMenu(Menu $menu)
41    {
42        if (!$menu->isValid($menu)) {
43            throw new \InvalidArgumentException('Menu "' . $menu->getIdentifier() . '" is not valid', 1442236362);
44        }
45        $this->menus[$menu->getIdentifier()] = clone $menu;
46    }
47
48    /**
49     * Returns all menus in an abstract array
50     *
51     * @return array
52     */
53    public function getMenus()
54    {
55        // @todo do we want to provide a hook here?
56        /**
57         * For Code Completion
58         *
59         * @var int $key
60         * @var Menu $menu
61         */
62        foreach ($this->menus as $key => $menu) {
63            if (empty($menu->getMenuItems())) {
64                unset($this->menus[$key]);
65            }
66        }
67        return $this->menus;
68    }
69
70    /**
71     * MenuFactory method
72     *
73     * @return Menu
74     */
75    public function makeMenu()
76    {
77        $menu = GeneralUtility::makeInstance(Menu::class);
78        return $menu;
79    }
80}
81