From 20bd328eb0f7d1418f9801be07307fd4ab279ca7 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 28 Jul 2016 17:01:08 -0700 Subject: [PATCH] MediaHandlerFactory: Don't use any global state Instead inject the configured MediaHandlers in the service constructor. Change-Id: I039c01ef531389c74524cb7adcb8cf1229d9a95d --- includes/ServiceWiring.php | 4 +++- includes/media/MediaHandlerFactory.php | 16 +++++++++++----- .../mocks/media/MockMediaHandlerFactory.php | 4 ++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 438f667c37..21c6377320 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -159,7 +159,9 @@ return [ }, 'MediaHandlerFactory' => function( MediaWikiServices $services ) { - return new MediaHandlerFactory(); + return new MediaHandlerFactory( + $services->getMainConfig()->get( 'MediaHandlers' ) + ); }, 'LinkCache' => function( MediaWikiServices $services ) { diff --git a/includes/media/MediaHandlerFactory.php b/includes/media/MediaHandlerFactory.php index 1deecd7b09..543dc80dfd 100644 --- a/includes/media/MediaHandlerFactory.php +++ b/includes/media/MediaHandlerFactory.php @@ -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; } diff --git a/tests/phpunit/mocks/media/MockMediaHandlerFactory.php b/tests/phpunit/mocks/media/MockMediaHandlerFactory.php index 0bcc6eba45..54d46b0271 100644 --- a/tests/phpunit/mocks/media/MockMediaHandlerFactory.php +++ b/tests/phpunit/mocks/media/MockMediaHandlerFactory.php @@ -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]; -- 2.20.1