Daniel Kinzler has uploaded a new change for review.

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

Change subject: Introduce top level service locator.
......................................................................

Introduce top level service locator.

The service locator, MediaWikiServices, is intended to faciliate
"manual" dependency injection in static entry points.

The initial implementation has support for the "Config" and "SiteLookup"
services. Many more are expected to be added in the future.

Change-Id: Id745664e25fd230e16f6fad3ba4e0f0e8ecf5b85
---
M autoload.php
A includes/MediaWikiServices.php
M includes/site/SiteSQLStore.php
M maintenance/exportSites.php
M maintenance/importSites.php
A tests/phpunit/includes/MediaWikiServicesTest.php
M tests/phpunit/includes/site/SiteSQLStoreTest.php
7 files changed, 180 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/83/245483/3

diff --git a/autoload.php b/autoload.php
index 731bdaa..138d319 100644
--- a/autoload.php
+++ b/autoload.php
@@ -751,6 +751,7 @@
        'MediaWikiI18N' => __DIR__ . '/includes/skins/MediaWikiI18N.php',
        'MediaWikiPageLinkRenderer' => __DIR__ . 
'/includes/title/MediaWikiPageLinkRenderer.php',
        'MediaWikiSite' => __DIR__ . '/includes/site/MediaWikiSite.php',
+       'MediaWikiServices' => __DIR__ . '/includes/MediaWikiServices.php',
        'MediaWikiTitleCodec' => __DIR__ . 
'/includes/title/MediaWikiTitleCodec.php',
        'MediaWikiVersionFetcher' => __DIR__ . 
'/includes/MediaWikiVersionFetcher.php',
        'MediaWiki\\Logger\\LegacyLogger' => __DIR__ . 
'/includes/debug/logger/LegacyLogger.php',
diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php
new file mode 100644
index 0000000..3606ea3
--- /dev/null
+++ b/includes/MediaWikiServices.php
@@ -0,0 +1,123 @@
+<?php
+
+/**
+ * Service locator for MediaWiki core services.
+ *
+ * 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
+ *
+ * @file
+ *
+ * @since 1.27
+ *
+ *
+ * MediaWikiServices is the service locator for the application scope of 
MediaWiki.
+ * It acts as a top factory/registry for top level services, and defines the 
object
+ * net that defines the services available to MediaWiki's application logic. 
In this
+ * way, it acts as the heart of MediaWiki's dependency injection mechanism.
+ */
+class MediaWikiServices {
+
+       /**
+        * Returns the global default instance of the top level service locator.
+        *
+        * @note This should only be called by static functions! The instance 
returned here
+        * should not be passed around! Objects that need access to a service 
should have
+        * that service injected into the constructor, never a service locator!
+        *
+        * @return MediaWikiServices
+        */
+       public static function getInstance() {
+               static $instance = null;
+
+               if ( $instance === null ) {
+                       $instance = new self( new GlobalVarConfig() );
+               }
+
+               return $instance;
+       }
+
+       /**
+        * @var Config
+        */
+       private $config;
+
+       /**
+        * @var object[]
+        */
+       private $services = array();
+
+       /**
+        * @param Config $config
+        */
+       public function __construct( Config $config ) {
+               $this->config = $config;
+       }
+
+       /**
+        * @return Config
+        */
+       public function getConfig() {
+               return $this->config;
+       }
+
+       /**
+        * @param string $name
+        *
+        * @return object
+        */
+       private function getService( $name ) {
+               if ( !isset( $this->services[$name] ) ) {
+                       $this->services[$name] = $this->createService( $name );
+               }
+
+               return $this->services[$name];
+       }
+
+       /**
+        * @param string $name
+        *
+        * @return object
+        */
+       private function createService( $name ) {
+               $method = "new$name";
+               return $this->$method();
+       }
+
+       /**
+        * @return SiteLookup
+        */
+       public function getSiteLookup() {
+               return $this->getSiteStore();
+       }
+
+       /**
+        * @return SiteStore
+        */
+       public function getSiteStore() {
+               return $this->getService( 'SiteStore' );
+       }
+
+       /**
+        * @note should be called by createService() only!
+        */
+       private function newSiteStore() {
+               $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING 
);
+               $siteStore = new DBSiteStore();
+
+               return new CachingSiteStore( $siteStore, $cache );
+       }
+
+}
diff --git a/includes/site/SiteSQLStore.php b/includes/site/SiteSQLStore.php
index e61179b..2fdfebb 100644
--- a/includes/site/SiteSQLStore.php
+++ b/includes/site/SiteSQLStore.php
@@ -27,32 +27,38 @@
  *
  * @license GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ *
