@@ -32,7 +32,8 @@ Configuration is done through the `config` service manager key, in an array with
32
32
the key ` Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory ` . If you are using
33
33
config merging from the MVC/ModuleManager, in this just means that you can
34
34
add a ` ConfigAbstractFactory::class ` key to your merged config which contains service
35
- definitions:
35
+ definitions, where the key is the service name (typically the FQNS of the class you are
36
+ defining), and the value is an array of it's dependencies, also defined as container keys.
36
37
37
38
``` php
38
39
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@@ -57,3 +58,49 @@ call with that key on the service manager it is attached to. In this way, you ca
57
58
correct tree of dependencies to successfully return any given service. Note that ` Handler ` does not have a
58
59
configuration for the abstract factory, but this would work if ` Handler ` had a traditional factory and
59
60
can be created by this service manager.
61
+
62
+ For a better example, consider the following classes:
63
+
64
+ ``` php
65
+ class UserMapper
66
+ {
67
+ public function __construct(Adapter $db, Cache $cache) {}
68
+ }
69
+
70
+ class Adapter
71
+ {
72
+ public function __construct(array $config) {}
73
+ }
74
+
75
+ class Cache
76
+ {
77
+ public function __construct(CacheAdapter $cacheAdapter) {}
78
+ }
79
+
80
+ class CacheAdapter
81
+ {
82
+ }
83
+ ```
84
+
85
+ In this case, we can define the configuration for these classes as follows:
86
+
87
+ ``` php
88
+ // config/autoload/dependencies.php or anywhere that gets merged into global config
89
+ return [
90
+ ConfigAbstractFactory::class => [
91
+ CacheAdapter::class => [], // no dependencies
92
+ Cache::class => [
93
+ CacheAdapter::class, // dependency on the CacheAdapter key defined above
94
+ ],
95
+ UserMapper::class => [
96
+ Adapter::class, // will be called using normal factory defined below
97
+ Cache::class, // defined above and will be created using this abstract factory
98
+ ],
99
+ ],
100
+ 'service_manager' => [
101
+ 'factories' => [
102
+ Adapter::class => AdapterFactory::class, // normal factory not using above config
103
+ ],
104
+ ],
105
+ ],
106
+ ```
0 commit comments