MediaWiki
1.28.0
|
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... | |
Construct objects from configuration instructions.
Definition at line 27 of file ObjectFactory.php.
|
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.
string | $clazz | Class name |
array | $args | Constructor arguments |
Definition at line 130 of file ObjectFactory.php.
References $args.
Referenced by HTMLForm\factory(), ObjectFactoryTest\testConstructClassInstance(), and ObjectFactoryTest\testNamedArgs().
|
staticprotected |
Iterate a list and call any closures it contains.
array | $list | List of things |
Definition at line 102 of file ObjectFactory.php.
References $value.
|
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.
array | $spec | Object specification |
InvalidArgumentException | when object specification does not contain 'class' or 'factory' keys |
ReflectionException | when 'args' are supplied and 'class' constructor is non-public or non-existent |
Definition at line 59 of file ObjectFactory.php.
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(), ObjectFactoryTest\testClosureExpansionEnabled(), ObjectFactoryTest\testGetObjectFromClass(), ObjectFactoryTest\testGetObjectFromFactory(), and ObjectFactoryTest\testGetObjectFromInvalid().