Introduce interface ComponentRegistryContext

- To mitigate passing around Skin god object.
- Provide limited context for components.
- Update relevant classes.

Bug: T301722
Change-Id: Icb7ea7ae5b86a39adf6eb2805b2bf42fb71d3bd0
This commit is contained in:
Clare Ming 2022-03-02 13:51:45 -07:00 committed by Jdlrobson
parent e8142e2356
commit ae6d0f33af
5 changed files with 151 additions and 12 deletions

View file

@ -1090,9 +1090,11 @@ $wgAutoloadLocalClasses = [
'MediaWiki\\Languages\\LanguageNameUtils' => __DIR__ . '/includes/language/LanguageNameUtils.php',
'MediaWiki\\ProcOpenError' => __DIR__ . '/includes/exception/ProcOpenError.php',
'MediaWiki\\ShellDisabledError' => __DIR__ . '/includes/exception/ShellDisabledError.php',
'MediaWiki\\Skin\\ComponentRegistryContext' => __DIR__ . '/includes/skins/components/ComponentRegistryContext.php',
'MediaWiki\\Skin\\SkinComponent' => __DIR__ . '/includes/skins/components/SkinComponent.php',
'MediaWiki\\Skin\\SkinComponentLogo' => __DIR__ . '/includes/skins/components/SkinComponentLogo.php',
'MediaWiki\\Skin\\SkinComponentRegistry' => __DIR__ . '/includes/skins/components/SkinComponentRegistry.php',
'MediaWiki\\Skin\\SkinComponentRegistryContext' => __DIR__ . '/includes/skins/components/SkinComponentRegistryContext.php',
'MediaWiki\\Skin\\SkinComponentSearch' => __DIR__ . '/includes/skins/components/SkinComponentSearch.php',
'MediaWiki\\Skin\\SkinComponentTableOfContents' => __DIR__ . '/includes/skins/components/SkinComponentTableOfContents.php',
'MediaWiki\\Skins\\Hook\\SkinAfterPortletHook' => __DIR__ . '/includes/skins/Hook/SkinAfterPortletHook.php',

View file

@ -27,6 +27,7 @@ use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Skin\SkinComponent;
use MediaWiki\Skin\SkinComponentRegistry;
use MediaWiki\Skin\SkinComponentRegistryContext;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityValue;
use Wikimedia\WrappedStringList;
@ -266,7 +267,7 @@ abstract class Skin extends ContextSource {
$this->skinname = $name;
}
$this->componentRegistry = new SkinComponentRegistry(
$this
new SkinComponentRegistryContext( $this )
);
}

View file

@ -0,0 +1,42 @@
<?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
*/
namespace MediaWiki\Skin;
use Config;
use Title;
/**
* @internal for use inside Skin and SkinTemplate classes only
* @unstable
*/
interface ComponentRegistryContext {
/**
* Returns the config needed for the component.
*
* @return Config
*/
public function getConfig(): Config;
/**
* Returns the Title object for the component.
*
* @return Title
*/
public function getTitle(): Title;
}

View file

@ -19,7 +19,6 @@
namespace MediaWiki\Skin;
use RuntimeException;
use Skin;
use SpecialPage;
/**
@ -30,14 +29,14 @@ class SkinComponentRegistry {
/** @var SkinComponent[]|null null if not initialized. */
private $components = null;
/** @var Skin */
private $skin;
/** @var SkinComponentRegistryContext */
private $skinContext;
/**
* @param Skin $skin
* @param SkinComponentRegistryContext $skinContext
*/
public function __construct( Skin $skin ) {
$this->skin = $skin;
public function __construct( SkinComponentRegistryContext $skinContext ) {
$this->skinContext = $skinContext;
}
/**
@ -82,27 +81,27 @@ class SkinComponentRegistry {
* @throws RuntimeException if given an unknown name
*/
private function registerComponent( string $name ) {
$skin = $this->skin;
$skin = $this->skinContext;
$user = $skin->getUser();
switch ( $name ) {
case 'logos':
$component = new SkinComponentLogo(
$this->skin->getConfig(),
$this->skin->getLanguage()->getCode()
$skin->getConfig(),
$skin->getLanguage()
);
break;
case 'search-box':
$component = new SkinComponentSearch(
$skin->getConfig(),
$user,
$skin->getContext(),
$skin->getMessageLocalizer(),
SpecialPage::newSearchPage( $user ),
$skin->getRelevantTitle()
);
break;
case 'toc':
$component = new SkinComponentTableOfContents(
$this->skin->getOutput()
$skin->getOutput()
);
break;
default:

View file

@ -0,0 +1,95 @@
<?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
*/
namespace MediaWiki\Skin;
use Config;
use MessageLocalizer;
use OutputPage;
use Skin;
use Title;
use User;
/**
* @internal for use inside Skin and SkinTemplate classes only
* @unstable
*/
class SkinComponentRegistryContext implements ComponentRegistryContext {
/** @var Skin */
private $skin;
/** @var MessageLocalizer */
private $localizer;
/**
* @param Skin $skin
*/
public function __construct( Skin $skin ) {
$this->skin = $skin;
$this->localizer = $skin->getContext();
}
/**
* @inheritDoc
*/
public function getConfig(): Config {
return $this->skin->getConfig();
}
/**
* @inheritDoc
*/
public function getTitle(): Title {
return $this->skin->getTitle() ?? Title::makeTitle( NS_MAIN, 'Foo' );
}
/**
* @return Title|null the "relevant" title - see Skin::getRelevantTitle
*/
public function getRelevantTitle() {
return $this->skin->getRelevantTitle() ?? $this->getTitle();
}
/**
* @return OutputPage
*/
public function getOutput(): OutputPage {
return $this->skin->getOutput();
}
/**
* @return User
*/
public function getUser() {
return $this->skin->getUser();
}
/**
* @return string|null $language
*/
public function getLanguage() {
return $this->skin->getLanguage()->getCode();
}
/**
* @return MessageLocalizer
*/
public function getMessageLocalizer(): MessageLocalizer {
return $this->localizer;
}
}