mediawiki-skins-MinervaNeue/includes/menu/User/UserMenuDirector.php
Piotr Miazga ad04f31441 Follow-up: User menu improvements
Changes:
 - We should limit the interfaces we pass around,
  AdvancedUserMenuBuilder doesn't need whole IContextSource as it
  uses only msg() method. It's better to define that this methods
  needs only MessageLocalizer
 - move UserMenuDirector into ServiceWiring to be consistent with
 other Directors/Builders
 - pass PersonalTools as a dependency to UserMenuDirector, which
 will pass to each Builder. The personalTools is set of links
 that can/should be used when rendering user menu (which in the
 fact has almost same subset of tools as the personal toolbox)

Bug: T214540
Change-Id: I7f744651b0665452a5a9d1ce661f20547e80812d
2019-07-25 08:41:22 -06:00

79 lines
2.4 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\User;
use MinervaUI;
use TemplateParser;
use MessageLocalizer;
/**
* Director responsible for building the user menu.
*/
final class UserMenuDirector {
/**
* @var IUserMenuBuilder
*/
private $builder;
/**
* @var MessageLocalizer
*/
private $localizer;
/**
* @param IUserMenuBuilder $builder
* @param MessageLocalizer $localizer
*/
public function __construct( IUserMenuBuilder $builder, MessageLocalizer $localizer ) {
$this->builder = $builder;
$this->localizer = $localizer;
}
/**
* Build the menu data array that can be passed to views/javascript
* @param array $personalTools Personal tools list generated by BaseTemplate::getPersonalTools
* @return string|null
*/
public function renderMenuData( array $personalTools ) {
$entries = $this->builder->getGroup( $personalTools )->getEntries();
foreach ( $entries as &$entry ) {
foreach ( $entry['components'] as &$component ) {
$component['class'] .= ' toggle-list-item__anchor--menu';
}
}
$templateParser = new TemplateParser( __DIR__ . '/../../../components' );
return empty( $entries )
? null
: $templateParser->processTemplate( 'ToggleList', [
'class' => 'minerva-user-menu',
'checkboxID' => 'minerva-user-menu-checkbox',
'toggleID' => 'minerva-user-menu-toggle', // See minerva.mustache too.
'toggleClass' => MinervaUI::iconClass(
'page-actions-overflow', 'element', 'wikimedia-ui-' . 'userAvatar' . '-base20'
),
'listClass' => 'minerva-user-menu-list toggle-list__list--drop-down', // See ToggleList/*.less.
'text' => $this->localizer->msg( 'minerva-user-menu-button' )->escaped(),
'items' => $entries
] );
}
}