From 38828a93a17e63abb85442c90f04f6cac20034ec Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 14 Jun 2012 12:33:16 +0200 Subject: [PATCH] Use class-static var for storing ContentHandler singletons. $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 | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/includes/ContentHandler.php b/includes/ContentHandler.php index 486de365c6..63fa47df48 100644 --- a/includes/ContentHandler.php +++ b/includes/ContentHandler.php @@ -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]; } /** -- 2.20.1