| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | namespace MediaWiki\Minerva\Menu\Main; |
| 21 | |
| 22 | /** |
| 23 | * Director responsible for building Main Menu |
| 24 | */ |
| 25 | final class MainMenuDirector { |
| 26 | |
| 27 | private IMainMenuBuilder $builder; |
| 28 | private ?array $menuData = null; |
| 29 | |
| 30 | /** |
| 31 | * Director responsible for Main Menu building |
| 32 | * |
| 33 | * @param IMainMenuBuilder $builder |
| 34 | */ |
| 35 | public function __construct( IMainMenuBuilder $builder ) { |
| 36 | $this->builder = $builder; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns a data representation of the main menus |
| 41 | * |
| 42 | * @param array $contentNavUrls result of buildContentNavigationUrls |
| 43 | * @param array $sidebar |
| 44 | * @return array |
| 45 | */ |
| 46 | public function getMenuData( array $contentNavUrls, array $sidebar ): array { |
| 47 | if ( $this->menuData === null ) { |
| 48 | $this->menuData = $this->buildMenu( |
| 49 | $contentNavUrls, |
| 50 | $sidebar |
| 51 | ); |
| 52 | } |
| 53 | return $this->menuData; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Build the menu data array that can be passed to views/javascript |
| 58 | * |
| 59 | * @param array $contentNavUrls |
| 60 | * @param array $sidebar |
| 61 | * @return array |
| 62 | */ |
| 63 | private function buildMenu( array $contentNavUrls, array $sidebar ): array { |
| 64 | $builder = $this->builder; |
| 65 | $menuData = [ |
| 66 | 'items' => [ |
| 67 | 'groups' => [], |
| 68 | 'sitelinks' => $builder->getSiteLinks()->getEntries() |
| 69 | ] |
| 70 | ]; |
| 71 | |
| 72 | $groups = [ |
| 73 | // sidebar comes from MediaWiki:Sidebar so we can't assume it doesn't exist. |
| 74 | $builder->getDiscoveryGroup( $sidebar['navigation'] ?? [] ), |
| 75 | $builder->getInteractionToolsGroup(), |
| 76 | $builder->getPersonalToolsGroup( $contentNavUrls['user-menu'] ), |
| 77 | $builder->getSettingsGroup(), |
| 78 | $builder->getDonateGroup(), |
| 79 | ]; |
| 80 | foreach ( $groups as $group ) { |
| 81 | if ( $group->hasEntries() ) { |
| 82 | $menuData['items']['groups'][] = $group->serialize(); |
| 83 | } |
| 84 | } |
| 85 | return $menuData; |
| 86 | } |
| 87 | } |