mediawiki-skins-MinervaNeue/includes/menu/Main/BuilderUtil.php
Stephen Niedzielski 933dc0e370 [dev] Replace menu entry inheritance with functions
Break up Menu/DefaultBuilder into functions that are reusable without
inheritance. The functions do not need much state to produce their
outputs and a weighty inheritance hierarchy makes the code difficult to
reason about. The functions are used in a following patch for the user
menu. They're now simple, independent, static functions in BuilderUtil
that are easy to reason about and compose.

Also, ban inheritance via `final` in a few places nearby. Inheritance
has not worked well in MobileFrontend and enabling it should be a
special deliberate case, not a default. E.g., in the user menu, the
changes could have been to the base class' getPersonalTools() method
such that the client passes a parameter for the advanced config or maybe
just override it in the subclass. In either case, it makes the whole
hierarchy nuanced and harder to reason about for something that should
be dead simple.

Bug: T214540
Change-Id: I6e9a2b36a1bff387eb3b33ea65b0a6806962810a
2019-07-23 18:07:02 -06:00

91 lines
2.8 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Minerva\Menu\Main;
use FatalError;
use Hooks;
use MWException;
use MediaWiki\Minerva\Menu\Definitions;
use MediaWiki\Minerva\Menu\Group;
/**
* Group generators shared between menu builders.
*
* @package MediaWiki\Minerva\Menu\Main
*/
final class BuilderUtil {
/**
* Prepares a list of links that have the purpose of discovery in the main navigation menu
* @param Definitions $definitions A menu items definitions set
* @return Group
* @throws FatalError
* @throws MWException
*/
public static function getDiscoveryTools( Definitions $definitions ): Group {
$group = new Group();
$definitions->insertHomeItem( $group );
$definitions->insertRandomItem( $group );
$definitions->insertNearbyIfSupported( $group );
// Allow other extensions to add or override tools
Hooks::run( 'MobileMenu', [ 'discovery', &$group ] );
return $group;
}
/**
* Like <code>SkinMinerva#getDiscoveryTools</code> and <code>#getPersonalTools</code>, create
* a group of configuration-related menu items. Currently, only the Settings menu item is in the
* group.
* @param Definitions $definitions A menu items definitions set
* @param bool $showMobileOptions Show MobileOptions instead of Preferences
* @return Group
* @throws MWException
*/
public static function getConfigurationTools(
Definitions $definitions, $showMobileOptions
): Group {
$group = new Group();
$showMobileOptions ?
$definitions->insertMobileOptionsItem( $group ) :
$definitions->insertPreferencesItem( $group );
return $group;
}
/**
* Returns an array of sitelinks to add into the main menu footer.
* @param Definitions $definitions A menu items definitions set
* @return Group Collection of site links
* @throws MWException
*/
public static function getSiteLinks( Definitions $definitions ): Group {
$group = new Group();
$definitions->insertAboutItem( $group );
$definitions->insertDisclaimersItem( $group );
// Allow other extensions to add or override tools
Hooks::run( 'MobileMenu', [ 'sitelinks', &$group ] );
return $group;
}
}