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// phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
6
14namespace MediaWiki\StubObject;
15
16use LogicException;
17use Wikimedia\ObjectFactory\ObjectFactory;
18
44 protected $global;
45
47 protected $class;
48
50 protected $factory;
51
53 protected $params;
54
63 public function __construct( $global = null, $class = null, $params = [] ) {
64 $this->global = $global;
65 if ( is_callable( $class ) ) {
66 $this->factory = $class;
67 } else {
68 $this->class = $class;
69 }
70 $this->params = $params;
71 }
72
80 public static function isRealObject( $obj ) {
81 return is_object( $obj ) && !$obj instanceof self;
82 }
83
92 public static function unstub( &$obj ) {
93 if ( $obj instanceof self ) {
94 $obj = $obj->_unstub( 'unstub', 3 );
95 }
96 }
97
109 public function _call( $name, $args ) {
110 $this->_unstub( $name, 5 );
111 return $GLOBALS[$this->global]->$name( ...$args );
112 }
113
118 public function _newObject() {
119 $params = $this->factory
120 ? [ 'factory' => $this->factory ]
121 : [ 'class' => $this->class ];
122
123 // ObjectFactory::getObjectFromSpec accepts an array, not just a callable (phan bug)
124 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
125 return ObjectFactory::getObjectFromSpec( $params + [
126 'args' => $this->params,
127 'closure_expansion' => false,
128 ] );
129 }
130
139 public function __call( $name, $args ) {
140 return $this->_call( $name, $args );
141 }
142
149 public function _get( $name ) {
150 $this->_unstub( "__get($name)", 5 );
151 return $GLOBALS[$this->global]->$name;
152 }
153
161 public function __get( $name ) {
162 return $this->_get( $name );
163 }
164
171 public function _set( $name, $value ) {
172 $this->_unstub( "__set($name)", 5 );
173 $GLOBALS[$this->global]->$name = $value;
174 }
175
183 public function __set( $name, $value ) {
184 $this->_set( $name, $value );
185 }
186
198 public function _unstub( $name = '_unstub', $level = 2 ) {
199 static $recursionLevel = 0;
200
201 if ( !$GLOBALS[$this->global] instanceof self ) {
202 return $GLOBALS[$this->global]; // already unstubbed.
203 }
204
205 if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
206 $caller = wfGetCaller( $level );
207 if ( ++$recursionLevel > 2 ) {
208 throw new LogicException( "Unstub loop detected on call of "
209 . "\${$this->global}->$name from $caller\n" );
210 }
211 wfDebug( "Unstubbing \${$this->global} on call of "
212 . "\${$this->global}::$name from $caller" );
213 $GLOBALS[$this->global] = $this->_newObject();
214 --$recursionLevel;
215 return $GLOBALS[$this->global];
216 }
217 }
218}
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...
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=[])