MediaWiki  REL1_22
StubObject.php
Go to the documentation of this file.
00001 <?php
00044 class StubObject {
00045     var $mGlobal, $mClass, $mParams;
00046 
00055     function __construct( $global = null, $class = null, $params = array() ) {
00056         $this->mGlobal = $global;
00057         $this->mClass = $class;
00058         $this->mParams = $params;
00059     }
00060 
00068     static function isRealObject( $obj ) {
00069         return is_object( $obj ) && !$obj instanceof StubObject;
00070     }
00071 
00080     static function unstub( $obj ) {
00081         if ( $obj instanceof StubObject ) {
00082             $obj->_unstub( 'unstub', 3 );
00083         }
00084     }
00085 
00097     function _call( $name, $args ) {
00098         $this->_unstub( $name, 5 );
00099         return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
00100     }
00101 
00106     function _newObject() {
00107         return MWFunction::newObj( $this->mClass, $this->mParams );
00108     }
00109 
00118     function __call( $name, $args ) {
00119         return $this->_call( $name, $args );
00120     }
00121 
00133     function _unstub( $name = '_unstub', $level = 2 ) {
00134         static $recursionLevel = 0;
00135 
00136         if ( !$GLOBALS[$this->mGlobal] instanceof StubObject ) {
00137             return $GLOBALS[$this->mGlobal]; // already unstubbed.
00138         }
00139 
00140         if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
00141             $fname = __METHOD__ . '-' . $this->mGlobal;
00142             wfProfileIn( $fname );
00143             $caller = wfGetCaller( $level );
00144             if ( ++$recursionLevel > 2 ) {
00145                 wfProfileOut( $fname );
00146                 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
00147             }
00148             wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
00149             $GLOBALS[$this->mGlobal] = $this->_newObject();
00150             --$recursionLevel;
00151             wfProfileOut( $fname );
00152         }
00153     }
00154 }
00155 
00162 class StubContLang extends StubObject {
00163 
00164     function __construct() {
00165         wfDeprecated( __CLASS__, '1.18' );
00166         parent::__construct( 'wgContLang' );
00167     }
00168 
00169     function __call( $name, $args ) {
00170         return $this->_call( $name, $args );
00171     }
00172 
00176     function _newObject() {
00177         global $wgLanguageCode;
00178         $obj = Language::factory( $wgLanguageCode );
00179         $obj->initEncoding();
00180         $obj->initContLang();
00181         return $obj;
00182     }
00183 }
00184 
00190 class StubUserLang extends StubObject {
00191 
00192     function __construct() {
00193         parent::__construct( 'wgLang' );
00194     }
00195 
00196     function __call( $name, $args ) {
00197         return $this->_call( $name, $args );
00198     }
00199 
00203     function _newObject() {
00204         return RequestContext::getMain()->getLanguage();
00205     }
00206 }