-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
LegacyConfig.php
95 lines (80 loc) · 2.83 KB
/
LegacyConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* @see https://github.com/zendframework/zend-di for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-di/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Di;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\Parameters;
/**
* Provides a migration config from zend-di 2.x configuration arrays
*/
class LegacyConfig extends Config
{
public function __construct($config)
{
parent::__construct([]);
if ($config instanceof Traversable) {
$config = ArrayUtils::iteratorToArray($config);
}
if (! is_array($config)) {
throw new Exception\InvalidArgumentException('Config data must be an array or implement Traversable');
}
if (isset($config['instance'])) {
$this->configureInstance($config['instance']);
}
}
private function prepareParametersArray($parameters, string $class)
{
$prepared = [];
foreach ($parameters as $key => $value) {
if (strpos($key, ':') !== false) {
trigger_error('Full qualified parameter positions are no longer supported', E_USER_DEPRECATED);
}
$prepared[$key] = $value;
}
return $prepared;
}
private function configureInstance($config)
{
foreach ($config as $target => $data) {
switch ($target) {
case 'aliases':
case 'alias':
foreach ($data as $name => $class) {
if (class_exists($class) || interface_exists($class)) {
$this->setAlias($name, $class);
}
}
break;
case 'preferences':
case 'preference':
foreach ($data as $type => $pref) {
$preference = is_array($pref) ? array_pop($pref) : $pref;
$this->setTypePreference($type, $preference);
}
break;
default:
$config = new Parameters($data);
$parameters = $config->get('parameters', $config->get('parameter'));
if (is_array($parameters) || ($parameters instanceof Traversable)) {
$parameters = $this->prepareParametersArray($parameters, $target);
$this->setParameters($target, $parameters);
}
break;
}
}
}
/**
* Export the configuraton to an array
*/
public function toArray() : array
{
return [
'preferences' => $this->preferences,
'types' => $this->types,
];
}
}