Use class-static var for storing ContentHandler singletons.
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 14 Jun 2012 10:33:16 +0000 (12:33 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Thu, 14 Jun 2012 10:41:59 +0000 (12:41 +0200)
$wgContentHandlers will be used for configuration only, and
is no longer changed during operation.

It's no longer possible to supply instances directly in $wgContentHandlers,
it only takes class names now.

Change-Id: Ifafb08d8638eff0d6965cd92fadbd9071c74de37

includes/ContentHandler.php

index 486de36..63fa47d 100644 (file)
@@ -247,6 +247,11 @@ abstract class ContentHandler {
                return ContentHandler::getForModelID( $modelId );
        }
 
+       /**
+        * @var Array A Cache of ContentHandler instances by model id
+        */
+       static $handlers;
+
        /**
         * Returns the ContentHandler singleton for the given model ID. Use the
         * CONTENT_MODEL_XXX constants to identify the desired content model.
@@ -276,25 +281,33 @@ abstract class ContentHandler {
        public static function getForModelID( $modelId ) {
                global $wgContentHandlers;
 
+               if ( isset( ContentHandler::$handlers[$modelId] ) ) {
+                       return ContentHandler::$handlers[$modelId];
+               }
+
                if ( empty( $wgContentHandlers[$modelId] ) ) {
                        $handler = null;
 
                        wfRunHooks( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
 
-                       if ( $handler ) { // NOTE: may be a string or an object, either is fine!
-                               $wgContentHandlers[$modelId] = $handler;
-                       } else {
-                               throw new MWException( "No handler for model #$modelId registered " .
-                                       "in \$wgContentHandlers" );
+                       if ( $handler === null ) {
+                               throw new MWException( "No handler for model #$modelId registered in \$wgContentHandlers" );
                        }
-               }
 
-               if ( is_string( $wgContentHandlers[$modelId] ) ) {
+                       if ( !( $handler instanceof ContentHandler ) ) {
+                               throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
+                       }
+               } else {
                        $class = $wgContentHandlers[$modelId];
-                       $wgContentHandlers[$modelId] = new $class( $modelId );
+                       $handler = new $class( $modelId );
+
+                       if ( !( $handler instanceof ContentHandler ) ) {
+                               throw new MWException( "$class from \$wgContentHandlers is not compatible with ContentHandler" );
+                       }
                }
 
-               return $wgContentHandlers[$modelId];
+               ContentHandler::$handlers[$modelId] = $handler;
+               return ContentHandler::$handlers[$modelId];
        }
 
        /**