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

Commit b25d50c

Browse files
committed
Re-added ConfigInterface
Discovered at least a half-dozen locations in the framework that use the interface. Because we define `withConfig()`, it's possible for these implementations to continue to live in v3, with minor modifications.
1 parent a2e6614 commit b25d50c

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

doc/book/migration.md

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ registered.
2525

2626
## Configuration
2727

28+
A number of changes have been made to configuration of service and plugin
29+
managers:
30+
31+
- Minor changes in configuration arrays may impact your usage.
32+
- `ConfigInterface` implementations and consumers will need updating.
33+
34+
### Configuration arrays
35+
2836
Configuration for v2 consisted of the following:
2937

3038
```php
@@ -77,7 +85,81 @@ In v3, the configuration remains the same, with the following additions:
7785
The main change is the addition of integrated lazy service configuration is now
7886
integrated.
7987

80-
### Invokables
88+
### ConfigInterface
89+
90+
The `ServiceManager` is now immutable, meaning that you cannot configure it
91+
after-the-fact. If your `ConfigInterface` implementation called on the various
92+
methods for injecting new services, such as:
93+
94+
- `setService()`
95+
- `setInvokableClass()`
96+
- `setFactory()`
97+
- `addAbstractFactory()`
98+
- `addInitializer()`
99+
- `addDelegator()`
100+
101+
then you will need to alter the logic to instead aggregate a full configuration
102+
specification. This can then be passed to the `ServiceManager` instance's
103+
`withConfig()` method, which does the following:
104+
105+
- Creates a clone of the manager.
106+
- Merges the incoming configuration with the current configuration within the
107+
clone.
108+
- Returns the clone.
109+
110+
As such, your implementation should return the result of
111+
`$serviceManager->withConfig()`.
112+
113+
As an example, consider this `HelperConfig` implementation from zend-i18n
114+
v2:
115+
116+
```php
117+
class HelperConfig implements ConfigInterface
118+
{
119+
protected $invokables = [
120+
'currencyformat' => 'Zend\I18n\View\Helper\CurrencyFormat',
121+
'dateformat' => 'Zend\I18n\View\Helper\DateFormat',
122+
'numberformat' => 'Zend\I18n\View\Helper\NumberFormat',
123+
'plural' => 'Zend\I18n\View\Helper\Plural',
124+
'translate' => 'Zend\I18n\View\Helper\Translate',
125+
'translateplural' => 'Zend\I18n\View\Helper\TranslatePlural',
126+
];
127+
128+
public function configureServiceManager(ServiceManager $serviceManager)
129+
{
130+
foreach ($this->invokables as $name => $service) {
131+
$serviceManager->setInvokableClass($name, $service);
132+
}
133+
}
134+
}
135+
```
136+
137+
In version 3, this will become:
138+
139+
```php
140+
use Interop\Container\ContainerInterface;
141+
142+
class HelperConfig implements ConfigInterface
143+
{
144+
protected $invokables = [
145+
'currencyformat' => 'Zend\I18n\View\Helper\CurrencyFormat',
146+
'dateformat' => 'Zend\I18n\View\Helper\DateFormat',
147+
'numberformat' => 'Zend\I18n\View\Helper\NumberFormat',
148+
'plural' => 'Zend\I18n\View\Helper\Plural',
149+
'translate' => 'Zend\I18n\View\Helper\Translate',
150+
'translateplural' => 'Zend\I18n\View\Helper\TranslatePlural',
151+
];
152+
153+
public function configureServiceManager(ServiceManager $serviceManager)
154+
{
155+
return $serviceManager->withConfig([
156+
'invokables' => $this->invokables,
157+
]);
158+
}
159+
}
160+
```
161+
162+
## Invokables
81163

82164
*Invokables no longer exist,* at least, not identically to how they existed in
83165
ZF2.
@@ -135,7 +217,7 @@ if needed).
135217
> }
136218
> ```
137219
138-
### Lazy Services
220+
## Lazy Services
139221
140222
In v2, if you wanted to create a lazy service, you needed to take the following
141223
steps:
@@ -680,7 +762,6 @@ We may re-introduce it via a separate component in the future.
680762
The following interfaces, traits, and classes were *removed*:
681763

682764
- `Zend\ServiceManager\Config`
683-
- `Zend\ServiceManager\ConfigInterface`
684765
- `Zend\ServiceManager\MutableCreationOptionsInterface`; this was previously
685766
used by the `AbstractPluginManager`, and is no longer required as we ship a
686767
separate `PluginManagerInterface`, and because the functionality is
@@ -697,8 +778,12 @@ too often abused under v2, and represent the antithesis of the purpose of the
697778
Service Manager component; dependencies should be directly injected, and the
698779
container should never be composed by objects.
699780

700-
The following classes have changes:
781+
The following classes and interfaces have changes:
701782

702783
- `Zend\ServiceManager\Proxy\LazyServiceFactory` is now marked `final`, and
703784
implements `Zend\ServiceManager\Proxy\DelegatorFactoryInterface`. Its
704785
dependencies and capabilities remain the same.
786+
- `Zend\ServiceManager\ConfigInterface` now is expected to *return* a
787+
`ServiceManager` instance. This is because instances cannot be configured
788+
in-situ; implementers will now need to call `withConfig()` and return the
789+
instance returned by that method.

src/ConfigInterface.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\ServiceManager;
11+
12+
interface ConfigInterface
13+
{
14+
/**
15+
* Configure a service manager.
16+
*
17+
* Implementations should pull configuration from somewhere (typically
18+
* local properties) and pass it to a ServiceManager's withConfig() method,
19+
* returning a new instance.
20+
*
21+
* @param ServiceManager $serviceManager
22+
* @return ServiceManager
23+
*/
24+
public function configureServiceManager(ServiceManager $serviceManager);
25+
}

0 commit comments

Comments
 (0)