MediaHandlerFactory: Don't use any global state
authorKunal Mehta <legoktm@member.fsf.org>
Fri, 29 Jul 2016 00:01:08 +0000 (17:01 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Wed, 3 Aug 2016 23:47:46 +0000 (16:47 -0700)
Instead inject the configured MediaHandlers in the service constructor.

Change-Id: I039c01ef531389c74524cb7adcb8cf1229d9a95d

includes/ServiceWiring.php
includes/media/MediaHandlerFactory.php
tests/phpunit/mocks/media/MockMediaHandlerFactory.php

index 438f667..21c6377 100644 (file)
@@ -159,7 +159,9 @@ return [
        },
 
        'MediaHandlerFactory' => function( MediaWikiServices $services ) {
-               return new MediaHandlerFactory();
+               return new MediaHandlerFactory(
+                       $services->getMainConfig()->get( 'MediaHandlers' )
+               );
        },
 
        'LinkCache' => function( MediaWikiServices $services ) {
index 1deecd7..543dc80 100644 (file)
@@ -49,6 +49,11 @@ class MediaHandlerFactory {
                'image/x-djvu' => DjVuHandler::class, // compat
        ];
 
+       /**
+        * @var array
+        */
+       private $registry;
+
        /**
         * Instance cache of MediaHandler objects by mimetype
         *
@@ -56,12 +61,13 @@ class MediaHandlerFactory {
         */
        private $handlers;
 
-       protected function getHandlerClass( $type ) {
-               global $wgMediaHandlers;
+       public function __construct( array $registry ) {
+               $this->registry = $registry + self::$coreHandlers;
+       }
 
-               $registry = $wgMediaHandlers + self::$coreHandlers;
-               if ( isset( $registry[$type] ) ) {
-                       return $registry[$type];
+       protected function getHandlerClass( $type ) {
+               if ( isset( $this->registry[$type] ) ) {
+                       return $this->registry[$type];
                } else {
                        return false;
                }
index 0bcc6eb..54d46b0 100644 (file)
@@ -36,6 +36,10 @@ class MockMediaHandlerFactory extends MediaHandlerFactory {
                'application/ogg' => MockOggHandler::class,
        ];
 
+       public function __construct() {
+               // override parent
+       }
+
        protected function getHandlerClass( $type ) {
                if ( isset( self::$overrides[$type] ) ) {
                        return self::$overrides[$type];