Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / StubObject.php
blob2dfcdc2f497b45f84718913dfc16ceb02fd84ab5
1 <?php
2 /**
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
20 * @file
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.
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
32 * calling the method.
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.
44 class StubObject {
45 /** @var null|string */
46 protected $global;
48 /** @var null|string */
49 protected $class;
51 /** @var array */
52 protected $params;
54 /**
55 * Constructor.
57 * @param string $global Name of the global variable.
58 * @param string $class Name of the class of the real object.
59 * @param array $params Parameters to pass to constructor of the real object.
61 public function __construct( $global = null, $class = null, $params = array() ) {
62 $this->global = $global;
63 $this->class = $class;
64 $this->params = $params;
67 /**
68 * Returns a bool value whenever $obj is a stub object. Can be used to break
69 * a infinite loop when unstubbing an object.
71 * @param object $obj Object to check.
72 * @return bool True if $obj is not an instance of StubObject class.
74 public static function isRealObject( $obj ) {
75 return is_object( $obj ) && !$obj instanceof StubObject;
78 /**
79 * Unstubs an object, if it is a stub object. Can be used to break a
80 * infinite loop when unstubbing an object or to avoid reference parameter
81 * breakage.
83 * @param object $obj Object to check.
84 * @return void
86 public static function unstub( &$obj ) {
87 if ( $obj instanceof StubObject ) {
88 $obj = $obj->_unstub( 'unstub', 3 );
92 /**
93 * Function called if any function exists with that name in this object.
94 * It is used to unstub the object. Only used internally, PHP will call
95 * self::__call() function and that function will call this function.
96 * This function will also call the function with the same name in the real
97 * object.
99 * @param string $name Name of the function called
100 * @param array $args Arguments
101 * @return mixed
103 public function _call( $name, $args ) {
104 $this->_unstub( $name, 5 );
105 return call_user_func_array( array( $GLOBALS[$this->global], $name ), $args );
109 * Create a new object to replace this stub object.
110 * @return object
112 public function _newObject() {
113 return ObjectFactory::getObjectFromSpec( array(
114 'class' => $this->class,
115 'args' => $this->params,
116 'closure_expansion' => false,
117 ) );
121 * Function called by PHP if no function with that name exists in this
122 * object.
124 * @param string $name Name of the function called
125 * @param array $args Arguments
126 * @return mixed
128 public function __call( $name, $args ) {
129 return $this->_call( $name, $args );
133 * This function creates a new object of the real class and replace it in
134 * the global variable.
135 * This is public, for the convenience of external callers wishing to access
136 * properties, e.g. eval.php
138 * @param string $name Name of the method called in this object.
139 * @param int $level Level to go in the stack trace to get the function
140 * who called this function.
141 * @return object The unstubbed version of itself
142 * @throws MWException
144 public function _unstub( $name = '_unstub', $level = 2 ) {
145 static $recursionLevel = 0;
147 if ( !$GLOBALS[$this->global] instanceof StubObject ) {
148 return $GLOBALS[$this->global]; // already unstubbed.
151 if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
152 $fname = __METHOD__ . '-' . $this->global;
153 $caller = wfGetCaller( $level );
154 if ( ++$recursionLevel > 2 ) {
155 throw new MWException( "Unstub loop detected on call of "
156 . "\${$this->global}->$name from $caller\n" );
158 wfDebug( "Unstubbing \${$this->global} on call of "
159 . "\${$this->global}::$name from $caller\n" );
160 $GLOBALS[$this->global] = $this->_newObject();
161 --$recursionLevel;
162 return $GLOBALS[$this->global];
168 * Stub object for the user language. It depends of the user preferences and
169 * "uselang" parameter that can be passed to index.php. This object have to be
170 * in $wgLang global.
172 class StubUserLang extends StubObject {
174 public function __construct() {
175 parent::__construct( 'wgLang' );
178 public function __call( $name, $args ) {
179 return $this->_call( $name, $args );
183 * Call Language::findVariantLink after unstubbing $wgLang.
185 * This method is implemented with a full signature rather than relying on
186 * __call so that the pass-by-reference signature of the proxied method is
187 * honored.
189 * @param string &$link The name of the link
190 * @param Title &$nt The title object of the link
191 * @param bool $ignoreOtherCond To disable other conditions when
192 * we need to transclude a template or update a category's link
194 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
195 global $wgLang;
196 $this->_unstub( 'findVariantLink', 3 );
197 return $wgLang->findVariantLink( $link, $nt, $ignoreOtherCond );
201 * @return Language
203 public function _newObject() {
204 return RequestContext::getMain()->getLanguage();