MediaWiki  master
StubObject.php
Go to the documentation of this file.
1 <?php
2 
3 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
4 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
5 
26 use Wikimedia\ObjectFactory;
27 
51 class StubObject {
53  protected $global;
54 
56  protected $class;
57 
59  protected $factory;
60 
62  protected $params;
63 
72  public function __construct( $global = null, $class = null, $params = [] ) {
73  $this->global = $global;
74  if ( is_callable( $class ) ) {
75  $this->factory = $class;
76  } else {
77  $this->class = $class;
78  }
79  $this->params = $params;
80  }
81 
89  public static function isRealObject( $obj ) {
90  return is_object( $obj ) && !$obj instanceof self;
91  }
92 
101  public static function unstub( &$obj ) {
102  if ( $obj instanceof self ) {
103  $obj = $obj->_unstub( 'unstub', 3 );
104  }
105  }
106 
118  public function _call( $name, $args ) {
119  $this->_unstub( $name, 5 );
120  return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
121  }
122 
127  public function _newObject() {
128  $params = $this->factory
129  ? [ 'factory' => $this->factory ]
130  : [ 'class' => $this->class ];
131 
132  // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
133  // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
134  return ObjectFactory::getObjectFromSpec( $params + [
135  'args' => $this->params,
136  'closure_expansion' => false,
137  ] );
138  }
139 
148  public function __call( $name, $args ) {
149  return $this->_call( $name, $args );
150  }
151 
164  public function _unstub( $name = '_unstub', $level = 2 ) {
165  static $recursionLevel = 0;
166 
167  if ( !$GLOBALS[$this->global] instanceof self ) {
168  return $GLOBALS[$this->global]; // already unstubbed.
169  }
170 
171  if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
172  $caller = wfGetCaller( $level );
173  if ( ++$recursionLevel > 2 ) {
174  throw new MWException( "Unstub loop detected on call of "
175  . "\${$this->global}->$name from $caller\n" );
176  }
177  wfDebug( "Unstubbing \${$this->global} on call of "
178  . "\${$this->global}::$name from $caller" );
179  $GLOBALS[$this->global] = $this->_newObject();
180  --$recursionLevel;
181  return $GLOBALS[$this->global];
182  }
183  }
184 }
StubObject\$class
null string $class
Definition: StubObject.php:56
StubObject\$global
null string $global
Definition: StubObject.php:53
StubObject
Class to implement stub globals, which are globals that delay loading the their associated module cod...
Definition: StubObject.php:51
StubObject\__call
__call( $name, $args)
Function called by PHP if no function with that name exists in this object.
Definition: StubObject.php:148
MWException
MediaWiki exception.
Definition: MWException.php:29
$args
if( $line===false) $args
Definition: mcc.php:124
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:914
StubObject\_call
_call( $name, $args)
Function called if any function exists with that name in this object.
Definition: StubObject.php:118
StubObject\_newObject
_newObject()
Create a new object to replace this stub object.
Definition: StubObject.php:127
StubObject\__construct
__construct( $global=null, $class=null, $params=[])
Stable to call.
Definition: StubObject.php:72
StubObject\isRealObject
static isRealObject( $obj)
Returns a bool value whenever $obj is a stub object.
Definition: StubObject.php:89
StubObject\$params
array $params
Definition: StubObject.php:62
StubObject\$factory
null callable $factory
Definition: StubObject.php:59
StubObject\_unstub
_unstub( $name='_unstub', $level=2)
This function creates a new object of the real class and replace it in the global variable.
Definition: StubObject.php:164
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:101
wfGetCaller
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Definition: GlobalFunctions.php:1407