Use class-static var for storing ContentHandler singletons.
[lhc/web/wiklou.git] / 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];
        }
 
        /**