MediaWiki  master
StubObject.php
Go to the documentation of this file.
1 <?php
23 
45 class StubObject {
47  protected $global;
48 
50  protected $class;
51 
53  protected $factory;
54 
56  protected $params;
57 
64  public function __construct( $global = null, $class = null, $params = [] ) {
65  $this->global = $global;
66  if ( is_callable( $class ) ) {
67  $this->factory = $class;
68  } else {
69  $this->class = $class;
70  }
71  $this->params = $params;
72  }
73 
81  public static function isRealObject( $obj ) {
82  return is_object( $obj ) && !$obj instanceof self;
83  }
84 
93  public static function unstub( &$obj ) {
94  if ( $obj instanceof self ) {
95  $obj = $obj->_unstub( 'unstub', 3 );
96  }
97  }
98 
110  public function _call( $name, $args ) {
111  $this->_unstub( $name, 5 );
112  return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
113  }
114 
119  public function _newObject() {
120  $params = $this->factory
121  ? [ 'factory' => $this->factory ]
122  : [ 'class' => $this->class ];
123  return ObjectFactory::getObjectFromSpec( $params + [
124  'args' => $this->params,
125  'closure_expansion' => false,
126  ] );
127  }
128 
137  public function __call( $name, $args ) {
138  return $this->_call( $name, $args );
139  }
140 
153  public function _unstub( $name = '_unstub', $level = 2 ) {
154  static $recursionLevel = 0;
155 
156  if ( !$GLOBALS[$this->global] instanceof self ) {
157  return $GLOBALS[$this->global]; // already unstubbed.
158  }
159 
160  if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
161  $caller = wfGetCaller( $level );
162  if ( ++$recursionLevel > 2 ) {
163  throw new MWException( "Unstub loop detected on call of "
164  . "\${$this->global}->$name from $caller\n" );
165  }
166  wfDebug( "Unstubbing \${$this->global} on call of "
167  . "\${$this->global}::$name from $caller\n" );
168  $GLOBALS[$this->global] = $this->_newObject();
169  --$recursionLevel;
170  return $GLOBALS[$this->global];
171  }
172  }
173 }
static isRealObject( $obj)
Returns a bool value whenever $obj is a stub object.
Definition: StubObject.php:81
__call( $name, $args)
Function called by PHP if no function with that name exists in this object.
Definition: StubObject.php:137
array $params
Definition: StubObject.php:56
_newObject()
Create a new object to replace this stub object.
Definition: StubObject.php:119
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
null callable $factory
Definition: StubObject.php:53
if( $line===false) $args
Definition: cdb.php:64
_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:153
__construct( $global=null, $class=null, $params=[])
Definition: StubObject.php:64
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Class to implement stub globals, which are globals that delay loading the their associated module cod...
Definition: StubObject.php:45
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
$GLOBALS['IP']
_call( $name, $args)
Function called if any function exists with that name in this object.
Definition: StubObject.php:110
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:93
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
null string $class
Definition: StubObject.php:50
null string $global
Definition: StubObject.php:47