MediaWiki  REL1_21
StubObject.php
Go to the documentation of this file.
00001 <?php
00038 class StubObject {
00039         var $mGlobal, $mClass, $mParams;
00040 
00049         function __construct( $global = null, $class = null, $params = array() ) {
00050                 $this->mGlobal = $global;
00051                 $this->mClass = $class;
00052                 $this->mParams = $params;
00053         }
00054 
00062         static function isRealObject( $obj ) {
00063                 return is_object( $obj ) && !($obj instanceof StubObject);
00064         }
00065 
00077         function _call( $name, $args ) {
00078                 $this->_unstub( $name, 5 );
00079                 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
00080         }
00081 
00086         function _newObject() {
00087                 return MWFunction::newObj( $this->mClass, $this->mParams );
00088         }
00089 
00098         function __call( $name, $args ) {
00099                 return $this->_call( $name, $args );
00100         }
00101 
00113         function _unstub( $name = '_unstub', $level = 2 ) {
00114                 static $recursionLevel = 0;
00115 
00116                 if ( !($GLOBALS[$this->mGlobal] instanceof StubObject) ) {
00117                         return $GLOBALS[$this->mGlobal]; // already unstubbed.
00118                 }
00119 
00120                 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
00121                         $fname = __METHOD__ . '-' . $this->mGlobal;
00122                         wfProfileIn( $fname );
00123                         $caller = wfGetCaller( $level );
00124                         if ( ++$recursionLevel > 2 ) {
00125                                 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
00126                         }
00127                         wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
00128                         $GLOBALS[$this->mGlobal] = $this->_newObject();
00129                         --$recursionLevel;
00130                         wfProfileOut( $fname );
00131                 }
00132         }
00133 }
00134 
00141 class StubContLang extends StubObject {
00142 
00143         function __construct() {
00144                 wfDeprecated( __CLASS__, '1.18' );
00145                 parent::__construct( 'wgContLang' );
00146         }
00147 
00148         function __call( $name, $args ) {
00149                 return $this->_call( $name, $args );
00150         }
00151 
00155         function _newObject() {
00156                 global $wgLanguageCode;
00157                 $obj = Language::factory( $wgLanguageCode );
00158                 $obj->initEncoding();
00159                 $obj->initContLang();
00160                 return $obj;
00161         }
00162 }
00163 
00169 class StubUserLang extends StubObject {
00170 
00171         function __construct() {
00172                 parent::__construct( 'wgLang' );
00173         }
00174 
00175         function __call( $name, $args ) {
00176                 return $this->_call( $name, $args );
00177         }
00178 
00182         function _newObject() {
00183                 return RequestContext::getMain()->getLanguage();
00184         }
00185 }