jenkins-bot has submitted this change and it was merged.

Change subject: Remove singleton pattern from TemplateRegistry
......................................................................


Remove singleton pattern from TemplateRegistry

This also defers template registration up until they are actually used.

Change-Id: Ieffe710451bb4645b996df2167a4105a349c0d06
---
M repo/Wikibase.php
A repo/includes/GlobalFunctions.php
M repo/includes/TemplateRegistry.php
M repo/includes/WikibaseRepo.php
M repo/includes/modules/TemplateModule.php
5 files changed, 54 insertions(+), 54 deletions(-)

Approvals:
  Thiemo Mättig (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 4096d67..ed531d3 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -220,43 +220,6 @@
        $wgHooks['BaseTemplateToolbox'][]               = 
'Wikibase\RepoHooks::onBaseTemplateToolbox';
        $wgHooks['SkinTemplateBuildNavUrlsNav_urlsAfterPermalink'][] = 
'Wikibase\RepoHooks::onSkinTemplateBuildNavUrlsNav_urlsAfterPermalink';
 
-       /**
-        * Called when setup is done. This is somewhat ugly, find a better time 
to register templates.
-        * @see https://www.mediawiki.org/wiki/Manual:Hooks/SetupAfterCache
-        *
-        * @return bool
-        */
-       $wgHooks['SetupAfterCache'][] = function() {
-               \Wikibase\TemplateRegistry::singleton()->addTemplates( include( 
__DIR__ . "/resources/templates.php" ) );
-               return true;
-       };
-
-       /**
-        * 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!
-        *
-        * @since 0.2
-        *
-        * @param $key string template key
-        * Varargs: normal template parameters
-        *
-        * @return string
-        */
-       function wfTemplate( $key /*...*/ ) {
-               $params = func_get_args();
-               array_shift( $params );
-
-               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
-                       $params = $params[0];
-               }
-
-               $template = new \Wikibase\Template( 
\Wikibase\TemplateRegistry::singleton(), $key, $params );
-
-               return $template->render();
-       }
-
        // Resource Loader Modules:
        $wgResourceModules = array_merge( $wgResourceModules, include( __DIR__ 
. "/resources/Resources.php" ) );
 
@@ -268,4 +231,6 @@
        if ( defined( 'WB_EXPERIMENTAL_FEATURES' ) && WB_EXPERIMENTAL_FEATURES 
) {
                include_once( __DIR__ . '/config/Wikibase.experimental.php' );
        }
+
+       require __DIR__ . '/includes/GlobalFunctions.php';
 } );
diff --git a/repo/includes/GlobalFunctions.php 
b/repo/includes/GlobalFunctions.php
new file mode 100644
index 0000000..280026c
--- /dev/null
+++ b/repo/includes/GlobalFunctions.php
@@ -0,0 +1,35 @@
+<?php
+
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\Template;
+
+/**
+ * @license GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ */
+
+/**
+ * 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!
+ *
+ * @since 0.2
+ *
+ * @param $key string template key
+ * Varargs: normal template parameters
+ *
+ * @return string
+ */
+function wfTemplate( $key /*...*/ ) {
+       $params = func_get_args();
+       array_shift( $params );
+
+       if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+               $params = $params[0];
+       }
+
+       $template = new Template( 
WikibaseRepo::getDefaultInstance()->getTemplateRegistry(), $key, $params );
+
+       return $template->render();
+}
diff --git a/repo/includes/TemplateRegistry.php 
b/repo/includes/TemplateRegistry.php
index 07285b2..f1afaa9 100644
--- a/repo/includes/TemplateRegistry.php
+++ b/repo/includes/TemplateRegistry.php
@@ -60,19 +60,4 @@
                $this->templates[$key] = str_replace( "\t", '', $snippet );
        }
 
-       /**
-        * Singleton pattern integration.
-        *
-        * @return TemplateRegistry
-        */
-       public static function singleton() {
-               static $instance = false;
-
-               if ( $instance === false ) {
-                       $instance = new static();
-               }
-
-               return $instance;
-       }
-
-}
\ No newline at end of file
+}
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 19d7cf7..4293bc8 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -70,6 +70,7 @@
 use Wikibase\Store;
 use Wikibase\StringNormalizer;
 use Wikibase\SummaryFormatter;
+use Wikibase\TemplateRegistry;
 use Wikibase\Utils;
 use Wikibase\Validators\EntityConstraintProvider;
 use Wikibase\Validators\SnakValidator;
@@ -156,6 +157,11 @@
         * @var Store
         */
        private $store;
+
+       /**
+        * @var TemplateRegistry
+        */
+       private $templateRegistry;
 
        /**
         * Returns the default instance constructed using newInstance().
@@ -961,4 +967,11 @@
                );
        }
 
+       public function getTemplateRegistry() {
+               if( !isset( $this->templateRegistry ) ) {
+                       $this->templateRegistry = new TemplateRegistry();
+                       $this->templateRegistry->addTemplates( include( __DIR__ 
. "/../resources/templates.php" ) );
+               }
+               return $this->templateRegistry;
+       }
 }
diff --git a/repo/includes/modules/TemplateModule.php 
b/repo/includes/modules/TemplateModule.php
index d943768..304a5d2 100644
--- a/repo/includes/modules/TemplateModule.php
+++ b/repo/includes/modules/TemplateModule.php
@@ -3,6 +3,7 @@
 namespace Wikibase;
 use ResourceLoaderContext;
 use ResourceLoaderFileModule;
+use Wikibase\Repo\WikibaseRepo;
 
 /**
  * Injects templates into JavaScript.
@@ -26,7 +27,8 @@
         */
        public function getScript( ResourceLoaderContext $context ) {
                // register HTML templates
-               $templatesJson = \FormatJson::encode( 
TemplateRegistry::singleton()->getTemplates() );
+               $templateRegistry = 
WikibaseRepo::getDefaultInstance()->getTemplateRegistry();
+               $templatesJson = \FormatJson::encode( 
$templateRegistry->getTemplates() );
 
                // template store JavaScript initialisation
                $script = <<<EOT

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ieffe710451bb4645b996df2167a4105a349c0d06
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Adrian Lang <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to