Merge "Update referenced PHP version in 'normal' README."
[lhc/web/wiklou.git] / includes / StubObject.php
1 <?php
2 /**
3 * Delayed loading of global objects.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Class to implement stub globals, which are globals that delay loading the
25 * their associated module code by deferring initialisation until the first
26 * method call.
27 *
28 * Note on unstub loops:
29 *
30 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
31 * another function, and the other function calls some method of the stub. The
32 * best way to avoid this is to make constructors as lightweight as possible,
33 * deferring any initialisation which depends on other modules. As a last
34 * resort, you can use StubObject::isRealObject() to break the loop, but as a
35 * general rule, the stub object mechanism should be transparent, and code
36 * which refers to it should be kept to a minimum.
37 */
38 class StubObject {
39 var $mGlobal, $mClass, $mParams;
40
41 /**
42 * Constructor.
43 *
44 * @param string $global name of the global variable.
45 * @param string $class name of the class of the real object.
46 * @param array $params parameters to pass to constructor of the real
47 * object.
48 */
49 function __construct( $global = null, $class = null, $params = array() ) {
50 $this->mGlobal = $global;
51 $this->mClass = $class;
52 $this->mParams = $params;
53 }
54
55 /**
56 * Returns a bool value whenever $obj is a stub object. Can be used to break
57 * a infinite loop when unstubbing an object.
58 *
59 * @param $obj Object to check.
60 * @return Boolean: true if $obj is not an instance of StubObject class.
61 */
62 static function isRealObject( $obj ) {
63 return is_object( $obj ) && !$obj instanceof StubObject;
64 }
65
66 /**
67 * Function called if any function exists with that name in this object.
68 * It is used to unstub the object. Only used internally, PHP will call
69 * self::__call() function and that function will call this function.
70 * This function will also call the function with the same name in the real
71 * object.
72 *
73 * @param string $name name of the function called
74 * @param array $args arguments
75 * @return mixed
76 */
77 function _call( $name, $args ) {
78 $this->_unstub( $name, 5 );
79 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
80 }
81
82 /**
83 * Create a new object to replace this stub object.
84 * @return object
85 */
86 function _newObject() {
87 return MWFunction::newObj( $this->mClass, $this->mParams );
88 }
89
90 /**
91 * Function called by PHP if no function with that name exists in this
92 * object.
93 *
94 * @param string $name name of the function called
95 * @param array $args arguments
96 * @return mixed
97 */
98 function __call( $name, $args ) {
99 return $this->_call( $name, $args );
100 }
101
102 /**
103 * This function creates a new object of the real class and replace it in
104 * the global variable.
105 * This is public, for the convenience of external callers wishing to access
106 * properties, e.g. eval.php
107 *
108 * @param string $name name of the method called in this object.
109 * @param $level Integer: level to go in the stack trace to get the function
110 * who called this function.
111 * @throws MWException
112 */
113 function _unstub( $name = '_unstub', $level = 2 ) {
114 static $recursionLevel = 0;
115
116 if ( !$GLOBALS[$this->mGlobal] instanceof StubObject ) {
117 return $GLOBALS[$this->mGlobal]; // already unstubbed.
118 }
119
120 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
121 $fname = __METHOD__ . '-' . $this->mGlobal;
122 wfProfileIn( $fname );
123 $caller = wfGetCaller( $level );
124 if ( ++$recursionLevel > 2 ) {
125 wfProfileOut( $fname );
126 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
127 }
128 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
129 $GLOBALS[$this->mGlobal] = $this->_newObject();
130 --$recursionLevel;
131 wfProfileOut( $fname );
132 }
133 }
134 }
135
136 /**
137 * Stub object for the content language of this wiki. This object have to be in
138 * $wgContLang global.
139 *
140 * @deprecated since 1.18
141 */
142 class StubContLang extends StubObject {
143
144 function __construct() {
145 wfDeprecated( __CLASS__, '1.18' );
146 parent::__construct( 'wgContLang' );
147 }
148
149 function __call( $name, $args ) {
150 return $this->_call( $name, $args );
151 }
152
153 /**
154 * @return Language
155 */
156 function _newObject() {
157 global $wgLanguageCode;
158 $obj = Language::factory( $wgLanguageCode );
159 $obj->initEncoding();
160 $obj->initContLang();
161 return $obj;
162 }
163 }
164
165 /**
166 * Stub object for the user language. It depends of the user preferences and
167 * "uselang" parameter that can be passed to index.php. This object have to be
168 * in $wgLang global.
169 */
170 class StubUserLang extends StubObject {
171
172 function __construct() {
173 parent::__construct( 'wgLang' );
174 }
175
176 function __call( $name, $args ) {
177 return $this->_call( $name, $args );
178 }
179
180 /**
181 * @return Language
182 */
183 function _newObject() {
184 return RequestContext::getMain()->getLanguage();
185 }
186 }