3 * Delayed loading of global objects.
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.
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.
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
24 * Class to implement stub globals, which are globals that delay loading the
25 * their associated module code by deferring initialisation until the first
28 * Note on reference parameters:
30 * If the called method takes any parameters by reference, the __call magic
31 * here won't work correctly. The solution is to unstub the object before
34 * Note on unstub loops:
36 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
37 * another function, and the other function calls some method of the stub. The
38 * best way to avoid this is to make constructors as lightweight as possible,
39 * deferring any initialisation which depends on other modules. As a last
40 * resort, you can use StubObject::isRealObject() to break the loop, but as a
41 * general rule, the stub object mechanism should be transparent, and code
42 * which refers to it should be kept to a minimum.
45 /** @var null|string */
48 /** @var null|string */
51 /** @var null|callable */
60 * @param string $global Name of the global variable.
61 * @param string|callable $class Name of the class of the real object
62 * or a factory function to call
63 * @param array $params Parameters to pass to constructor of the real object.
65 public function __construct( $global = null, $class = null, $params = [] ) {
66 $this->global = $global;
67 if ( is_callable( $class ) ) {
68 $this->factory
= $class;
70 $this->class = $class;
72 $this->params
= $params;
76 * Returns a bool value whenever $obj is a stub object. Can be used to break
77 * a infinite loop when unstubbing an object.
79 * @param object $obj Object to check.
80 * @return bool True if $obj is not an instance of StubObject class.
82 public static function isRealObject( $obj ) {
83 return is_object( $obj ) && !$obj instanceof StubObject
;
87 * Unstubs an object, if it is a stub object. Can be used to break a
88 * infinite loop when unstubbing an object or to avoid reference parameter
91 * @param object $obj Object to check.
94 public static function unstub( &$obj ) {
95 if ( $obj instanceof StubObject
) {
96 $obj = $obj->_unstub( 'unstub', 3 );
101 * Function called if any function exists with that name in this object.
102 * It is used to unstub the object. Only used internally, PHP will call
103 * self::__call() function and that function will call this function.
104 * This function will also call the function with the same name in the real
107 * @param string $name Name of the function called
108 * @param array $args Arguments
111 public function _call( $name, $args ) {
112 $this->_unstub( $name, 5 );
113 return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
117 * Create a new object to replace this stub object.
120 public function _newObject() {
121 $params = $this->factory
122 ?
[ 'factory' => $this->factory
]
123 : [ 'class' => $this->class ];
124 return ObjectFactory
::getObjectFromSpec( $params +
[
125 'args' => $this->params
,
126 'closure_expansion' => false,
131 * Function called by PHP if no function with that name exists in this
134 * @param string $name Name of the function called
135 * @param array $args Arguments
138 public function __call( $name, $args ) {
139 return $this->_call( $name, $args );
143 * This function creates a new object of the real class and replace it in
144 * the global variable.
145 * This is public, for the convenience of external callers wishing to access
146 * properties, e.g. eval.php
148 * @param string $name Name of the method called in this object.
149 * @param int $level Level to go in the stack trace to get the function
150 * who called this function.
151 * @return object The unstubbed version of itself
152 * @throws MWException
154 public function _unstub( $name = '_unstub', $level = 2 ) {
155 static $recursionLevel = 0;
157 if ( !$GLOBALS[$this->global] instanceof StubObject
) {
158 return $GLOBALS[$this->global]; // already unstubbed.
161 if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
162 $caller = wfGetCaller( $level );
163 if ( ++
$recursionLevel > 2 ) {
164 throw new MWException( "Unstub loop detected on call of "
165 . "\${$this->global}->$name from $caller\n" );
167 wfDebug( "Unstubbing \${$this->global} on call of "
168 . "\${$this->global}::$name from $caller\n" );
169 $GLOBALS[$this->global] = $this->_newObject();
171 return $GLOBALS[$this->global];
177 * Stub object for the user language. Assigned to the $wgLang global.
179 class StubUserLang
extends StubObject
{
181 public function __construct() {
182 parent
::__construct( 'wgLang' );
186 * Call Language::findVariantLink after unstubbing $wgLang.
188 * This method is implemented with a full signature rather than relying on
189 * __call so that the pass-by-reference signature of the proxied method is
192 * @param string &$link The name of the link
193 * @param Title &$nt The title object of the link
194 * @param bool $ignoreOtherCond To disable other conditions when
195 * we need to transclude a template or update a category's link
197 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
199 $this->_unstub( 'findVariantLink', 3 );
200 $wgLang->findVariantLink( $link, $nt, $ignoreOtherCond );
206 public function _newObject() {
207 return RequestContext
::getMain()->getLanguage();