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

Commit ce9d4c5

Browse files
committed
C/P ArrayUtils::merge add test for merging overrided config
1 parent b229413 commit ce9d4c5

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

src/Config.php

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,26 @@
99

1010
namespace Zend\ServiceManager;
1111

12+
use Zend\Stdlib\ArrayUtils\MergeRemoveKey;
13+
use Zend\Stdlib\ArrayUtils\MergeReplaceKeyInterface;
14+
1215
class Config implements ConfigInterface
1316
{
17+
/**
18+
* @var array
19+
*/
20+
private $allowedKeys = [
21+
'abstract_factories' => true,
22+
'aliases' => true,
23+
'delegators' => true,
24+
'factories' => true,
25+
'initializers' => true,
26+
'invokables' => true,
27+
'lazy_services' => true,
28+
'services' => true,
29+
'shared' => true,
30+
];
31+
1432
/**
1533
* @var array
1634
*/
@@ -32,9 +50,13 @@ class Config implements ConfigInterface
3250
public function __construct(array $config = [])
3351
{
3452
// Only merge keys we're interested in
35-
foreach (array_keys($this->config) as $requiredKey) {
36-
if (isset($config[$requiredKey])) {
37-
$this->config[$requiredKey] = $config[$requiredKey];
53+
foreach (array_keys($this->allowedKeys) as $requiredKey) {
54+
if ($this->isValidValue($config, $requiredKey)) {
55+
if ($this->isValidValue($this->config, $requiredKey)) {
56+
$this->config[$requiredKey] = $this->merge($this->config[$requiredKey], $config[$requiredKey]);
57+
} else {
58+
$this->config[$requiredKey] = $config[$requiredKey];
59+
}
3860
}
3961
}
4062
}
@@ -54,4 +76,45 @@ public function toArray()
5476
{
5577
return $this->config;
5678
}
79+
80+
/**
81+
* @param array $array
82+
* @param string $key
83+
*
84+
* @return bool
85+
*/
86+
private function isValidValue(array $array, $key)
87+
{
88+
return isset($array[$key]) && is_array($array[$key]);
89+
}
90+
91+
/**
92+
* Copy paste from https://github.com/zendframework/zend-stdlib/commit/26fcc32a358aa08de35625736095cb2fdaced090
93+
* to keep compatibility with previous version
94+
*
95+
* @link https://github.com/zendframework/zend-servicemanager/pull/68
96+
*/
97+
private function merge(array $a, array $b)
98+
{
99+
foreach ($b as $key => $value) {
100+
if ($value instanceof MergeReplaceKeyInterface) {
101+
$a[$key] = $value->getData();
102+
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
103+
if ($value instanceof MergeRemoveKey) {
104+
unset($a[$key]);
105+
} elseif (is_int($key)) {
106+
$a[] = $value;
107+
} elseif (is_array($value) && is_array($a[$key])) {
108+
$a[$key] = $this->merge($a[$key], $value);
109+
} else {
110+
$a[$key] = $value;
111+
}
112+
} else {
113+
if (!$value instanceof MergeRemoveKey) {
114+
$a[$key] = $value;
115+
}
116+
}
117+
}
118+
return $a;
119+
}
57120
}

test/ConfigTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,44 @@
1818
*/
1919
class ConfigTest extends TestCase
2020
{
21+
public function testMergeArrays()
22+
{
23+
$config = [
24+
'invokables' => [
25+
'foo' => TestAsset\InvokableObject::class,
26+
],
27+
'delegators' => [
28+
'foo' => [
29+
TestAsset\PreDelegator::class,
30+
]
31+
],
32+
'factories' => [
33+
'service' => TestAsset\FactoryObject::class,
34+
],
35+
];
36+
37+
$configuration = new TestAsset\ExtendedConfig($config);
38+
$result = $configuration->toArray();
39+
40+
$expected = [
41+
'invokables' => [
42+
'foo' => TestAsset\InvokableObject::class,
43+
TestAsset\InvokableObject::class => TestAsset\InvokableObject::class,
44+
],
45+
'delegators' => [
46+
'foo' => [
47+
TestAsset\InvokableObject::class,
48+
TestAsset\PreDelegator::class,
49+
],
50+
],
51+
'factories' => [
52+
'service' => TestAsset\FactoryObject::class,
53+
],
54+
];
55+
56+
$this->assertEquals($expected, $result);
57+
}
58+
2159
public function testPassesKnownServiceConfigKeysToServiceManagerWithConfigMethod()
2260
{
2361
$expected = [

test/TestAsset/ExtendedConfig.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 ZendTest\ServiceManager\TestAsset;
11+
12+
use Zend\ServiceManager\Config;
13+
14+
class ExtendedConfig extends Config
15+
{
16+
protected $config = [
17+
'invokables' => [
18+
InvokableObject::class => InvokableObject::class,
19+
],
20+
'delegators' => [
21+
'foo' => [
22+
InvokableObject::class,
23+
],
24+
],
25+
];
26+
}

0 commit comments

Comments
 (0)