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

Commit 917792a

Browse files
committed
Added some more missing features from zendframework/zend-modulemanager which might be useful in non-mvc environments
1 parent f073443 commit 917792a

7 files changed

+247
-60
lines changed

src/ZendModuleProvider.php

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
* New BSD License
77
*/
88
declare(strict_types=1);
9+
910
namespace Zend\ConfigAggregatorModuleManager;
1011

1112
use InvalidArgumentException;
1213
use Traversable;
1314
use Zend\Config\Config;
1415
use Zend\ModuleManager\Feature\ConfigProviderInterface;
15-
use Zend\ModuleManager\Feature\ControllerProviderInterface;
16+
use Zend\ModuleManager\Feature\FilterProviderInterface;
17+
use Zend\ModuleManager\Feature\FormElementProviderInterface;
18+
use Zend\ModuleManager\Feature\HydratorProviderInterface;
19+
use Zend\ModuleManager\Feature\InputFilterProviderInterface;
20+
use Zend\ModuleManager\Feature\RouteProviderInterface;
21+
use Zend\ModuleManager\Feature\SerializerProviderInterface;
1622
use Zend\ModuleManager\Feature\ServiceProviderInterface;
23+
use Zend\ModuleManager\Feature\ValidatorProviderInterface;
1724

1825
/**
1926
* Provide a configuration by using zend-modulemanager Module files.
@@ -46,27 +53,24 @@ public function __construct($module)
4653

4754
public function __invoke(): array
4855
{
49-
return array_replace_recursive($this->getModuleConfig(), [
56+
return array_filter(array_replace_recursive($this->getModuleConfig(), [
5057
$this->getDependenciesIdentifier() => $this->getModuleDependencies(),
51-
]);
52-
}
53-
54-
private function getModuleDependencies(): array
55-
{
56-
$module = $this->module;
57-
if (! $module instanceof ServiceProviderInterface) {
58-
return $this->dependencies;
59-
}
60-
61-
return array_replace_recursive($this->dependencies, $this->convert($module->getServiceConfig()));
58+
'route_manager' => $this->getRouteConfig(),
59+
'form_elements' => $this->getFormElementConfig(),
60+
'filters' => $this->getFilterConfig(),
61+
'validators' => $this->getValidatorConfig(),
62+
'hydrators' => $this->getHydratorConfig(),
63+
'input_filters' => $this->getInputFilterConfig(),
64+
'serializers' => $this->getSerializerConfig(),
65+
]));
6266
}
6367

6468
private function getModuleConfig(): array
6569
{
6670
$module = $this->module;
6771

68-
if (! $module instanceof ConfigProviderInterface
69-
&& ! is_callable([$module, 'getConfig'])
72+
if (!$module instanceof ConfigProviderInterface
73+
&& !is_callable([$module, 'getConfig'])
7074
) {
7175
return [];
7276
}
@@ -96,7 +100,7 @@ private function convert($config): array
96100
$config = iterator_to_array($config);
97101
}
98102

99-
if (! is_array($config)) {
103+
if (!is_array($config)) {
100104
throw new InvalidArgumentException(sprintf(
101105
'Config being merged must be an array, '
102106
. 'implement the Traversable interface, or be an '
@@ -117,4 +121,77 @@ public function setDependenciesIdentifier(string $dependenciesIdentifier): void
117121
{
118122
$this->dependenciesIdentifier = (string) $dependenciesIdentifier;
119123
}
124+
125+
private function getModuleDependencies(): array
126+
{
127+
$module = $this->module;
128+
if (!$module instanceof ServiceProviderInterface) {
129+
return $this->dependencies;
130+
}
131+
132+
return array_replace_recursive($this->dependencies, $this->convert($module->getServiceConfig()));
133+
}
134+
135+
public function getRouteConfig(): array
136+
{
137+
if (!$this->module instanceof RouteProviderInterface) {
138+
return [];
139+
}
140+
141+
return $this->convert($this->module->getRouteConfig());
142+
}
143+
144+
public function getFormElementConfig(): array
145+
{
146+
if (!$this->module instanceof FormElementProviderInterface) {
147+
return [];
148+
}
149+
150+
return $this->convert($this->module->getFormElementConfig());
151+
}
152+
153+
public function getFilterConfig(): array
154+
{
155+
if (!$this->module instanceof FilterProviderInterface) {
156+
return [];
157+
}
158+
159+
return $this->convert($this->module->getFilterConfig());
160+
}
161+
162+
public function getValidatorConfig(): array
163+
{
164+
if (!$this->module instanceof ValidatorProviderInterface) {
165+
return [];
166+
}
167+
168+
return $this->convert($this->module->getValidatorConfig());
169+
}
170+
171+
public function getHydratorConfig(): array
172+
{
173+
if (!$this->module instanceof HydratorProviderInterface) {
174+
return [];
175+
}
176+
177+
return $this->convert($this->module->getHydratorConfig());
178+
}
179+
180+
public function getInputFilterConfig()
181+
{
182+
if (!$this->module instanceof InputFilterProviderInterface) {
183+
return [];
184+
}
185+
186+
return $this->convert($this->module->getInputFilterConfig());
187+
}
188+
189+
public function getSerializerConfig(): array
190+
{
191+
if (!$this->module instanceof SerializerProviderInterface) {
192+
return [];
193+
}
194+
195+
return $this->convert($this->module->getSerializerConfig());
196+
}
120197
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregatorModuleManager\Resources;
4+
5+
trait ServiceManagerConfigurationTrait
6+
{
7+
private function createServiceManagerConfiguration(): array
8+
{
9+
return [
10+
'factories' => [],
11+
'invokables' => [],
12+
'aliases' => [],
13+
'delegators' => [],
14+
'abstract_factories' => [],
15+
'shared' => [],
16+
'initializers' => [],
17+
];
18+
}
19+
}

test/Resources/ZendModule.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
class ZendModule implements ServiceProviderInterface, ConfigProviderInterface
1313
{
14+
use ServiceManagerConfigurationTrait;
1415

1516
/**
1617
* {@inheritDoc}
@@ -27,10 +28,6 @@ public function getConfig()
2728
*/
2829
public function getServiceConfig()
2930
{
30-
return [
31-
'factories' => [
32-
'MyInvokable' => InvokableFactory::class,
33-
],
34-
];
31+
return $this->createServiceManagerConfiguration();
3532
}
3633
}

