MediaWiki  master
StubObject.php
Go to the documentation of this file.
1 <?php
22 use Wikimedia\ObjectFactory;
23 
47 class StubObject {
49  protected $global;
50 
52  protected $class;
53 
55  protected $factory;
56 
58  protected $params;
59 
68  public function __construct( $global = null, $class = null, $params = [] ) {
69  $this->global = $global;
70  if ( is_callable( $class ) ) {
71  $this->factory = $class;
72  } else {
73  $this->class = $class;
74  }
75  $this->params = $params;
76  }
77 
85  public static function isRealObject( $obj ) {
86  return is_object( $obj ) && !$obj instanceof self;
87  }
88 
97  public static function unstub( &$obj ) {
98  if ( $obj instanceof self ) {
99  $obj = $obj->_unstub( 'unstub', 3 );
100  }
101  }
102 
114  public function _call( $name, $args ) {
115  $this->_unstub( $name, 5 );
116  return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
117  }
118 
123  public function _newObject() {
124  $params = $this->factory
125  ? [ 'factory' => $this->factory ]
126  : [ 'class' => $this->class ];
127  return ObjectFactory::getObjectFromSpec( $params + [
128  'args' => $this->params,
129  'closure_expansion' => false,
130  ] );
131  }
132 
141  public function __call( $name, $args ) {
142  return $this->_call( $name, $args );
143  }
144 
157  public function _unstub( $name = '_unstub', $level = 2 ) {
158  static $recursionLevel = 0;
159 
160  if ( !$GLOBALS[$this->global] instanceof self ) {
161  return $GLOBALS[$this->global]; // already unstubbed.
162  }
163 
164  if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
165  $caller = wfGetCaller( $level );
166  if ( ++$recursionLevel > 2 ) {
167  throw new MWException( "Unstub loop detected on call of "
168  . "\${$this->global}->$name from $caller\n" );
169  }
170  wfDebug( "Unstubbing \${$this->global} on call of "
171  . "\${$this->global}::$name from $caller" );
172  $GLOBALS[$this->global] = $this->_newObject();
173  --$recursionLevel;
174  return $GLOBALS[$this->global];
175  }
176  }
177 }
StubObject\$class
null string $class
Definition: StubObject.php:52
StubObject\$global
null string $global
Definition: StubObject.php:49
StubObject
Class to implement stub globals, which are globals that delay loading the their associated module cod...
Definition: StubObject.php:47
StubObject\__call
__call( $name, $args)
Function called by PHP if no function with that name exists in this object.
Definition: StubObject.php:141
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:909
StubObject\_call
_call( $name, $args)
Function called if any function exists with that name in this object.
Definition: StubObject.php:114
StubObject\_newObject
_newObject()
Create a new object to replace this stub object.
Definition: StubObject.php:123
StubObject\__construct
__construct( $global=null, $class=null, $params=[])
Stable to call.
Definition: StubObject.php:68
StubObject\isRealObject
static isRealObject( $obj)
Returns a bool value whenever $obj is a stub object.
Definition: StubObject.php:85
StubObject\$params
array $params
Definition: StubObject.php:58
StubObject\$factory
null callable $factory
Definition: StubObject.php:55
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:157
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:97
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:1390
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6