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

Commit 71cfb1c

Browse files
committed
Refactor Config implementation
Per @Ocramius, to reduce complexity, it's easier to simply ensure that the keys we're interested in are always present, and then pass the configuration directly to the SM instance.
1 parent a3efbdf commit 71cfb1c

File tree

1 file changed

+40
-51
lines changed

1 file changed

+40
-51
lines changed

src/Config.php

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,57 @@
99

1010
namespace Zend\ServiceManager;
1111

12+
use Zend\Stdlib\ArrayUtils;
13+
1214
class Config implements ConfigInterface
1315
{
1416
/**
17+
* Allowed configuration keys
18+
*
1519
* @var array
1620
*/
17-
protected $config = [];
21+
protected $allowedKeys = [
22+
'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,
31+
];
32+
33+
/**
34+
* @var array
35+
*/
36+
protected $config = [
37+
'abstract_factories' => [],
38+
'aliases' => [],
39+
'delegators' => [],
40+
'factories' => [],
41+
'initializers' => [],
42+
'invokables' => [],
43+
'lazy_services' => [],
44+
'services' => [],
45+
'shared' => [],
46+
];
1847

1948
/**
2049
* Constructor
2150
*
2251
* @param array $config
2352
*/
24-
public function __construct($config = [])
53+
public function __construct(array $config = [])
2554
{
26-
$this->config = $config;
55+
// Only merge keys we're interested in
56+
foreach (array_keys($config) as $key) {
57+
if (! isset($this->allowedKeys[$key])) {
58+
unset($config[$key]);
59+
}
60+
}
61+
62+
$this->config = ArrayUtils::merge($this->config, $config);
2763
}
2864

2965
/**
@@ -34,53 +70,6 @@ public function __construct($config = [])
3470
*/
3571
public function configureServiceManager(ServiceManager $serviceManager)
3672
{
37-
$config = [];
38-
$abstractFactories = isset($this->config['abstract_factories']) ? $this->config['abstract_factories'] : [];
39-
$aliases = isset($this->config['aliases']) ? $this->config['aliases'] : [];
40-
$delegators = isset($this->config['delegators']) ? $this->config['delegators'] : [];
41-
$factories = isset($this->config['factories']) ? $this->config['factories'] : [];
42-
$initializers = isset($this->config['initializers']) ? $this->config['initializers'] : [];
43-
$invokables = isset($this->config['invokables']) ? $this->config['invokables'] : [];
44-
$lazyServices = isset($this->config['lazy_services']) ? $this->config['lazy_services'] : [];
45-
$services = isset($this->config['services']) ? $this->config['services'] : [];
46-
$shared = isset($this->config['shared']) ? $this->config['shared'] : [];
47-
48-
if (! empty($abstractFactories)) {
49-
$config['abstract_factories'] = $abstractFactories;
50-
}
51-
52-
if (! empty($aliases)) {
53-
$config['aliases'] = $aliases;
54-
}
55-
56-
if (! empty($delegators)) {
57-
$config['delegators'] = $delegators;
58-
}
59-
60-
if (! empty($factories)) {
61-
$config['factories'] = $factories;
62-
}
63-
64-
if (! empty($initializers)) {
65-
$config['initializers'] = $initializers;
66-
}
67-
68-
if (! empty($invokables)) {
69-
$config['invokables'] = $invokables;
70-
}
71-
72-
if (! empty($lazyServices)) {
73-
$config['lazy_services'] = $lazyServices;
74-
}
75-
76-
if (! empty($services)) {
77-
$config['services'] = $services;
78-
}
79-
80-
if (! empty($shared)) {
81-
$config['shared'] = $shared;
82-
}
83-
84-
return $serviceManager->withConfig($config);
73+
return $serviceManager->withConfig($this->config);
8574
}
8675
}

0 commit comments

Comments
 (0)