Output more MW version info in update.php
[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 use Wikimedia\ObjectFactory;
23
24 /**
25 * Class to implement stub globals, which are globals that delay loading the
26 * their associated module code by deferring initialisation until the first
27 * method call.
28 *
29 * Note on reference parameters:
30 *
31 * If the called method takes any parameters by reference, the __call magic
32 * here won't work correctly. The solution is to unstub the object before
33 * calling the method.
34 *
35 * Note on unstub loops:
36 *
37 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
38 * another function, and the other function calls some method of the stub. The
39 * best way to avoid this is to make constructors as lightweight as possible,
40 * deferring any initialisation which depends on other modules. As a last
41 * resort, you can use StubObject::isRealObject() to break the loop, but as a
42 * general rule, the stub object mechanism should be transparent, and code
43 * which refers to it should be kept to a minimum.
44 */
45 class StubObject {
46 /** @var null|string */
47 protected $global;
48
49 /** @var null|string */
50 protected $class;
51
52 /** @var null|callable */
53 protected $factory;
54
55 /** @var array */
56 protected $params;
57
58 /**
59 * @param string|null $global Name of the global variable.
60 * @param string|callable|null $class Name of the class of the real object
61 * or a factory function to call
62 * @param array $params Parameters to pass to constructor of the real object.
63 */
64 public function __construct( $global = null, $class = null, $params = [] ) {
65 $this->global = $global;
66 if ( is_callable( $class ) ) {
67 $this->factory = $class;
68 } else {
69 $this->class = $class;
70 }
71 $this->params = $params;
72 }
73
74 /**
75 * Returns a bool value whenever $obj is a stub object. Can be used to break
76 * a infinite loop when unstubbing an object.
77 *
78 * @param object $obj Object to check.
79 * @return bool True if $obj is not an instance of StubObject class.
80 */
81 public static function isRealObject( $obj ) {
82 return is_object( $obj ) && !$obj instanceof self;
83 }
84
85 /**
86 * Unstubs an object, if it is a stub object. Can be used to break a
87 * infinite loop when unstubbing an object or to avoid reference parameter
88 * breakage.
89 *
90 * @param object &$obj Object to check.
91 * @return void
92 */
93 public static function unstub( &$obj ) {
94 if ( $obj instanceof self ) {
95 $obj = $obj->_unstub( 'unstub', 3 );
96 }
97 }
98
99 /**
100 * Function called if any function exists with that name in this object.
101 * It is used to unstub the object. Only used internally, PHP will call
102 * self::__call() function and that function will call this function.
103 * This function will also call the function with the same name in the real
104 * object.
105 *
106 * @param string $name Name of the function called
107 * @param array $args Arguments
108 * @return mixed
109 */
110 public function _call( $name, $args ) {
111 $this->_unstub( $name, 5 );
112 return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
113 }
114
115 /**
116 * Create a new object to replace this stub object.
117 * @return object
118 */
119 public function _newObject() {
120 $params = $this->factory
121 ? [ 'factory' => $this->factory ]
122 : [ 'class' => $this->class ];
123 return ObjectFactory::getObjectFromSpec( $params + [
124 'args' => $this->params,
125 'closure_expansion' => false,
126 ] );
127 }
128
129 /**
130 * Function called by PHP if no function with that name exists in this
131 * object.
132 *
133 * @param string $name Name of the function called
134 * @param array $args Arguments
135 * @return mixed
136 */
137 public function __call( $name, $args ) {
138 return $this->_call( $name, $args );
139 }
140
141 /**
142 * This function creates a new object of the real class and replace it in
143 * the global variable.
144 * This is public, for the convenience of external callers wishing to access
145 * properties, e.g. eval.php
146 *
147 * @param string $name Name of the method called in this object.
148 * @param int $level Level to go in the stack trace to get the function
149 * who called this function.
150 * @return object The unstubbed version of itself
151 * @throws MWException
152 */
153 public function _unstub( $name = '_unstub', $level = 2 ) {
154 static $recursionLevel = 0;
155
156 if ( !$GLOBALS[$this->global] instanceof self ) {
157 return $GLOBALS[$this->global]; // already unstubbed.
158 }
159
160 if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
161 $caller = wfGetCaller( $level );
162 if ( ++$recursionLevel > 2 ) {
163 throw new MWException( "Unstub loop detected on call of "
164 . "\${$this->global}->$name from $caller\n" );
165 }
166 wfDebug( "Unstubbing \${$this->global} on call of "
167 . "\${$this->global}::$name from $caller\n" );
168 $GLOBALS[$this->global] = $this->_newObject();
169 --$recursionLevel;
170 return $GLOBALS[$this->global];
171 }
172 }
173 }