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

Commit 319e634

Browse files
michalbundyraweierophinney
authored andcommitted
Updated configuring service manager documentation
Invokables are used in zend-expressive a lot and these were missing in the docs
1 parent 9f35a10 commit 319e634

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

docs/book/configuring-the-service-manager.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ The Service Manager component can be configured by passing an associative array
44
constructor. The following keys are:
55

66
- `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.
710
- `factories`: associative array that map a key to a factory name, or any callable.
811
- `abstract_factories`: a list of abstract factories classes. An abstract
912
factory is a factory that can potentially create any object, based on some
1013
criterias.
1114
- `delegators`: an associative array that maps service keys to lists of delegator factory keys, see the [delegators documentation](delegators.md) for more details.
1215
- `aliases`: associative array that map a key to a service key (or another alias).
1316
- `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.
1717
- `lazy_services`: configuration for the lazy service proxy manager, and a class
1818
map of service:class pairs that will act as lazy services; see the
1919
[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.
2023
- `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.
2225

2326
Here is an example of how you could configure a service manager:
2427

@@ -27,11 +30,15 @@ use Zend\ServiceManager\ServiceManager;
2730

2831
$serviceManager = new ServiceManager([
2932
'services' => [],
33+
'invokables' => [],
3034
'factories' => [],
3135
'abstract_factories' => [],
3236
'delegators' => [],
37+
'aliases' => [],
38+
'initializers' => [],
39+
'lazy_services' => [],
3340
'shared' => [],
34-
'shared_by_default' => true
41+
'shared_by_default' => true,
3542
]);
3643
```
3744

@@ -50,12 +57,13 @@ use stdClass;
5057

5158
$serviceManager = new ServiceManager([
5259
'factories' => [
53-
stdClass::class => InvokableFactory::class
54-
]
60+
MyObject::class => MyObjectFactory::class,
61+
],
5562
]);
5663
```
5764

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.
5967
6068
As said before, a factory can also be a callable, to create more complex objects:
6169

@@ -95,6 +103,16 @@ class MyObjectFactory implements FactoryInterface
95103
}
96104
}
97105

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+
98116
// When creating the service manager:
99117
$serviceManager = new ServiceManager([
100118
'factories' => [
@@ -130,6 +148,16 @@ class MyObjectFactory implements FactoryInterface
130148
}
131149
}
132150

151+
// or without implementing the interface:
152+
class MyObjectFactory
153+
{
154+
public function __invoke(ContainerInterface $container, $requestedName)
155+
{
156+
$dependency = $container(Dependency::class);
157+
return new $requestedName($dependency);
158+
}
159+
}
160+
133161
// When creating the service manager:
134162
$serviceManager = new ServiceManager([
135163
'factories' => [

0 commit comments

Comments
 (0)