From: daniel Date: Thu, 14 Jun 2012 10:33:16 +0000 (+0200) Subject: Use class-static var for storing ContentHandler singletons. X-Git-Tag: 1.31.0-rc.0~22097^2^2~98 X-Git-Url: http://git.cyclocoop.org/%22%20.%20%20%20%24self2%20.%20%20%20%22&var_mode_affiche=boucle?a=commitdiff_plain;h=38828a93a17e63abb85442c90f04f6cac20034ec;p=lhc%2Fweb%2Fwiklou.git 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 --- 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]; } /**