test/Resources/ZendModuleWithTraversableConfig.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
*/
1111
class ZendModuleWithTraversableConfig
1212
{
13+
use ServiceManagerConfigurationTrait;
1314

1415
public function getConfig()
1516
{
1617
return new ArrayObject([
17-
'service_manager' => [
18-
'invokables' => [
19-
stdClass::class => stdClass::class,
20-
],
21-
],
18+
'service_manager' => $this->createServiceManagerConfiguration(),
2219
]);
2320
}
2421
}

test/Resources/ZendModuleWithZendConfig.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
*/
1111
class ZendModuleWithZendConfig
1212
{
13+
use ServiceManagerConfigurationTrait;
1314

1415
public function getConfig()
1516
{
1617
return new Config([
17-
'service_manager' => [
18-
'invokables' => [
19-
stdClass::class => stdClass::class,
20-
],
21-
],
18+
'service_manager' => $this->createServiceManagerConfiguration(),
2219
]);
2320
}
2421
}

test/Resources/ZendModuleWithoutImplementingInterfaces.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
class ZendModuleWithoutImplementingInterfaces
1313
{
14+
use ServiceManagerConfigurationTrait;
1415

1516
/**
1617
* {@inheritDoc}
@@ -19,11 +20,7 @@ public function getConfig()
1920
{
2021
return [
2122
'__class__' => __CLASS__,
22-
'service_manager' => [
23-
'factories' => [
24-
'SomeObject' => InvokableFactory::class,
25-
],
26-
],
23+
'service_manager' => $this->createServiceManagerConfiguration(),
2724
];
2825
}
2926
}

0 commit comments

Comments
 (0)