Mwjames has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/71250


Change subject: \SMW\ParserFunctionFactory
......................................................................

\SMW\ParserFunctionFactory

Shield against public exposure of dependency injection allowing simple
factoring such as

\SMW\ParserFunctionFactory::newFromParser( $parser )->getSubobjectParser();

Change-Id: I7fe6bac11f56cb5d5fa2be9a07f388c6770dbd58
---
M includes/Setup.php
A includes/parserhooks/ParserFunctionFactory.php
A tests/phpunit/includes/parserhooks/ParserFunctionFactoryTest.php
3 files changed, 192 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/50/71250/1

diff --git a/includes/Setup.php b/includes/Setup.php
index 405a8ce..347a1ca 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -274,6 +274,7 @@
        $wgAutoloadClasses['SMW\SubobjectParserFunction']       = $phDir . 
'SubobjectParserFunction.php';
        $wgAutoloadClasses['SMW\RecurringEventsParserFunction'] = $phDir . 
'RecurringEventsParserFunction.php';
        $wgAutoloadClasses['SMW\DocumentationParserFunction']   = $phDir . 
'DocumentationParserFunction.php';
+       $wgAutoloadClasses['SMW\ParserFunctionFactory']         = $phDir . 
'ParserFunctionFactory.php';
 
        // Query related classes
        $qeDir = $smwgIP . 'includes/query/';
diff --git a/includes/parserhooks/ParserFunctionFactory.php 
b/includes/parserhooks/ParserFunctionFactory.php
new file mode 100644
index 0000000..c8d76da
--- /dev/null
+++ b/includes/parserhooks/ParserFunctionFactory.php
@@ -0,0 +1,96 @@
+<?php
+
+namespace SMW;
+
+use Parser;
+
+/**
+ * Factory class for convenience parser function instantiation
+ *
+ * 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
+ *
+ *
+ * @since 1.9
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @author mwjames
+ */
+
+/**
+ * Factory class for convenience parser function instantiation
+ *
+ * @ingroup ParserFunction
+ */
+class ParserFunctionFactory {
+
+       /** @var Parser */
+       protected $parser;
+
+       /**
+        * @since 1.9
+        *
+        * @param Parser $parser
+        */
+       public function __construct( Parser $parser ) {
+               $this->parser = $parser;
+       }
+
+       /**
+        * Convenience instantiation of a ParserFunctionFactory object
+        *
+        * @since 1.9
+        *
+        * @param Parser $parser
+        *
+        * @return ParserFunctionFactory
+        */
+       public static function newFromParser( Parser $parser ) {
+               return new self( $parser );
+       }
+
+       /**
+        * Convenience instantiation of a SubobjectParserFunction object
+        *
+        * @since 1.9
+        *
+        * @return SubobjectParserFunction
+        */
+       public function getSubobjectParser() {
+               return new SubobjectParserFunction(
+                       new ParserData( $this->parser->getTitle(), 
$this->parser->getOutput() ),
+                       new Subobject( $this->parser->getTitle() ),
+                       new MessageFormatter( 
$this->parser->getTargetLanguage() )
+               );
+       }
+
+       /**
+        * Convenience instantiation of a RecurringEventsParserFunction object
+        *
+        * @since 1.9
+        *
+        * @return RecurringEventsParserFunction
+        */
+       public function getRecurringEventsParser() {
+               return new RecurringEventsParserFunction(
+                       new ParserData( $this->parser->getTitle(), 
$this->parser->getOutput() ),
+                       new Subobject( $this->parser->getTitle() ),
+                       new MessageFormatter( 
$this->parser->getTargetLanguage() ),
+                       Settings::newFromGlobals()
+               );
+       }
+}
diff --git a/tests/phpunit/includes/parserhooks/ParserFunctionFactoryTest.php 
b/tests/phpunit/includes/parserhooks/ParserFunctionFactoryTest.php
new file mode 100644
index 0000000..5c7fde5
--- /dev/null
+++ b/tests/phpunit/includes/parserhooks/ParserFunctionFactoryTest.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\ParserFunctionFactory;
+
+use WikiPage;
+use Parser;
+
+/**
+ * Tests for the ParserFunctionFactory class
+ *
+ * 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
+ *
+ * @since 1.9
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @author mwjames
+ */
+
+/**
+ * @covers \SMW\ParserFunctionFactory
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ */
+class ParserFunctionFactoryTest extends ParserTestCase {
+
+       /**
+        * Returns the name of the class to be tested
+        *
+        * @return string
+        */
+       public function getClass() {
+               return '\SMW\ParserFunctionFactory';
+       }
+
+       /**
+        * Helper method that returns a ParserFunctionFactory object
+        *
+        * @since 1.9
+        *
+        * @param Title $title
+        * @param ParserOutput $parserOutput
+        *
+        * @return ParserFunctionFactory
+        */
+       private function getInstance() {
+               return ParserFunctionFactory::newFromParser( $this->getParser( 
$this->getTitle(), $this->getUser() ) );
+       }
+
+       /**
+        * @test ParserFunctionFactory::__construct
+        *
+        * @since 1.9
+        */
+       public function testConstructor() {
+               $this->assertInstanceOf( $this->getClass(), 
$this->getInstance() );
+       }
+
+       /**
+        * @test ParserFunctionFactory::getSubobjectParser
+        *
+        * @since 1.9
+        */
+       public function testGetSubobjectParser() {
+               $this->assertInstanceOf( '\SMW\SubobjectParserFunction', 
$this->getInstance()->getSubobjectParser() );
+       }
+
+       /**
+        * @test ParserFunctionFactory::getRecurringEventsParser
+        *
+        * @since 1.9
+        */
+       public function testGetRecurringEventsParser() {
+               $this->assertInstanceOf( '\SMW\RecurringEventsParserFunction', 
$this->getInstance()->getRecurringEventsParser() );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/71250
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7fe6bac11f56cb5d5fa2be9a07f388c6770dbd58
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to