MediaWiki REL1_41
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
27namespace MediaWiki\StubObject;
28
29use MWException;
30use Wikimedia\ObjectFactory\ObjectFactory;
31
57 protected $global;
58
60 protected $class;
61
63 protected $factory;
64
66 protected $params;
67
76 public function __construct( $global = null, $class = null, $params = [] ) {
77 $this->global = $global;
78 if ( is_callable( $class ) ) {
79 $this->factory = $class;
80 } else {
81 $this->class = $class;
82 }
83 $this->params = $params;
84 }
85
93 public static function isRealObject( $obj ) {
94 return is_object( $obj ) && !$obj instanceof self;
95 }
96
105 public static function unstub( &$obj ) {
106 if ( $obj instanceof self ) {
107 $obj = $obj->_unstub( 'unstub', 3 );
108 }
109 }
110
122 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
123 public function _call( $name, $args ) {
124 $this->_unstub( $name, 5 );
125 return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
126 }
127
132 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
133 public function _newObject() {
134 $params = $this->factory
135 ? [ 'factory' => $this->factory ]
136 : [ 'class' => $this->class ];
137
138 // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
139 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
140 return ObjectFactory::getObjectFromSpec( $params + [
141 'args' => $this->params,
142 'closure_expansion' => false,
143 ] );
144 }
145
154 public function __call( $name, $args ) {
155 return $this->_call( $name, $args );
156 }
157
164 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
165 public function _get( $name ) {
166 $this->_unstub( "__get($name)", 5 );
167 return $GLOBALS[$this->global]->$name;
168 }
169
177 public function __get( $name ) {
178 return $this->_get( $name );
179 }
180
187 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
188 public function _set( $name, $value ) {
189 $this->_unstub( "__set($name)", 5 );
190 $GLOBALS[$this->global]->$name = $value;
191 }
192
200 public function __set( $name, $value ) {
201 $this->_set( $name, $value );
202 }
203
216 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
217 public function _unstub( $name = '_unstub', $level = 2 ) {
218 static $recursionLevel = 0;
219
220 if ( !$GLOBALS[$this->global] instanceof self ) {
221 return $GLOBALS[$this->global]; // already unstubbed.
222 }
223
224 if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
225 $caller = wfGetCaller( $level );
226 if ( ++$recursionLevel > 2 ) {
227 throw new MWException( "Unstub loop detected on call of "
228 . "\${$this->global}->$name from $caller\n" );
229 }
230 wfDebug( "Unstubbing \${$this->global} on call of "
231 . "\${$this->global}::$name from $caller" );
232 $GLOBALS[$this->global] = $this->_newObject();
233 --$recursionLevel;
234 return $GLOBALS[$this->global];
235 }
236 }
237}
238
242class_alias( StubObject::class, 'StubObject' );
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...
_set( $name, $value)
Wrapper for __set(), similar to _call() above.
_get( $name)
Wrapper for __get(), similar to _call() above.
__get( $name)
Function called by PHP if no property with that name exists in this object.
_unstub( $name='_unstub', $level=2)
This function creates a new object of the real class and replace it in the global variable.
_newObject()
Create a new object to replace this stub object.
__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.
static unstub(&$obj)
Unstubs an object, if it is a stub object.
_call( $name, $args)
Function called if any function exists with that name in this object.
__set( $name, $value)
Function called by PHP if no property with that name exists in this object.
__construct( $global=null, $class=null, $params=[])