1
1
# Config Abstract Factory
2
2
3
- You can simplify the process of creating factories by adding the
4
- ` ConfigAbstractFactory ` to your service manager. This allows you to define
5
- services using a configuration map, rather than having to create separate
6
- factories for all your services.
3
+ - Since 3.2.0
7
4
8
- ## Enabling
9
- You can enable the ` ConfigAbstractFactory ` in the same way that you would enable
10
- any other abstract factory - in your own code:
5
+ You can simplify the process of creating factories by registering
6
+ ` Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory ` with your service
7
+ manager instance. This allows you to define services using a configuration map,
8
+ rather than having to create separate factories for each of your services.
9
+
10
+ ## Enabling the ConfigAbstractFactory
11
+
12
+ Enable the ` ConfigAbstractFactory ` in the same way that you would enable
13
+ any other abstract factory.
14
+
15
+ Programmatically:
11
16
12
17
``` php
13
18
$serviceManager = new ServiceManager();
14
19
$serviceManager->addAbstractFactory(new ConfigAbstractFactory());
15
20
```
16
21
17
- Or within any config provider using :
22
+ Or within configuration :
18
23
19
24
``` php
20
25
return [
26
+ // zend-mvc:
21
27
'service_manager' => [
22
28
'abstract_factories' => [
23
29
ConfigAbstractFactory::class,
24
30
],
25
31
],
32
+
33
+ // zend-expressive or ConfigProvider consumers:
34
+ 'dependencies' => [
35
+ 'abstract_factories' => [
36
+ ConfigAbstractFactory::class,
37
+ ],
38
+ ],
26
39
];
27
40
```
28
41
29
- It is also possible to use the config abstract factory in the traditional way; by registering
30
- it as a factory for a specific class. This marginally improves performance but loses
31
- the benefit of not needing to create a factory key for all of you configured factories :
42
+ Like all abstract factories starting in version 3, you may also use the config
43
+ abstract factory as a mapped factory, registering it as a factory for a specific
44
+ class :
32
45
33
46
``` php
34
47
return [
@@ -40,14 +53,20 @@ return [
40
53
];
41
54
```
42
55
43
- ## Configuring
56
+ ## Configuration
57
+
58
+ Configuration should be provided via the ` config ` service, which should return
59
+ an array or ` ArrayObject ` . ` ConfigAbstractFactory ` looks for a top-level key in
60
+ this service named after itself (i.e., ` Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory ` )
61
+ that is an array value. Each item in the array:
44
62
45
- Configuration is done through the ` config ` service manager key, in an array with
46
- the key ` Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory ` . If you are using
47
- config merging from the MVC/ModuleManager, in this just means that you can
48
- add a ` ConfigAbstractFactory::class ` key to your merged config which contains service
49
- definitions, where the key is the service name (typically the FQNS of the class you are
50
- defining), and the value is an array of it's dependencies, also defined as container keys.
63
+ - Should have a key representing the service name (typically the fully
64
+ qualified class name)
65
+ - Should have a value that is an array of each dependency, ordered using the
66
+ constructor argument order, and using service names registered with the
67
+ container.
68
+
69
+ As an example:
51
70
52
71
``` php
53
72
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@@ -65,15 +84,20 @@ return [
65
84
];
66
85
```
67
86
68
- The definition tells the service manager how this abstract factory should manage dependencies in
69
- the classes defined. In the above example, ` MySimpleClass ` has a single dependency on a ` Logger `
70
- instance. The abstract factory will simply look to fulfil that dependency by calling a ` get `
71
- call with that key on the service manager it is attached to. In this way, you can create the
72
- correct tree of dependencies to successfully return any given service. Note that ` Handler ` does not have a
73
- configuration for the abstract factory, but this would work if ` Handler ` had a traditional factory and
74
- can be created by this service manager.
87
+ The definition tells the service manager how this abstract factory should manage
88
+ dependencies in the classes defined. In the above example, ` MySimpleClass ` has a
89
+ single dependency on a ` Logger ` instance. The abstract factory will simply look
90
+ to fulfil that dependency by calling ` get() ` with that key on the container
91
+ passed to it. In this way, you can create the correct tree of
92
+ dependencies to successfully return any given service.
93
+
94
+ In the above example, note that the abstract factory configuration does not
95
+ contain configuration for the ` Handler ` class. At first glance, this appears as
96
+ if it will fail; however, if ` Handler ` is configured directly with the container
97
+ already &mdash ; for example, mapped to a custom factory &mdash ; the service will
98
+ be created and used as a dependency.
75
99
76
- For a better example, consider the following classes:
100
+ As another, more complete example, consider the following classes:
77
101
78
102
``` php
79
103
class UserMapper
0 commit comments