MediaWiki  1.30.0
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.

Note
© 2014 Wikimedia Foundation and contributors

Definition at line 26 of file ObjectFactory.php.

Member Function Documentation

◆ constructClassInstance()

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 129 of file ObjectFactory.php.

References $args, and captcha-old\count.

Referenced by HTMLForm\factory(), ObjectFactoryTest\testConstructClassInstance(), and ObjectFactoryTest\testNamedArgs().

◆ expandClosures()

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 101 of file ObjectFactory.php.

References $value.

◆ getObjectFromSpec()

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 58 of file ObjectFactory.php.

References $args, and as.

Referenced by ReplicatedBagOStuff\__construct(), MultiWriteBagOStuff\__construct(), EtcdConfig\__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(), ObjectFactoryTest\testClosureExpansionEnabled(), ObjectFactoryTest\testGetObjectFromClass(), ObjectFactoryTest\testGetObjectFromFactory(), and ObjectFactoryTest\testGetObjectFromInvalid().


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