StubObject.php 6.4 KB

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