MediaWiki  1.27.4
ObjectFactory Class Reference

Construct objects from configuration instructions. More...

Static Public Member Functions

static constructClassInstance ($clazz, $args)
 Construct an instance of the given class using the given arguments. More...
 
static getObjectFromSpec ($spec)
 Instantiate an object based on a specification array. More...
 

Static Protected Member Functions

static expandClosures ($list)
 Iterate a list and call any closures it contains. More...
 

Detailed Description

Construct objects from configuration instructions.

Author
Bryan Davis bd808.nosp@m.@wik.nosp@m.imedi.nosp@m.a.or.nosp@m.g
Note
© 2014 Bryan Davis and Wikimedia Foundation.

Definition at line 27 of file ObjectFactory.php.

Member Function Documentation

static ObjectFactory::constructClassInstance (   $clazz,
  $args 
)
static

Construct an instance of the given class using the given arguments.

PHP's call_user_func_array() doesn't work with object construction so we have to use other measures. Starting with PHP 5.6.0 we could use the "splat" operator (...) to unpack the array into an argument list. Sadly there is no way to conditionally include a syntax construct like a new operator in a way that allows older versions of PHP to still parse the file. Instead, we will try a loop unrolling technique that works for 0-10 arguments. If we are passed 11 or more arguments we will take the performance penalty of using ReflectionClass::newInstanceArgs() to construct the desired object.

Parameters
string$clazzClass name
array$argsConstructor arguments
Returns
mixed Constructed instance

Definition at line 130 of file ObjectFactory.php.

References $args.

Referenced by ObjectFactoryTest\testConstructClassInstance(), ObjectFactoryTest\testNamedArgs(), and MessageTest\testRawHtmlInMsg().

static ObjectFactory::expandClosures (   $list)
staticprotected

Iterate a list and call any closures it contains.

Parameters
array$listList of things
Returns
array List with any Closures replaced with their output

Definition at line 102 of file ObjectFactory.php.

References $value.

static ObjectFactory::getObjectFromSpec (   $spec)
static

Instantiate an object based on a specification array.

The specification array must contain a 'class' key with string value that specifies the class name to instantiate or a 'factory' key with a callable (is_callable() === true). It can optionally contain an 'args' key that provides arguments to pass to the constructor/callable.

Values in the arguments collection which are Closure instances will be expanded by invoking them with no arguments before passing the resulting value on to the constructor/callable. This can be used to pass IDatabase instances or other live objects to the constructor/callable. This behavior can be suppressed by adding closure_expansion => false to the specification.

The specification may also contain a 'calls' key that describes method calls to make on the newly created object before returning it. This pattern is often known as "setter injection". The value of this key is expected to be an associative array with method names as keys and argument lists as values. The argument list will be expanded (or not) in the same way as the 'args' key for the main object.

Parameters
array$specObject specification
Returns
object
Exceptions
InvalidArgumentExceptionwhen object specification does not contain 'class' or 'factory' keys
ReflectionExceptionwhen 'args' are supplied and 'class' constructor is non-public or non-existent

Definition at line 59 of file ObjectFactory.php.

References $args, and as.

Referenced by ReplicatedBagOStuff\__construct(), MultiWriteBagOStuff\__construct(), StubObject\_newObject(), CentralIdLookup\factory(), MediaWiki\Logger\MonologSpi\getFormatter(), MediaWiki\Logger\MonologSpi\getHandler(), SpecialPageFactory\getPage(), MediaWiki\Logger\MonologSpi\getProcessor(), MediaWiki\Logger\LoggerFactory\getProvider(), MediaWiki\Session\SessionManager\getProviders(), MediaWiki\Auth\AuthManager\providerArrayFromSpecs(), ObjectFactoryTest\testClosureExpansionDisabled(), and ObjectFactoryTest\testClosureExpansionEnabled().


The documentation for this class was generated from the following file: