| 1 |
Wikimedia ObjectFactory |
| 2 |
======================= |
| 3 |
|
| 4 |
Construct objects from configuration instructions. |
| 5 |
|
| 6 |
It can be used statically, or as a service wrapping a PSR-11 service container |
| 7 |
for lazy instantiation of objects with dependency injection. |
| 8 |
|
| 9 |
Specification array |
| 10 |
------------------- |
| 11 |
|
| 12 |
Contents of the specification array are as follows: |
| 13 |
|
| 14 |
'factory' => callable, |
| 15 |
'class' => string, |
| 16 |
|
| 17 |
The specification array must contain either a 'class' key with string value |
| 18 |
that specifies the class name to instantiate or a 'factory' key with a |
| 19 |
callable (is_callable() === true). If both are passed, 'factory' takes |
| 20 |
precedence but an InvalidArgumentException will be thrown if the resulting |
| 21 |
object is not an instance of the named class. |
| 22 |
|
| 23 |
'args' => array, |
| 24 |
'closure_expansion' => bool, // default true |
| 25 |
'spec_is_arg' => bool, // default false |
| 26 |
'services' => (string|null)[], // default empty |
| 27 |
'optional_services' => (string|null)[], // default empty |
| 28 |
|
| 29 |
The 'args' key, if provided, specifies arguments to pass to the constructor/callable. |
| 30 |
Values in 'args' which are Closure instances will be expanded by invoking |
| 31 |
them with no arguments before passing the resulting value on to the |
| 32 |
constructor/callable. This can be used to pass live objects to the |
| 33 |
constructor/callable. This behavior can be suppressed by adding |
| 34 |
closure_expansion => false to the specification. |
| 35 |
|
| 36 |
If 'spec_is_arg' => true is in the specification, 'args' is ignored. The |
| 37 |
entire spec array is passed to the constructor/callable instead. |
| 38 |
|
| 39 |
If 'services' is supplied and non-empty (and a service container is available), |
| 40 |
the named services are requested from the PSR-11 service container and |
| 41 |
prepended before 'args'. `null` values in 'services' are passed to the constructor |
| 42 |
unchanged. |
| 43 |
|
| 44 |
Optional services declared via 'optional_services' are handled the same, |
| 45 |
except that if the service is not available from the service container |
| 46 |
`null` is passed as a parameter instead. Optional services are appended |
| 47 |
directly after the normal required services |
| 48 |
|
| 49 |
If any extra arguments are passed in the options to getObjectFromSpec() or |
| 50 |
createObject(), these are prepended before the 'services' and 'args'. |
| 51 |
These are passed using the `extraArgs` key, and any Closure instances that |
| 52 |
are included this way are never expanded, regardless of whether the main |
| 53 |
argument list is expanded. |
| 54 |
|
| 55 |
'calls' => array |
| 56 |
|
| 57 |
The specification may also contain a 'calls' key that describes method |
| 58 |
calls to make on the newly created object before returning it. This |
| 59 |
pattern is often known as "setter injection". The value of this key is |
| 60 |
expected to be an associative array with method names as keys and |
| 61 |
argument lists as values. The argument list will be expanded (or not) |
| 62 |
in the same way as the 'args' key for the main object. |
| 63 |
|
| 64 |
Note these calls are not passed the extra arguments. |
| 65 |
|
| 66 |
Installation |
| 67 |
------------ |
| 68 |
|
| 69 |
``` |
| 70 |
$ composer require wikimedia/object-factory |
| 71 |
``` |
| 72 |
|
| 73 |
Usage |
| 74 |
----- |
| 75 |
|
| 76 |
```php |
| 77 |
<?php |
| 78 |
|
| 79 |
$specs = [ |
| 80 |
// Simple constructor based injection |
| 81 |
'testDB' => [ |
| 82 |
'class' => PDO::class, |
| 83 |
'args' => [ |
| 84 |
'mysql:dbname=testdb;host=127.0.0.1', |
| 85 |
'dbuser', |
| 86 |
'dbpass', |
| 87 |
], |
| 88 |
], |
| 89 |
]; |
| 90 |
|
| 91 |
$db = ObjectFactory::getObjectFromSpec( $specs['testDB'] ): |
| 92 |
``` |
| 93 |
|
| 94 |
License |
| 95 |
------- |
| 96 |
Wikimedia ObjectFactory is licensed under the GNU General Public License, |
| 97 |
version 2 and any later version (GPL-2.0-or-later). See the |
| 98 |
[`COPYING`](COPYING) file for more details. |