StubObject.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * 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 reference parameters:
  29. *
  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.
  33. *
  34. * Note on unstub loops:
  35. *
  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.
  43. */
  44. class StubObject {
  45. /** @var null|string */
  46. protected $global;
  47. /** @var null|string */
  48. protected $class;
  49. /** @var null|callable */
  50. protected $factory;
  51. /** @var array */
  52. protected $params;
  53. /**
  54. * @param string|null $global Name of the global variable.
  55. * @param string|callable|null $class Name of the class of the real object
  56. * or a factory function to call
  57. * @param array $params Parameters to pass to constructor of the real object.
  58. */
  59. public function __construct( $global = null, $class = null, $params = [] ) {
  60. $this->global = $global;
  61. if ( is_callable( $class ) ) {
  62. $this->factory = $class;
  63. } else {
  64. $this->class = $class;
  65. }
  66. $this->params = $params;
  67. }
  68. /**
  69. * Returns a bool value whenever $obj is a stub object. Can be used to break
  70. * a infinite loop when unstubbing an object.
  71. *
  72. * @param object $obj Object to check.
  73. * @return bool True if $obj is not an instance of StubObject class.
  74. */
  75. public static function isRealObject( $obj ) {
  76. return is_object( $obj ) && !$obj instanceof self;
  77. }
  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.
  82. *
  83. * @param object &$obj Object to check.
  84. * @return void
  85. */
  86. public static function unstub( &$obj ) {
  87. if ( $obj instanceof self ) {
  88. $obj = $obj->_unstub( 'unstub', 3 );
  89. }
  90. }
  91. /**
  92. * Function called if any function exists with that name in this object.
  93. * It is used to unstub the object. Only used internally, PHP will call
  94. * self::__call() function and that function will call this function.
  95. * This function will also call the function with the same name in the real
  96. * object.
  97. *
  98. * @param string $name Name of the function called
  99. * @param array $args Arguments
  100. * @return mixed
  101. */
  102. public function _call( $name, $args ) {
  103. $this->_unstub( $name, 5 );
  104. return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
  105. }
  106. /**
  107. * Create a new object to replace this stub object.
  108. * @return object
  109. */
  110. public function _newObject() {
  111. $params = $this->factory
  112. ? [ 'factory' => $this->factory ]
  113. : [ 'class' => $this->class ];
  114. return ObjectFactory::getObjectFromSpec( $params + [
  115. 'args' => $this->params,
  116. 'closure_expansion' => false,
  117. ] );
  118. }
  119. /**
  120. * Function called by PHP if no function with that name exists in this
  121. * object.
  122. *
  123. * @param string $name Name of the function called
  124. * @param array $args Arguments
  125. * @return mixed
  126. */
  127. public function __call( $name, $args ) {
  128. return $this->_call( $name, $args );
  129. }
  130. /**
  131. * This function creates a new object of the real class and replace it in
  132. * the global variable.
  133. * This is public, for the convenience of external callers wishing to access
  134. * properties, e.g. eval.php
  135. *
  136. * @param string $name Name of the method called in this object.
  137. * @param int $level Level to go in the stack trace to get the function
  138. * who called this function.
  139. * @return object The unstubbed version of itself
  140. * @throws MWException
  141. */
  142. public function _unstub( $name = '_unstub', $level = 2 ) {
  143. static $recursionLevel = 0;
  144. if ( !$GLOBALS[$this->global] instanceof self ) {
  145. return $GLOBALS[$this->global]; // already unstubbed.
  146. }
  147. if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
  148. $caller = wfGetCaller( $level );
  149. if ( ++$recursionLevel > 2 ) {
  150. throw new MWException( "Unstub loop detected on call of "
  151. . "\${$this->global}->$name from $caller\n" );
  152. }
  153. wfDebug( "Unstubbing \${$this->global} on call of "
  154. . "\${$this->global}::$name from $caller\n" );
  155. $GLOBALS[$this->global] = $this->_newObject();
  156. --$recursionLevel;
  157. return $GLOBALS[$this->global];
  158. }
  159. }
  160. }