Pmiazga has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/332008 )

Change subject: [WIP] Hygiene: Use ServiceWirings instead of statics
......................................................................

[WIP] Hygiene: Use ServiceWirings instead of statics

Instead of calling PopupsContext::getInstance() create proper DI
using ServiceWiring

Change-Id: I4a6a34a80e859f51ee0e2ae8996f6cf0843de85e
---
M Popups.hooks.php
M extension.json
M includes/PopupsContext.php
A includes/ServiceWiring.php
4 files changed, 79 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups 
refs/changes/08/332008/1

diff --git a/Popups.hooks.php b/Popups.hooks.php
index ee9ea97..c6c7735 100644
--- a/Popups.hooks.php
+++ b/Popups.hooks.php
@@ -19,13 +19,15 @@
  * @ingroup extensions
  */
 use Popups\PopupsContext;
+use MediaWiki\MediaWikiServices;
 
 class PopupsHooks {
        const PREVIEWS_PREFERENCES_SECTION = 'rendering/reading';
 
        static function onGetBetaPreferences( User $user, array &$prefs ) {
                global $wgExtensionAssetsPath;
-               if ( PopupsContext::getInstance()->isBetaFeatureEnabled() !== 
true ) {
+
+               if ( self::getContext()->isBetaFeatureEnabled() !== true ) {
                        return;
                }
                $prefs[PopupsContext::PREVIEWS_BETA_PREFERENCE_NAME] = [
@@ -50,7 +52,7 @@
         * @param array $prefs
         */
        static function onGetPreferences( User $user, array &$prefs ) {
-               $context = PopupsContext::getInstance();
+               $context = self::getContext();
 
                if ( !$context->showPreviewsOptInOnPreferencesPage() ) {
                        return;
@@ -84,16 +86,16 @@
        }
 
        public static function onBeforePageDisplay( OutputPage &$out, Skin 
&$skin ) {
-               $module = PopupsContext::getInstance();
+               $context = self::getContext();
 
-               if ( !$module->areDependenciesMet() ) {
-                       $logger = $module->getLogger();
+               if ( !$context->areDependenciesMet() ) {
+                       $logger = $context->getLogger();
                        $logger->error( 'Popups requires the PageImages and 
TextExtracts extensions. '
                                . 'If Beta mode is on it requires also 
BetaFeatures extension' );
                        return true;
                }
 
-               if ( $module->isEnabledByUser( $skin->getUser() ) ) {
+               if ( $context->isEnabledByUser( $skin->getUser() ) ) {
                        $out->addModules( [ 'ext.popups' ] );
                }
                return true;
@@ -140,7 +142,7 @@
         * @param array $vars
         */
        public static function onResourceLoaderGetConfigVars( array &$vars ) {
-               $conf = PopupsContext::getInstance()->getConfig();
+               $conf = self::getContext()->getConfig();
                $vars['wgPopupsSchemaPopupsSamplingRate'] = $conf->get( 
'SchemaPopupsSamplingRate' );
        }
 
@@ -151,7 +153,17 @@
         */
        public static function onUserGetDefaultOptions( &$wgDefaultUserOptions 
) {
                $wgDefaultUserOptions[ 
PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ] =
-                       
PopupsContext::getInstance()->getDefaultIsEnabledState();
+                       self::getContext()->getDefaultIsEnabledState();
+       }
+
+       /**
+        * Helper function to for getting PopupsContext from MediaWikiServices
+        *
+        * @return PopupsContext
+        */
+       private static function getContext() {
+               /** @noinspection PhpIncompatibleReturnTypeInspection */
+               return MediaWikiServices::getInstance()->getService( 
'Popups.Context' );
        }
 
 }
diff --git a/extension.json b/extension.json
index 25d76e5..776c913 100644
--- a/extension.json
+++ b/extension.json
@@ -10,6 +10,7 @@
        "type": "betafeatures",
        "AutoloadClasses": {
                "PopupsHooks": "Popups.hooks.php",
+               "Popups\\ServiceWiring": "includes/ServiceWiring.php",
                "Popups\\PopupsContext": "includes/PopupsContext.php",
                "Popups\\PopupsGadgetsIntegration": 
"includes/PopupsGadgetsIntegration.php"
        },
@@ -34,6 +35,9 @@
                ],
                "UserGetDefaultOptions": [
                        "PopupsHooks::onUserGetDefaultOptions"
+               ],
+               "MediaWikiServices": [
+                       "Popups\\ServiceWiring::register"
                ]
        },
        "MessagesDirs": {
diff --git a/includes/PopupsContext.php b/includes/PopupsContext.php
index f74e02c..4812101 100644
--- a/includes/PopupsContext.php
+++ b/includes/PopupsContext.php
@@ -21,7 +21,6 @@
 namespace Popups;
 
 use MediaWiki\Logger\LoggerFactory;
-use MediaWiki\MediaWikiServices;
 use ExtensionRegistry;
 use Config;
 
@@ -80,31 +79,13 @@
         * @param ExtensionRegistry $extensionRegistry
         * @param PopupsGadgetsIntegration $gadgetsIntegration
         */
-       protected function __construct( Config $config, ExtensionRegistry 
$extensionRegistry,
+       public function __construct( Config $config, ExtensionRegistry 
$extensionRegistry,
                PopupsGadgetsIntegration $gadgetsIntegration ) {
                /** @todo Use MediaWikiServices Service Locator when it's ready 
*/
                $this->extensionRegistry = $extensionRegistry;
                $this->gadgetsIntegration = $gadgetsIntegration;
 
                $this->config = $config;
-       }
-
-       /**
-        * Get a PopupsContext instance
-        *
-        * @return PopupsContext
-        */
-       public static function getInstance() {
-               if ( !self::$instance ) {
-                       /** @todo Use MediaWikiServices Service Locator when 
it's ready */
-
-                       $registry = ExtensionRegistry::getInstance();
-                       $config = 
MediaWikiServices::getInstance()->getConfigFactory()
-                               ->makeConfig( PopupsContext::EXTENSION_NAME );
-                       $gadgetsIntegration = new PopupsGadgetsIntegration( 
$config, $registry );
-                       self::$instance = new PopupsContext( $config, 
$registry, $gadgetsIntegration );
-               }
-               return self::$instance;
        }
 
        /**
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php
new file mode 100644
index 0000000..fe4794c
--- /dev/null
+++ b/includes/ServiceWiring.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace Popups;
+
+use MediaWiki\MediaWikiServices;
+use ExtensionRegistry;
+use Config;
+
+/**
+ * Poge Previews Dependency Injection
+ * Service Wiring definitions
+ *
+ * @package Popups
+ */
+class ServiceWiring {
+
+       const CONFIG = 'Popups.Config';
+       const GADGETS_INTEGRATION = 'Popups.GadgetsIntegration';
+       const CONTEXT = 'Popups.Context';
+
+       /**
+        * MediaWikiServices hook handler.
+        *
+        * For now, loads the <code>ServiceWiring.php</code> service wiring 
file. As we add more
+        * top-level services, that file may need to be split up.
+        *
+        * @param MediaWikiServices $services
+        */
+       public static function register( MediaWikiServices $services ) {
+               $services->applyWiring([
+                       self::CONFIG => function ( MediaWikiServices $services 
) {
+                               return $services->getService( 'ConfigFactory' )
+                                       ->makeConfig( 
PopupsContext::EXTENSION_NAME );
+                       },
+                       self::GADGETS_INTEGRATION => function ( 
MediaWikiServices $services ) {
+                               /**
+                                * @var Config $config
+                                */
+                               $config = $services->getService( self::CONFIG );
+                               return new PopupsGadgetsIntegration ( $config,  
ExtensionRegistry::getInstance() );
+                       },
+                       self::CONTEXT => function ( MediaWikiServices $services 
) {
+                               /**
+                                * @var Config $config
+                                * @var PopupsGadgetsIntegration 
$gadgetsIntegration
+                                */
+                               $config = $services->getService( self::CONFIG );
+                               $gadgetsIntegration = $services->getService( 
self::GADGETS_INTEGRATION);
+                               return new PopupsContext( $config, 
ExtensionRegistry::getInstance(), $gadgetsIntegration );
+                       }
+               ]);
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4a6a34a80e859f51ee0e2ae8996f6cf0843de85e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: mpga
Gerrit-Owner: Pmiazga <pmia...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to