jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/347337 )
Change subject: Remove subclassing and unused code from LexemeTemplateFactory
......................................................................
Remove subclassing and unused code from LexemeTemplateFactory
See discussion at Ie743670. This patch removes the subclassing that was
there for code reuse only. I strongly believe that the amount of reused
code just does not justify the disadvantages this approach comes with:
This introduces a ton of unused code, and blocks us from doing more
trivial refactorings on the base class.
This patch also turns both TemplateRegistry as well as Template classes
into private implementation details.
This patch also makes all tests independent from the actual templates.php
file.
Bug: T161789
Change-Id: I745a53ad077d0cc5e482ed4eaf19597682393bef
---
M src/View/LexemeViewFactory.php
M src/View/Template/LexemeTemplateFactory.php
M tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
M tests/phpunit/mediawiki/View/Template/LexemeTemplateFactoryTest.php
4 files changed, 40 insertions(+), 32 deletions(-)
Approvals:
WMDE-leszek: Looks good to me, approved
Aleksey Bekh-Ivanov (WMDE): Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/src/View/LexemeViewFactory.php b/src/View/LexemeViewFactory.php
index 5e012d8..204ab47 100644
--- a/src/View/LexemeViewFactory.php
+++ b/src/View/LexemeViewFactory.php
@@ -67,12 +67,13 @@
}
public function newLexemeView() {
+ $templates = include __DIR__ . '/../../resources/templates.php';
$languageDirectionalityLookup = new
MediaWikiLanguageDirectionalityLookup();
$localizedTextProvider = new MediaWikiLocalizedTextProvider(
$this->languageCode );
$formsView = new LexemeFormsView(
$localizedTextProvider,
- LexemeTemplateFactory::getDefaultInstance()
+ new LexemeTemplateFactory( $templates )
);
$sensesView = new SensesView( $localizedTextProvider );
diff --git a/src/View/Template/LexemeTemplateFactory.php
b/src/View/Template/LexemeTemplateFactory.php
index 6ed9da5..1c6e8ac 100644
--- a/src/View/Template/LexemeTemplateFactory.php
+++ b/src/View/Template/LexemeTemplateFactory.php
@@ -2,28 +2,50 @@
namespace Wikibase\Lexeme\View\Template;
-use Wikibase\View\Template\TemplateFactory;
+use Wikibase\View\Template\Template;
use Wikibase\View\Template\TemplateRegistry;
/**
* @license GPL-2.0+
* @author Amir Sarabadani <[email protected]>
+ * @author Thiemo Mättig
*/
-class LexemeTemplateFactory extends TemplateFactory {
+class LexemeTemplateFactory {
/**
- * @var self
+ * @var TemplateRegistry
*/
- private static $instance;
+ private $templateRegistry;
- public static function getDefaultInstance() {
- if ( self::$instance === null ) {
- self::$instance = new self(
- new TemplateRegistry( include __DIR__ .
'/../../../resources/templates.php' )
- );
+ /**
+ * @param string[] $templates
+ */
+ public function __construct( array $templates ) {
+ $this->templateRegistry = new TemplateRegistry( $templates );
+ }
+
+ /**
+ * Shorthand function to retrieve a template filled with the specified
parameters.
+ *
+ * important! note that the Template class does not escape anything.
+ * be sure to escape your params before using this function!
+ *
+ * @param string $key template key
+ * Varargs: normal template parameters
+ *
+ * @return string
+ */
+ public function render( $key /*...*/ ) {
+ $params = func_get_args();
+ array_shift( $params );
+
+ if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+ $params = $params[0];
}
- return self::$instance;
+ $template = new Template( $this->templateRegistry, $key,
$params );
+
+ return $template->render();
}
}
diff --git a/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
b/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
index c8c951b..86e9ca8 100644
--- a/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
+++ b/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
@@ -56,7 +56,7 @@
assertThat(
$html,
is( htmlPiece( havingChild( both( tagMatchingOutline(
- '<h3
class="wikibase-lexeme-form-representation" lang="some language">'
+ '<h3 lang="some language">'
) )->andAlso( havingTextContents( 'FORM_REPRESENTATION
(FORM_ID)' ) ) ) ) )
);
}
@@ -64,7 +64,10 @@
private function newFormsView() {
return new LexemeFormsView(
new DummyLocalizedTextProvider(),
- LexemeTemplateFactory::getDefaultInstance()
+ new LexemeTemplateFactory( [
+ 'wikibase-lexeme-form' => '<h3 lang="$1">$2
$3</h3>',
+ 'wikibase-lexeme-form-id' => '$1',
+ ] )
);
}
diff --git
a/tests/phpunit/mediawiki/View/Template/LexemeTemplateFactoryTest.php
b/tests/phpunit/mediawiki/View/Template/LexemeTemplateFactoryTest.php
index aa988a0..e5bd95e 100644
--- a/tests/phpunit/mediawiki/View/Template/LexemeTemplateFactoryTest.php
+++ b/tests/phpunit/mediawiki/View/Template/LexemeTemplateFactoryTest.php
@@ -4,7 +4,6 @@
use PHPUnit_Framework_TestCase;
use Wikibase\Lexeme\View\Template\LexemeTemplateFactory;
-use Wikibase\View\Template\TemplateRegistry;
/**
* @covers Wikibase\Lexeme\View\Template\LexemeTemplateFactory
@@ -19,24 +18,7 @@
class TemplateFactoryTest extends PHPUnit_Framework_TestCase {
private function newInstance() {
- return new LexemeTemplateFactory( new TemplateRegistry( [
'basic' => '$1' ] ) );
- }
-
- public function testGetDefaultInstance() {
- $instance = LexemeTemplateFactory::getDefaultInstance();
- $this->assertInstanceOf( LexemeTemplateFactory::class,
$instance );
- }
-
- public function testGetTemplates() {
- $templates = $this->newInstance()->getTemplates();
- $this->assertSame( [ 'basic' => '$1' ], $templates );
- }
-
- public function testGet() {
- $template = $this->newInstance()->get( 'basic', [ '<PARAM>' ] );
- $this->assertSame( 'basic', $template->getKey() );
- $this->assertSame( [ '<PARAM>' ], $template->getParams() );
- $this->assertSame( '<PARAM>', $template->plain() );
+ return new LexemeTemplateFactory( [ 'basic' => '$1' ] );
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/347337
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I745a53ad077d0cc5e482ed4eaf19597682393bef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
Gerrit-Reviewer: Jakob <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits