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

Commit b4f1f0c

Browse files
committed
Merge pull request #68 from snapshotpl/remove-stdlib-dependency
Remove Zend\Stdlib dependency
2 parents 97dfca5 + 120896c commit b4f1f0c

File tree

3 files changed

+107
-20
lines changed

3 files changed

+107
-20
lines changed

src/Config.php

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99

1010
namespace Zend\ServiceManager;
1111

12-
use Zend\Stdlib\ArrayUtils;
12+
use Zend\Stdlib\ArrayUtils\MergeRemoveKey;
13+
use Zend\Stdlib\ArrayUtils\MergeReplaceKeyInterface;
1314

1415
class Config implements ConfigInterface
1516
{
1617
/**
17-
* Allowed configuration keys
18-
*
1918
* @var array
2019
*/
21-
protected $allowedKeys = [
20+
private $allowedKeys = [
2221
'abstract_factories' => true,
23-
'aliases' => true,
24-
'delegators' => true,
25-
'factories' => true,
26-
'initializers' => true,
27-
'invokables' => true,
28-
'lazy_services' => true,
29-
'services' => true,
30-
'shared' => 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,
3130
];
3231

3332
/**
@@ -46,8 +45,6 @@ class Config implements ConfigInterface
4645
];
4746

4847
/**
49-
* Constructor
50-
*
5148
* @param array $config
5249
*/
5350
public function __construct(array $config = [])
@@ -58,15 +55,11 @@ public function __construct(array $config = [])
5855
unset($config[$key]);
5956
}
6057
}
61-
62-
$this->config = ArrayUtils::merge($this->config, $config);
58+
$this->config = $this->merge($this->config, $config);
6359
}
6460

6561
/**
66-
* Configure service manager
67-
*
68-
* @param ServiceManager $serviceManager
69-
* @return ServiceManager Returns the updated service manager instance.
62+
* @inheritdoc
7063
*/
7164
public function configureServiceManager(ServiceManager $serviceManager)
7265
{
@@ -80,4 +73,34 @@ public function toArray()
8073
{
8174
return $this->config;
8275
}
76+
77+
/**
78+
* Copy paste from https://github.com/zendframework/zend-stdlib/commit/26fcc32a358aa08de35625736095cb2fdaced090
79+
* to keep compatibility with previous version
80+
*
81+
* @link https://github.com/zendframework/zend-servicemanager/pull/68
82+
*/
83+
private function merge(array $a, array $b)
84+
{
85+
foreach ($b as $key => $value) {
86+
if ($value instanceof MergeReplaceKeyInterface) {
87+
$a[$key] = $value->getData();
88+
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
89+
if ($value instanceof MergeRemoveKey) {
90+
unset($a[$key]);
91+
} elseif (is_int($key)) {
92+
$a[] = $value;
93+
} elseif (is_array($value) && is_array($a[$key])) {
94+
$a[$key] = $this->merge($a[$key], $value);
95+
} else {
96+
$a[$key] = $value;
97+
}
98+
} else {
99+
if (!$value instanceof MergeRemoveKey) {
100+
$a[$key] = $value;
101+
}
102+
}
103+
}
104+
return $a;
105+
}
83106
}

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)