4 * Class to implement stub globals, which are globals that delay loading the
5 * their associated module code by deferring initialisation until the first
8 * Note on unstub loops:
10 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
11 * another function, and the other function calls some method of the stub. The
12 * best way to avoid this is to make constructors as lightweight as possible,
13 * deferring any initialisation which depends on other modules. As a last
14 * resort, you can use StubObject::isRealObject() to break the loop, but as a
15 * general rule, the stub object mechanism should be transparent, and code
16 * which refers to it should be kept to a minimum.
19 var $mGlobal, $mClass, $mParams;
24 * @param $global String: name of the global variable.
25 * @param $class String: name of the class of the real object.
26 * @param $params Array: parameters to pass to contructor of the real
29 function __construct( $global = null, $class = null, $params = array() ) {
30 $this->mGlobal
= $global;
31 $this->mClass
= $class;
32 $this->mParams
= $params;
36 * Returns a bool value whetever $obj is a stub object. Can be used to break
37 * a infinite loop when unstubbing an object.
39 * @param $obj Object to check.
40 * @return Boolean: true if $obj is not an instance of StubObject class.
42 static function isRealObject( $obj ) {
43 return is_object( $obj ) && !($obj instanceof StubObject
);
47 * Function called if any function exists with that name in this object.
48 * It is used to unstub the object. Only used internally, PHP will call
49 * self::__call() function and that function will call this function.
50 * This function will also call the function with the same name in the real
53 * @param $name String: name of the function called
54 * @param $args Array: arguments
56 function _call( $name, $args ) {
57 $this->_unstub( $name, 5 );
58 return call_user_func_array( array( $GLOBALS[$this->mGlobal
], $name ), $args );
62 * Create a new object to replace this stub object.
64 function _newObject() {
65 return MWFunction
::newObj( $this->mClass
, $this->mParams
);
69 * Function called by PHP if no function with that name exists in this
72 * @param $name String: name of the function called
73 * @param $args Array: arguments
75 function __call( $name, $args ) {
76 return $this->_call( $name, $args );
80 * This function creates a new object of the real class and replace it in
81 * the global variable.
82 * This is public, for the convenience of external callers wishing to access
83 * properties, e.g. eval.php
85 * @param $name String: name of the method called in this object.
86 * @param $level Integer: level to go in the stact trace to get the function
87 * who called this function.
89 function _unstub( $name = '_unstub', $level = 2 ) {
90 static $recursionLevel = 0;
92 if ( !($GLOBALS[$this->mGlobal
] instanceof StubObject
) )
93 return $GLOBALS[$this->mGlobal
]; // already unstubbed.
95 if ( get_class( $GLOBALS[$this->mGlobal
] ) != $this->mClass
) {
96 $fname = __METHOD__
.'-'.$this->mGlobal
;
97 wfProfileIn( $fname );
98 $caller = wfGetCaller( $level );
99 if ( ++
$recursionLevel > 2 ) {
100 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
102 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
103 $GLOBALS[$this->mGlobal
] = $this->_newObject();
105 wfProfileOut( $fname );
111 * Stub object for the content language of this wiki. This object have to be in
112 * $wgContLang global.
114 * @deprecated since 1.18
116 class StubContLang
extends StubObject
{
118 function __construct() {
119 parent
::__construct( 'wgContLang' );
122 function __call( $name, $args ) {
123 return $this->_call( $name, $args );
126 function _newObject() {
127 global $wgLanguageCode;
128 $obj = Language
::factory( $wgLanguageCode );
129 $obj->initEncoding();
130 $obj->initContLang();
136 * Stub object for the user language. It depends of the user preferences and
137 * "uselang" parameter that can be passed to index.php. This object have to be
140 class StubUserLang
extends StubObject
{
142 function __construct() {
143 parent
::__construct( 'wgLang' );
146 function __call( $name, $args ) {
147 return $this->_call( $name, $args );
150 function _newObject() {
151 return RequestContext
::getMain()->lang
;