Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 9bb8571

Browse files
committed
Edited ConfigAbstractFactory documentation
Final review.
1 parent 367fd0c commit 9bb8571

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

doc/book/config-abstract-factory.md

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
11
# Config Abstract Factory
22

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
74

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:
1116

1217
```php
1318
$serviceManager = new ServiceManager();
1419
$serviceManager->addAbstractFactory(new ConfigAbstractFactory());
1520
```
1621

17-
Or within any config provider using:
22+
Or within configuration:
1823

1924
```php
2025
return [
26+
// zend-mvc:
2127
'service_manager' => [
2228
'abstract_factories' => [
2329
ConfigAbstractFactory::class,
2430
],
2531
],
32+
33+
// zend-expressive or ConfigProvider consumers:
34+
'dependencies' => [
35+
'abstract_factories' => [
36+
ConfigAbstractFactory::class,
37+
],
38+
],
2639
];
2740
```
2841

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:
3245

3346
```php
3447
return [
@@ -40,14 +53,20 @@ return [
4053
];
4154
```
4255

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:
4462

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:
5170

5271
```php
5372
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@@ -65,15 +84,20 @@ return [
6584
];
6685
```
6786

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 — for example, mapped to a custom factory — the service will
98+
be created and used as a dependency.
7599

76-
For a better example, consider the following classes:
100+
As another, more complete example, consider the following classes:
77101

78102
```php
79103
class UserMapper

0 commit comments

Comments
 (0)