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

Commit 3f4e6a6

Browse files
committed
Merge branch 'feature/config-as-array' into develop
Close #31
2 parents 4d373b8 + 1d87c88 commit 3f4e6a6

File tree

5 files changed

+65
-27
lines changed

5 files changed

+65
-27
lines changed

CHANGELOG.md

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,6 @@ All notable changes to this project will be documented in this file, in reverse
7777

7878
- Integration with `Zend\Di` has been removed. It may be re-integrated later.
7979

80-
- The `invokables` configuration key no longer exists. It has been replaced by a
81-
built-in factory.
82-
83-
In versions 2.x:
84-
85-
```php
86-
return [
87-
'service_manager' => [
88-
'invokables' => [
89-
MyClass::class => MyClass:class,
90-
],
91-
],
92-
];
93-
```
94-
95-
In ZF 3.x:
96-
97-
```php
98-
return [
99-
'service_manager' => [
100-
'factories' => [
101-
MyClass::class => 'Zend\ServiceManager\Factory\InvokableFactory',
102-
],
103-
],
104-
];
105-
```
106-
10780
- `MutableCreationOptionsInterface` has been removed, as options can now be
10881
passed directly through factories.
10982

@@ -138,12 +111,24 @@ changes, outlined in this section.
138111
]);
139112
```
140113

114+
`Config` and `ConfigInterface` still exist, however, but primarily for the
115+
purposes of codifying and aggregating configuration to use.
116+
141117
- The ServiceManager is now immutable. Once configured, it cannot be altered.
142118
You need to create a new service manager if you need to change the
143119
configuration. This ensures safer and more aggressive caching. A new method,
144120
`withConfig()`, allows you to create a new instance that merges the provided
145121
configuration.
146122

123+
- `ConfigInterface` has two important changes:
124+
- `configureServiceManager()` now **must** return a service manager instance.
125+
Since the ServiceManager is now immutable, and the various methods for
126+
injecting services are gone, the expectation is that this method will pass
127+
configuration to `ServiceManager::withConfig()` and return the new instance.
128+
- A new method, `toArray()`, was added, to allow pulling the configuration in
129+
order to pass to a ServiceManager or plugin manager's constructor or
130+
`withConfig()` method.
131+
147132
- Interfaces for `FactoryInterface`, `DelegatorFactoryInterface` and
148133
`AbstractFactoryInterface` have changed. All are now directly invokable. This
149134
allows a number of performance optimization internally.
@@ -227,6 +212,9 @@ changes, outlined in this section.
227212
- `PluginManager` now enforces the need for the main service locator in its
228213
constructor. In v2.x, people often forgot to set the parent locator, which led
229214
to bugs in factories trying to fetch dependencies from the parent locator.
215+
Additionally, plugin managers now pull dependencies from the parent locator by
216+
default; if you need to pull a peer plugin, your factories will now need to
217+
pull the corresponding plugin manager first.
230218

231219
- It's so fast now that your app will fly!
232220

doc/book/migration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ class HelperConfig implements ConfigInterface
159159
}
160160
```
161161

162+
Additionally, we have **added** another method to `ConfigInterface`,
163+
`toArray()`. This should return an array in a format that can be passed to the
164+
`ServiceManager`'s constructor or `withConfig()` method.
165+
162166
### Config class
163167

164168
`Zend\ServiceManager\Config` has been updated to follow the changes to the

src/Config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ public function configureServiceManager(ServiceManager $serviceManager)
7272
{
7373
return $serviceManager->withConfig($this->config);
7474
}
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
public function toArray()
80+
{
81+
return $this->config;
82+
}
7583
}

src/ConfigInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,27 @@ interface ConfigInterface
2222
* @return ServiceManager
2323
*/
2424
public function configureServiceManager(ServiceManager $serviceManager);
25+
26+
/**
27+
* Return configuration for a service manager instance as an array.
28+
*
29+
* Implementations MUST return an array compatible with ServiceManager::configure,
30+
* containing one or more of the following keys:
31+
*
32+
* - abstract_factories
33+
* - aliases
34+
* - delegators
35+
* - factories
36+
* - initializers
37+
* - invokables
38+
* - lazy_services
39+
* - services
40+
* - shared
41+
*
42+
* In other words, this should return configuration that can be used to instantiate
43+
* a service manager or plugin manager, or pass to its `withConfig()` method.
44+
*
45+
* @return array
46+
*/
47+
public function toArray();
2548
}

test/ConfigTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,20 @@ public function testPassesKnownServiceConfigKeysToServiceManagerWithConfigMethod
6969

7070
$configuration = new Config($config);
7171
$this->assertEquals('CALLED', $configuration->configureServiceManager($services->reveal()));
72+
73+
return [
74+
'array' => $expected,
75+
'config' => $configuration,
76+
];
77+
}
78+
79+
/**
80+
* @depends testPassesKnownServiceConfigKeysToServiceManagerWithConfigMethod
81+
*/
82+
public function testToArrayReturnsConfiguration($dependencies)
83+
{
84+
$configuration = $dependencies['array'];
85+
$configInstance = $dependencies['config'];
86+
$this->assertSame($configuration, $configInstance->toArray());
7287
}
7388
}

0 commit comments

Comments
 (0)