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

Commit 539bca7

Browse files
committed
Added project files after initial commit
1 parent b6a6c33 commit 539bca7

7 files changed

+379
-0
lines changed

src/ZendModuleProvider.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-config-aggregator-modulemanager for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-config-aggregator-modulemanager/blob/master/LICENSE.md
6+
* New BSD License
7+
*/
8+
9+
namespace Zend\ConfigAggregator\ModuleManager;
10+
11+
use InvalidArgumentException;
12+
use Traversable;
13+
use Zend\Config\Config;
14+
use Zend\ModuleManager\Feature\ConfigProviderInterface;
15+
use Zend\ModuleManager\Feature\ControllerProviderInterface;
16+
use Zend\ModuleManager\Feature\ServiceProviderInterface;
17+
18+
/**
19+
* Provide a configuration by using zend-modulemanager Module files.
20+
*/
21+
class ZendModuleProvider
22+
{
23+
24+
/**
25+
* @var object
26+
*/
27+
private $module;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $dependencies = [];
33+
34+
/**
35+
* @var string
36+
*/
37+
private $dependenciesIdentifier = 'dependencies';
38+
39+
/**
40+
* @param object $module
41+
*/
42+
public function __construct($module)
43+
{
44+
$this->module = $module;
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function __invoke()
51+
{
52+
return array_replace_recursive($this->getModuleConfig(), [
53+
$this->getDependenciesIdentifier() => $this->getModuleDependencies(),
54+
]);
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
private function getModuleDependencies()
61+
{
62+
$module = $this->module;
63+
if (! $module instanceof ServiceProviderInterface) {
64+
return $this->dependencies;
65+
}
66+
67+
return array_replace_recursive($this->dependencies, $this->convert($module->getServiceConfig()));
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
private function getModuleConfig()
74+
{
75+
$module = $this->module;
76+
77+
if (! $module instanceof ConfigProviderInterface
78+
&& ! is_callable([$module, 'getConfig'])
79+
) {
80+
return [];
81+
}
82+
83+
$converted = $this->convert($module->getConfig());
84+
85+
if (isset($converted['service_manager'])) {
86+
$this->dependencies = $converted['service_manager'] ?: [];
87+
unset($converted['service_manager']);
88+
}
89+
90+
return $converted;
91+
}
92+
93+
/**
94+
* @param array|Traversable $config
95+
*
96+
* @return array
97+
*/
98+
private function convert($config)
99+
{
100+
if ($config instanceof Config) {
101+
$config = $config->toArray();
102+
}
103+
104+
if ($config instanceof Traversable) {
105+
$config = iterator_to_array($config);
106+
}
107+
108+
if (! is_array($config)) {
109+
throw new InvalidArgumentException(sprintf(
110+
'Config being merged must be an array, '
111+
. 'implement the Traversable interface, or be an '
112+
. 'instance of Zend\Config\Config. %s given.',
113+
is_object($config) ? get_class($config) : gettype($config)
114+
));
115+
}
116+
117+
return $config;
118+
}
119+
120+
/**
121+
* @return string
122+
*/
123+
public function getDependenciesIdentifier()
124+
{
125+
return $this->dependenciesIdentifier;
126+
}
127+
128+
/**
129+
* @param string $dependenciesIdentifier
130+
*
131+
* @return void
132+
*/
133+
public function setDependenciesIdentifier($dependenciesIdentifier)
134+
{
135+
$this->dependenciesIdentifier = (string) $dependenciesIdentifier;
136+
}
137+
}

test/Resources/ZendModule.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregator\ModuleManager\Resources;
4+
5+
use Zend\ModuleManager\Feature\ConfigProviderInterface;
6+
use Zend\ModuleManager\Feature\ServiceProviderInterface;
7+
use Zend\ServiceManager\Factory\InvokableFactory;
8+
9+
/**
10+
* @author Maximilian Bösing <max@boesing.email>
11+
*/
12+
class ZendModule implements ServiceProviderInterface, ConfigProviderInterface
13+
{
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function getConfig()
19+
{
20+
return [
21+
'__class__' => __CLASS__,
22+
];
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function getServiceConfig()
29+
{
30+
return [
31+
'factories' => [
32+
'MyInvokable' => InvokableFactory::class,
33+
],
34+
];
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregator\ModuleManager\Resources;
4+
5+
/**
6+
* @author Maximilian Bösing <max@boesing.email>
7+
*/
8+
class ZendModuleWithInvalidConfiguration
9+
{
10+
11+
public function getConfig()
12+
{
13+
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregator\ModuleManager\Resources;
4+
5+
use ArrayObject;
6+
use stdClass;
7+
8+
/**
9+
* @author Maximilian Bösing <max@boesing.email>
10+
*/
11+
class ZendModuleWithTraversableConfig
12+
{
13+
14+
public function getConfig()
15+
{
16+
return new ArrayObject([
17+
'service_manager' => [
18+
'invokables' => [
19+
stdClass::class => stdClass::class,
20+
],
21+
],
22+
]);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregator\ModuleManager\Resources;
4+
5+
use stdClass;
6+
use Zend\Config\Config;
7+
8+
/**
9+
* @author Maximilian Bösing <max@boesing.email>
10+
*/
11+
class ZendModuleWithZendConfig
12+
{
13+
14+
public function getConfig()
15+
{
16+
return new Config([
17+
'service_manager' => [
18+
'invokables' => [
19+
stdClass::class => stdClass::class,
20+
],
21+
],
22+
]);
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregator\ModuleManager\Resources;
4+
5+
use Zend\ModuleManager\Feature\ConfigProviderInterface;
6+
use Zend\ModuleManager\Feature\ServiceProviderInterface;
7+
use Zend\ServiceManager\Factory\InvokableFactory;
8+
9+
/**
10+
* @author Maximilian Bösing <max@boesing.email>
11+
*/
12+
class ZendModuleWithoutImplementingInterfaces
13+
{
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function getConfig()
19+
{
20+
return [
21+
'__class__' => __CLASS__,
22+
'service_manager' => [
23+
'factories' => [
24+
'SomeObject' => InvokableFactory::class,
25+
],
26+
],
27+
];
28+
}
29+
}

test/ZendModuleProviderTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace ZendTest\ConfigAggregator\ModuleManager;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
use stdClass;
8+
use Zend\ConfigAggregator\ModuleManager\ZendModuleProvider;
9+
use Zend\ServiceManager\Factory\InvokableFactory;
10+
use ZendTest\ConfigAggregator\ModuleManager\Resources\ZendModule;
11+
use ZendTest\ConfigAggregator\ModuleManager\Resources\ZendModuleWithInvalidConfiguration;
12+
use ZendTest\ConfigAggregator\ModuleManager\Resources\ZendModuleWithoutImplementingInterfaces;
13+
use ZendTest\ConfigAggregator\ModuleManager\Resources\ZendModuleWithTraversableConfig;
14+
use ZendTest\ConfigAggregator\ModuleManager\Resources\ZendModuleWithZendConfig;
15+
16+
/**
17+
* @author Maximilian Bösing <max@boesing.email>
18+
*/
19+
class ZendModuleProviderTest extends TestCase
20+
{
21+
22+
public function testCanProvideDependenciesFromInterface()
23+
{
24+
$module = new ZendModule();
25+
$provider = new ZendModuleProvider($module);
26+
27+
$config = $provider();
28+
29+
$this->assertArrayHasKey('dependencies', $config);
30+
$this->assertSame([
31+
'factories' => [
32+
'MyInvokable' => InvokableFactory::class,
33+
],
34+
], $config['dependencies']);
35+
}
36+
37+
public function testCanProvideAnyConfigValue()
38+
{
39+
$module = new ZendModule();
40+
$provider = new ZendModuleProvider($module);
41+
42+
$config = $provider();
43+
44+
$this->assertArrayHasKey('__class__', $config);
45+
$this->assertSame(ZendModule::class, $config['__class__']);
46+
}
47+
48+
public function testCanProvideDependenciesFromModuleWithoutInterface()
49+
{
50+
$module = new ZendModuleWithoutImplementingInterfaces();
51+
$provider = new ZendModuleProvider($module);
52+
53+
$config = $provider();
54+
55+
$this->assertArrayHasKey('dependencies', $config);
56+
$this->assertSame([
57+
'factories' => [
58+
'SomeObject' => InvokableFactory::class,
59+
],
60+
], $config['dependencies']);
61+
}
62+
63+
public function testCanHandleModulesWithoutConfigurationProvider()
64+
{
65+
$module = new stdClass();
66+
$provider = new ZendModuleProvider($module);
67+
68+
$config = $provider();
69+
70+
$this->assertArrayHasKey('dependencies', $config);
71+
$this->assertEmpty($config['dependencies']);
72+
}
73+
74+
public function testCanHandleModulesWithTraversableConfiguration()
75+
{
76+
$module = new ZendModuleWithZendConfig();
77+
$provider = new ZendModuleProvider($module);
78+
79+
$config = $provider();
80+
81+
$this->assertArrayHasKey('dependencies', $config);
82+
$this->assertSame([
83+
'invokables' => [
84+
stdClass::class => stdClass::class,
85+
],
86+
], $config['dependencies']);
87+
}
88+
89+
public function testCanHandleModuelsWithZendConfigConfiguration()
90+
{
91+
$module = new ZendModuleWithTraversableConfig();
92+
$provider = new ZendModuleProvider($module);
93+
94+
$config = $provider();
95+
96+
$this->assertArrayHasKey('dependencies', $config);
97+
$this->assertSame([
98+
'invokables' => [
99+
stdClass::class => stdClass::class,
100+
],
101+
], $config['dependencies']);
102+
}
103+
104+
/**
105+
* @expectedException InvalidArgumentException
106+
*/
107+
public function testThrowsInvalidArgumentExceptionOnInvalidConfiguration()
108+
{
109+
$module = new ZendModuleWithInvalidConfiguration();
110+
$provider = new ZendModuleProvider($module);
111+
112+
$provider();
113+
}
114+
}

0 commit comments

Comments
 (0)