* Use local context to get messages
[lhc/web/wiklou.git] / includes / StubObject.php
1 <?php
2
3 /**
4 * Class to implement stub globals, which are globals that delay loading the
5 * their associated module code by deferring initialisation until the first
6 * method call.
7 *
8 * Note on unstub loops:
9 *
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.
17 */
18 class StubObject {
19 var $mGlobal, $mClass, $mParams;
20
21 /**
22 * Constructor.
23 *
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
27 * object.
28 */
29 function __construct( $global = null, $class = null, $params = array() ) {
30 $this->mGlobal = $global;
31 $this->mClass = $class;
32 $this->mParams = $params;
33 }
34
35 /**
36 * Returns a bool value whetever $obj is a stub object. Can be used to break
37 * a infinite loop when unstubbing an object.
38 *
39 * @param $obj Object to check.
40 * @return Boolean: true if $obj is not an instance of StubObject class.
41 */
42 static function isRealObject( $obj ) {
43 return is_object( $obj ) && !($obj instanceof StubObject);
44 }
45
46 /**
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
51 * object.
52 *
53 * @param $name String: name of the function called
54 * @param $args Array: arguments
55 * @return mixed
56 */
57 function _call( $name, $args ) {
58 $this->_unstub( $name, 5 );
59 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
60 }
61
62 /**
63 * Create a new object to replace this stub object.
64 * @return object
65 */
66 function _newObject() {
67 return MWFunction::newObj( $this->mClass, $this->mParams );
68 }
69
70 /**
71 * Function called by PHP if no function with that name exists in this
72 * object.
73 *
74 * @param $name String: name of the function called
75 * @param $args Array: arguments
76 * @return mixed
77 */
78 function __call( $name, $args ) {
79 return $this->_call( $name, $args );
80 }
81
82 /**
83 * This function creates a new object of the real class and replace it in
84 * the global variable.
85 * This is public, for the convenience of external callers wishing to access
86 * properties, e.g. eval.php
87 *
88 * @param $name String: name of the method called in this object.
89 * @param $level Integer: level to go in the stact trace to get the function
90 * who called this function.
91 */
92 function _unstub( $name = '_unstub', $level = 2 ) {
93 static $recursionLevel = 0;
94
95 if ( !($GLOBALS[$this->mGlobal] instanceof StubObject) ) {
96 return $GLOBALS[$this->mGlobal]; // already unstubbed.
97 }
98
99 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
100 $fname = __METHOD__.'-'.$this->mGlobal;
101 wfProfileIn( $fname );
102 $caller = wfGetCaller( $level );
103 if ( ++$recursionLevel > 2 ) {
104 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
105 }
106 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
107 $GLOBALS[$this->mGlobal] = $this->_newObject();
108 --$recursionLevel;
109 wfProfileOut( $fname );
110 }
111 }
112 }
113
114 /**
115 * Stub object for the content language of this wiki. This object have to be in
116 * $wgContLang global.
117 *
118 * @deprecated since 1.18
119 */
120 class StubContLang extends StubObject {
121
122 function __construct() {
123 wfDeprecated( __CLASS__, '1.18' );
124 parent::__construct( 'wgContLang' );
125 }
126
127 function __call( $name, $args ) {
128 return $this->_call( $name, $args );
129 }
130
131 /**
132 * @return Language
133 */
134 function _newObject() {
135 global $wgLanguageCode;
136 $obj = Language::factory( $wgLanguageCode );
137 $obj->initEncoding();
138 $obj->initContLang();
139 return $obj;
140 }
141 }
142
143 /**
144 * Stub object for the user language. It depends of the user preferences and
145 * "uselang" parameter that can be passed to index.php. This object have to be
146 * in $wgLang global.
147 */
148 class StubUserLang extends StubObject {
149
150 function __construct() {
151 parent::__construct( 'wgLang' );
152 }
153
154 function __call( $name, $args ) {
155 return $this->_call( $name, $args );
156 }
157
158 /**
159 * @return Language
160 */
161 function _newObject() {
162 return RequestContext::getMain()->getLanguage();
163 }
164 }