+ * @node SiteSQLStore is a backwards compatibility stub. Despite the name, no 
guarantee is given
+ * regarding the backend used for storing site information.
  */
 class SiteSQLStore extends CachingSiteStore {
 
        /**
+        * Backwards compatibility alias for MediaWikiSites::getSiteStore();
+        *
         * @since 1.21
-        * @deprecated 1.25 Construct a SiteStore instance directly instead.
+        * @deprecated 1.25 Use MediaWikiServices::getSiteStore() instead.
         *
         * @param null $sitesTable Unused
-        * @param BagOStuff|null $cache
+        * @param null $cache
         *
-        * @return SiteStore
+        * @throws InvalidArgumentException if $sitesTable or $cache is not 
null.
+        * @return SiteStore A SiteStore; no guarantee is given regarding the 
storage backend used.
         */
-       public static function newInstance( $sitesTable = null, BagOStuff 
$cache = null ) {
+       public static function newInstance( $sitesTable = null, $cache = null ) 
{
                if ( $sitesTable !== null ) {
                        throw new InvalidArgumentException(
                                __METHOD__ . ': $sitesTable parameter is unused 
and must be null'
                        );
                }
 
-               if ( $cache === null ) {
-                       $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : 
CACHE_ANYTHING );
+               if ( $cache !== null ) {
+                       throw new InvalidArgumentException(
+                               __METHOD__ . ': $cache parameter is unused and 
must be null'
+                       );
                }
 
-               $siteStore = new DBSiteStore();
-
-               return new static( $siteStore, $cache );
+               return MediaWikiServices::getInstance()->getSiteStore();
        }
 
 }
diff --git a/maintenance/exportSites.php b/maintenance/exportSites.php
index 145c924..70e0241 100644
--- a/maintenance/exportSites.php
+++ b/maintenance/exportSites.php
@@ -42,7 +42,7 @@
 
                $exporter = new SiteExporter( $handle );
 
-               $sites = SiteSQLStore::newInstance()->getSites( 'recache' );
+               $sites = 
MediaWikiServices::getInstance()->getSiteStore()->getSites( 'recache' );
                $exporter->exportSites( $sites );
 
                fclose( $handle );
diff --git a/maintenance/importSites.php b/maintenance/importSites.php
index 7cd2000..ee31fbf 100644
--- a/maintenance/importSites.php
+++ b/maintenance/importSites.php
@@ -31,7 +31,8 @@
        public function execute() {
                $file = $this->getArg( 0 );
 
-               $importer = new SiteImporter( SiteSQLStore::newInstance() );
+               $siteStore = MediaWikiServices::getInstance()->getSiteStore();
+               $importer = new SiteImporter( $siteStore );
                $importer->setExceptionCallback( array( $this, 
'reportException' ) );
 
                $importer->importFromFile( $file );
diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php 
b/tests/phpunit/includes/MediaWikiServicesTest.php
new file mode 100644
index 0000000..a776920
--- /dev/null
+++ b/tests/phpunit/includes/MediaWikiServicesTest.php
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * @covers MediaWikiServices
+ *
+ * @group MediaWiki
+ */
+class MediaWikiServicesTest extends PHPUnit_Framework_TestCase {
+
+       /**
+        * @param string $expectedType expected class
+        * @param string $getter method name
+        */
+       private function assertGetterReturnType( $expectedType, $getter ) {
+               $locator = MediaWikiServices::getInstance();
+               $service = $locator->$getter();
+               $this->assertInstanceOf( $expectedType, $service );
+       }
+
+       public function testGetInstance() {
+               $locator = MediaWikiServices::getInstance();
+               $this->assertInstanceOf( 'MediaWikiServices', $locator );
+       }
+
+       public function testGetConfig() {
+               $this->assertGetterReturnType( 'Config', 'getConfig' );
+       }
+
+       public function testGetSiteStore() {
+               $this->assertGetterReturnType( 'SiteStore', 'getSiteStore' );
+       }
+
+       public function testGetSiteLookup() {
+               $this->assertGetterReturnType( 'SiteLookup', 'getSiteLookup' );
+       }
+
+}
diff --git a/tests/phpunit/includes/site/SiteSQLStoreTest.php 
b/tests/phpunit/includes/site/SiteSQLStoreTest.php
index 6908800..6960e51 100644
--- a/tests/phpunit/includes/site/SiteSQLStoreTest.php
+++ b/tests/phpunit/includes/site/SiteSQLStoreTest.php
@@ -34,7 +34,7 @@
         */
        public function testNewInstance() {
                $siteStore = SiteSQLStore::newInstance();
-               $this->assertInstanceOf( 'SiteSQLStore', $siteStore );
+               $this->assertInstanceOf( 'SiteStore', $siteStore );
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id745664e25fd230e16f6fad3ba4e0f0e8ecf5b85
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to