MediaWiki  1.29.1
StubObject.php
Go to the documentation of this file.
1 <?php
44 class StubObject {
46  protected $global;
47 
49  protected $class;
50 
52  protected $factory;
53 
55  protected $params;
56 
65  public function __construct( $global = null, $class = null, $params = [] ) {
66  $this->global = $global;
67  if ( is_callable( $class ) ) {
68  $this->factory = $class;
69  } else {
70  $this->class = $class;
71  }
72  $this->params = $params;
73  }
74 
82  public static function isRealObject( $obj ) {
83  return is_object( $obj ) && !$obj instanceof StubObject;
84  }
85 
94  public static function unstub( &$obj ) {
95  if ( $obj instanceof StubObject ) {
96  $obj = $obj->_unstub( 'unstub', 3 );
97  }
98  }
99 
111  public function _call( $name, $args ) {
112  $this->_unstub( $name, 5 );
113  return call_user_func_array( [ $GLOBALS[$this->global], $name ], $args );
114  }
115 
120  public function _newObject() {
121  $params = $this->factory
122  ? [ 'factory' => $this->factory ]
123  : [ 'class' => $this->class ];
125  'args' => $this->params,
126  'closure_expansion' => false,
127  ] );
128  }
129 
138  public function __call( $name, $args ) {
139  return $this->_call( $name, $args );
140  }
141 
154  public function _unstub( $name = '_unstub', $level = 2 ) {
155  static $recursionLevel = 0;
156 
157  if ( !$GLOBALS[$this->global] instanceof StubObject ) {
158  return $GLOBALS[$this->global]; // already unstubbed.
159  }
160 
161  if ( get_class( $GLOBALS[$this->global] ) != $this->class ) {
162  $caller = wfGetCaller( $level );
163  if ( ++$recursionLevel > 2 ) {
164  throw new MWException( "Unstub loop detected on call of "
165  . "\${$this->global}->$name from $caller\n" );
166  }
167  wfDebug( "Unstubbing \${$this->global} on call of "
168  . "\${$this->global}::$name from $caller\n" );
169  $GLOBALS[$this->global] = $this->_newObject();
170  --$recursionLevel;
171  return $GLOBALS[$this->global];
172  }
173  }
174 }
175 
179 class StubUserLang extends StubObject {
180 
181  public function __construct() {
182  parent::__construct( 'wgLang' );
183  }
184 
197  public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
198  global $wgLang;
199  $this->_unstub( 'findVariantLink', 3 );
200  $wgLang->findVariantLink( $link, $nt, $ignoreOtherCond );
201  }
202 
206  public function _newObject() {
207  return RequestContext::getMain()->getLanguage();
208  }
209 }
StubObject\$class
null string $class
Definition: StubObject.php:49
StubUserLang\__construct
__construct()
Definition: StubObject.php:181
StubObject\$global
null string $global
Definition: StubObject.php:46
StubObject
Class to implement stub globals, which are globals that delay loading the their associated module cod...
Definition: StubObject.php:44
StubObject\__call
__call( $name, $args)
Function called by PHP if no function with that name exists in this object.
Definition: StubObject.php:138
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
php
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
MWException
MediaWiki exception.
Definition: MWException.php:26
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$GLOBALS
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition: Autoload.php:10
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:999
StubUserLang
Stub object for the user language.
Definition: StubObject.php:179
StubObject\_call
_call( $name, $args)
Function called if any function exists with that name in this object.
Definition: StubObject.php:111
ObjectFactory\getObjectFromSpec
static getObjectFromSpec( $spec)
Instantiate an object based on a specification array.
Definition: ObjectFactory.php:59
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
StubObject\_newObject
_newObject()
Create a new object to replace this stub object.
Definition: StubObject.php:120
$args
if( $line===false) $args
Definition: cdb.php:63
StubObject\__construct
__construct( $global=null, $class=null, $params=[])
Constructor.
Definition: StubObject.php:65
StubObject\isRealObject
static isRealObject( $obj)
Returns a bool value whenever $obj is a stub object.
Definition: StubObject.php:82
StubObject\$params
array $params
Definition: StubObject.php:55
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:2929
StubUserLang\_newObject
_newObject()
Definition: StubObject.php:206
StubObject\$factory
null callable $factory
Definition: StubObject.php:52
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:154
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:94
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:1561
array
the array() calling protocol came about after MediaWiki 1.4rc1.
StubUserLang\findVariantLink
findVariantLink(&$link, &$nt, $ignoreOtherCond=false)
Call Language::findVariantLink after unstubbing $wgLang.
Definition: StubObject.php:197