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

Commit 61c0cbc

Browse files
committed
Merge branch 'hotfix/68'
Close #68
2 parents 97dfca5 + 14aa9c0 commit 61c0cbc

File tree

5 files changed

+127
-22
lines changed

5 files changed

+127
-22
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ All notable changes to this project will be documented in this file, in reverse
1414

1515
### Removed
1616

17-
- Nothing.
17+
- [#68](https://github.com/zendframework/zend-servicemanager/pull/68) removes
18+
the dependency on zend-stdlib by inlining the `ArrayUtils::merge()` routine
19+
as a private method of `Zend\ServiceManager\Config`.
1820

1921
### Fixed
2022

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"athletic/athletic": "dev-master"
2525
},
2626
"suggest": {
27-
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services"
27+
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services",
28+
"zendframework/zend-stdlib": "zend-stdlib ^2.5 if you wish to use the MergeReplaceKey or MergeRemoveKey features in Config instances"
2829
},
2930
"minimum-stability": "dev",
3031
"prefer-stable": true,

src/Config.php

+58-20
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,39 @@
99

1010
namespace Zend\ServiceManager;
1111

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

15+
/**
16+
* Object for defining configuration and configuring an existing service manager instance.
17+
*
18+
* In order to provide configuration merging capabilities, this class implements
19+
* the same functionality as `Zend\Stdlib\ArrayUtils::merge()`. That routine
20+
* allows developers to specifically shape how values are merged:
21+
*
22+
* - A value which is an instance of `MergeRemoveKey` indicates the value should
23+
* be removed during merge.
24+
* - A value that is an instance of `MergeReplaceKeyInterface` indicates that the
25+
* value it contains should be used to replace any previous versions.
26+
*
27+
* These features are advanced, and not typically used. If you wish to use them,
28+
* you will need to require the zend-stdlib package in your application.
29+
*/
1430
class Config implements ConfigInterface
1531
{
1632
/**
17-
* Allowed configuration keys
18-
*
1933
* @var array
2034
*/
21-
protected $allowedKeys = [
35+
private $allowedKeys = [
2236
'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,
37+
'aliases' => true,
38+
'delegators' => true,
39+
'factories' => true,
40+
'initializers' => true,
41+
'invokables' => true,
42+
'lazy_services' => true,
43+
'services' => true,
44+
'shared' => true,
3145
];
3246

3347
/**
@@ -46,8 +60,6 @@ class Config implements ConfigInterface
4660
];
4761

4862
/**
49-
* Constructor
50-
*
5163
* @param array $config
5264
*/
5365
public function __construct(array $config = [])
@@ -58,15 +70,11 @@ public function __construct(array $config = [])
5870
unset($config[$key]);
5971
}
6072
}
61-
62-
$this->config = ArrayUtils::merge($this->config, $config);
73+
$this->config = $this->merge($this->config, $config);
6374
}
6475

6576
/**
66-
* Configure service manager
67-
*
68-
* @param ServiceManager $serviceManager
69-
* @return ServiceManager Returns the updated service manager instance.
77+
* @inheritdoc
7078
*/
7179
public function configureServiceManager(ServiceManager $serviceManager)
7280
{
@@ -80,4 +88,34 @@ public function toArray()
8088
{
8189
return $this->config;
8290
}
91+
92+
/**
93+
* Copy paste from https://github.com/zendframework/zend-stdlib/commit/26fcc32a358aa08de35625736095cb2fdaced090
94+
* to keep compatibility with previous version
95+
*
96+
* @link https://github.com/zendframework/zend-servicemanager/pull/68
97+
*/
98+
private function merge(array $a, array $b)
99+
{
100+
foreach ($b as $key => $value) {
101+
if ($value instanceof MergeReplaceKeyInterface) {
102+
$a[$key] = $value->getData();
103+
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
104+
if ($value instanceof MergeRemoveKey) {
105+
unset($a[$key]);
106+
} elseif (is_int($key)) {
107+
$a[] = $value;
108+
} elseif (is_array($value) && is_array($a[$key])) {
109+
$a[$key] = $this->merge($a[$key], $value);
110+
} else {
111+
$a[$key] = $value;
112+
}
113+
} else {
114+
if (!$value instanceof MergeRemoveKey) {
115+
$a[$key] = $value;
116+
}
117+
}
118+
}
119+
return $a;
120+
}
83121
}

test/ConfigTest.php

+38
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

+26
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)