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

Commit 3c09c6d

Browse files
committed
Added example to documentation
1 parent cf92672 commit 3c09c6d

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

doc/book/config-abstract-factory.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Configuration is done through the `config` service manager key, in an array with
3232
the key `Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory`. If you are using
3333
config merging from the MVC/ModuleManager, in this just means that you can
3434
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.
3637

3738
```php
3839
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
5758
correct tree of dependencies to successfully return any given service. Note that `Handler` does not have a
5859
configuration for the abstract factory, but this would work if `Handler` had a traditional factory and
5960
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+
```

doc/book/tmp.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class UserMapper
4+
{
5+
public function __construct(Adapter $db, Cache $cache) {}
6+
}
7+
8+
class Adapter
9+
{
10+
public function __construct(array $config) {}
11+
}
12+
13+
class Cache
14+
{
15+
public function __construct(CacheAdapter $cacheAdapter) {}
16+
}
17+
18+
class CacheAdapter
19+
{
20+
}

0 commit comments

Comments
 (0)