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

Commit b888982

Browse files
committed
Add method for retrieving configuration to ConfigInterface
Added `toArray()` method to `ConfigInterface` to provide a mechanism for fetching configuration without needing to configure the service manager. This will be useful in particular for `Zend\ModuleManager\Listener\ServiceListener`, which primarily needs to aggregate and merge configuration *prior* to creating a new instance of a plugin manager. An implementation was added to the `Config` class, along with tests.
1 parent 4d373b8 commit b888982

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

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)