MediaWiki REL1_39
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
26use Wikimedia\ObjectFactory\ObjectFactory;
27
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
158 public function _get( $name ) {
159 $this->_unstub( "__get($name)", 5 );
160 return $GLOBALS[$this->global]->$name;
161 }
162
170 public function __get( $name ) {
171 return $this->_get( $name );
172 }
173
180 public function _set( $name, $value ) {
181 $this->_unstub( "__set($name)", 5 );
182 $GLOBALS[$this->global]->$name = $value;
183 }
184
192 public function __set( $name, $value ) {
193 $this->_set( $name, $value );
194 }
195
208 public function _unstub( $name = '_unstub', $level = 2 ) {
209 static $recursionLevel = 0;
210
211 if ( !$GLOBALS[$this->global] instanceof self ) {
212 return $GLOBALS[$this->global]; // already unstubbed.
213 }
214
215 if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
216 $caller = wfGetCaller( $level );
217 if ( ++$recursionLevel > 2 ) {
218 throw new MWException( "Unstub loop detected on call of "
219 . "\${$this->global}->$name from $caller\n" );
220 }
221 wfDebug( "Unstubbing \${$this->global} on call of "
222 . "\${$this->global}::$name from $caller" );
223 $GLOBALS[$this->global] = $this->_newObject();
224 --$recursionLevel;
225 return $GLOBALS[$this->global];
226 }
227 }
228}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
MediaWiki exception.
Class to implement stub globals, which are globals that delay loading the their associated module cod...
_unstub( $name='_unstub', $level=2)
This function creates a new object of the real class and replace it in the global variable.
_set( $name, $value)
Wrapper for __set(), similar to _call() above.
null string $global
_call( $name, $args)
Function called if any function exists with that name in this object.
_get( $name)
Wrapper for __get(), similar to _call() above.
null callable $factory
array $params
static unstub(&$obj)
Unstubs an object, if it is a stub object.
_newObject()
Create a new object to replace this stub object.
null string $class
__construct( $global=null, $class=null, $params=[])
__call( $name, $args)
Function called by PHP if no function with that name exists in this object.
static isRealObject( $obj)
Returns a bool value whenever $obj is a stub object.
__set( $name, $value)
Function called by PHP if no property with that name exists in this object.
__get( $name)
Function called by PHP if no property with that name exists in this object.
if( $line===false) $args
Definition mcc.php:124