You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 6, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: docs/book/configuring-the-service-manager.md
+36-8Lines changed: 36 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,24 @@ The Service Manager component can be configured by passing an associative array
4
4
constructor. The following keys are:
5
5
6
6
-`services`: associative array that maps a key to a service instance.
7
+
-`invokables`: an associative array that map a key to a constructor-less service;
8
+
i.e., for services that do not require arguments to the constructor. The key and
9
+
service name usually are the same; if they are not, the key is treated as an alias.
7
10
-`factories`: associative array that map a key to a factory name, or any callable.
8
11
-`abstract_factories`: a list of abstract factories classes. An abstract
9
12
factory is a factory that can potentially create any object, based on some
10
13
criterias.
11
14
-`delegators`: an associative array that maps service keys to lists of delegator factory keys, see the [delegators documentation](delegators.md) for more details.
12
15
-`aliases`: associative array that map a key to a service key (or another alias).
13
16
-`initializers`: a list of callable or initializers that are run whenever a service has been created.
14
-
-`shared`: associative array that map a service name to a boolean, in order to
15
-
indicate the service manager if it should cache or not a service created
16
-
through the `get` method, independant of the `shared_by_default` setting.
17
17
-`lazy_services`: configuration for the lazy service proxy manager, and a class
18
18
map of service:class pairs that will act as lazy services; see the
19
19
[lazy services documentation](lazy-services.md) for more details.
20
+
-`shared`: associative array that map a service name to a boolean, in order to
21
+
indicate the service manager if it should cache or not a service created
22
+
through the `get` method, independent of the `shared_by_default` setting.
20
23
-`shared_by_default`: boolean that indicates whether services created through
21
-
the `get` method should be cached. This is true by default.
24
+
the `get` method should be cached. This is `true` by default.
22
25
23
26
Here is an example of how you could configure a service manager:
24
27
@@ -27,11 +30,15 @@ use Zend\ServiceManager\ServiceManager;
27
30
28
31
$serviceManager = new ServiceManager([
29
32
'services' => [],
33
+
'invokables' => [],
30
34
'factories' => [],
31
35
'abstract_factories' => [],
32
36
'delegators' => [],
37
+
'aliases' => [],
38
+
'initializers' => [],
39
+
'lazy_services' => [],
33
40
'shared' => [],
34
-
'shared_by_default' => true
41
+
'shared_by_default' => true,
35
42
]);
36
43
```
37
44
@@ -50,12 +57,13 @@ use stdClass;
50
57
51
58
$serviceManager = new ServiceManager([
52
59
'factories' => [
53
-
stdClass::class => InvokableFactory::class
54
-
]
60
+
MyObject::class => MyObjectFactory::class,
61
+
],
55
62
]);
56
63
```
57
64
58
-
> This mechanism replaces the `invokables` key that was used in Zend Framework 2.
65
+
> For invokable classes we can use `Zend\ServiceManager\Factory\InvokableFactory`
66
+
> but for performance reasons using `invokables` configuration is recommended.
59
67
60
68
As said before, a factory can also be a callable, to create more complex objects:
61
69
@@ -95,6 +103,16 @@ class MyObjectFactory implements FactoryInterface
95
103
}
96
104
}
97
105
106
+
// or without implementing the interface:
107
+
class MyObjectFactory
108
+
{
109
+
public function __invoke(ContainerInterface $container, $requestedName)
110
+
{
111
+
$dependency = $container->get(Dependency::class);
112
+
return new MyObject($dependency);
113
+
}
114
+
}
115
+
98
116
// When creating the service manager:
99
117
$serviceManager = new ServiceManager([
100
118
'factories' => [
@@ -130,6 +148,16 @@ class MyObjectFactory implements FactoryInterface
130
148
}
131
149
}
132
150
151
+
// or without implementing the interface:
152
+
class MyObjectFactory
153
+
{
154
+
public function __invoke(ContainerInterface $container, $requestedName)
0 commit comments