Merge "Fix trailing whitespace (and mixed spaces) in XSD files"
[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 $global String: name of the global variable.
45 * @param $class String: name of the class of the real object.
46 * @param $params Array: parameters to pass to contructor 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 whetever $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 $name String: name of the function called
74 * @param $args Array: 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 $name String: name of the function called
95 * @param $args Array: 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 $name String: name of the method called in this object.
109 * @param $level Integer: level to go in the stact trace to get the function
110 * who called this function.
111 */
112 function _unstub( $name = '_unstub', $level = 2 ) {
113 static $recursionLevel = 0;
114
115 if ( !($GLOBALS[$this->mGlobal] instanceof StubObject) ) {
116 return $GLOBALS[$this->mGlobal]; // already unstubbed.
117 }
118
119 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
120 $fname = __METHOD__.'-'.$this->mGlobal;
121 wfProfileIn( $fname );
122 $caller = wfGetCaller( $level );
123 if ( ++$recursionLevel > 2 ) {
124 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
125 }
126 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
127 $GLOBALS[$this->mGlobal] = $this->_newObject();
128 --$recursionLevel;
129 wfProfileOut( $fname );
130 }
131 }
132 }
133
134 /**
135 * Stub object for the content language of this wiki. This object have to be in
136 * $wgContLang global.
137 *
138 * @deprecated since 1.18
139 */
140 class StubContLang extends StubObject {
141
142 function __construct() {
143 wfDeprecated( __CLASS__, '1.18' );
144 parent::__construct( 'wgContLang' );
145 }
146
147 function __call( $name, $args ) {
148 return $this->_call( $name, $args );
149 }
150
151 /**
152 * @return Language
153 */
154 function _newObject() {
155 global $wgLanguageCode;
156 $obj = Language::factory( $wgLanguageCode );
157 $obj->initEncoding();
158 $obj->initContLang();
159 return $obj;
160 }
161 }
162
163 /**
164 * Stub object for the user language. It depends of the user preferences and
165 * "uselang" parameter that can be passed to index.php. This object have to be
166 * in $wgLang global.
167 */
168 class StubUserLang extends StubObject {
169
170 function __construct() {
171 parent::__construct( 'wgLang' );
172 }
173
174 function __call( $name, $args ) {
175 return $this->_call( $name, $args );
176 }
177
178 /**
179 * @return Language
180 */
181 function _newObject() {
182 return RequestContext::getMain()->getLanguage();
183 }
184 }