1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Definitions; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Definitions\Contract\DefinitionInterface; |
9
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Definitions\Exception\NotFoundException; |
11
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
12
|
|
|
use Yiisoft\Definitions\Infrastructure\ArrayDefinitionBuilder; |
13
|
|
|
|
14
|
|
|
use function count; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Builds object by array config |
18
|
|
|
* |
19
|
|
|
* @psalm-type MethodOrPropertyItem = array{0:string,1:string,2:mixed} |
20
|
|
|
* @psalm-type ArrayDefinitionConfig = array{class:class-string,'__construct()'?:array}&array<string, mixed> |
21
|
|
|
*/ |
22
|
|
|
final class ArrayDefinition implements DefinitionInterface |
23
|
|
|
{ |
24
|
|
|
public const CLASS_NAME = 'class'; |
25
|
|
|
public const CONSTRUCTOR = '__construct()'; |
26
|
|
|
|
27
|
|
|
public const TYPE_PROPERTY = 'property'; |
28
|
|
|
public const TYPE_METHOD = 'method'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @psalm-var class-string |
32
|
|
|
*/ |
33
|
|
|
private string $class; |
34
|
|
|
private array $constructorArguments; |
35
|
|
|
private ?ContainerInterface $referenceContainer = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @psalm-var array<string, MethodOrPropertyItem> |
39
|
|
|
*/ |
40
|
|
|
private array $methodsAndProperties; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @psalm-param class-string $class |
44
|
|
|
* @psalm-param array<string, MethodOrPropertyItem> $methodsAndProperties |
45
|
|
|
*/ |
46
|
23 |
|
private function __construct(string $class, array $constructorArguments, array $methodsAndProperties) |
47
|
|
|
{ |
48
|
23 |
|
$this->class = $class; |
49
|
23 |
|
$this->constructorArguments = $constructorArguments; |
50
|
23 |
|
$this->methodsAndProperties = $methodsAndProperties; |
51
|
23 |
|
} |
52
|
|
|
|
53
|
|
|
public function setReferenceContainer(?ContainerInterface $referenceContainer): void |
54
|
|
|
{ |
55
|
|
|
$this->referenceContainer = $referenceContainer; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @psalm-param ArrayDefinitionConfig $config |
60
|
|
|
*/ |
61
|
21 |
|
public static function fromConfig(array $config): self |
62
|
|
|
{ |
63
|
21 |
|
return new self( |
64
|
21 |
|
$config[self::CLASS_NAME], |
65
|
21 |
|
$config[self::CONSTRUCTOR] ?? [], |
66
|
21 |
|
self::getMethodsAndPropertiesFromConfig($config) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @psalm-param class-string $class |
72
|
|
|
* @psalm-param array<string, MethodOrPropertyItem> $methodsAndProperties |
73
|
|
|
*/ |
74
|
2 |
|
public static function fromPreparedData(string $class, array $constructorArguments = [], array $methodsAndProperties = []): self |
75
|
|
|
{ |
76
|
2 |
|
return new self($class, $constructorArguments, $methodsAndProperties); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @psalm-param array<string, mixed> $config |
81
|
|
|
* |
82
|
|
|
* @psalm-return array<string, MethodOrPropertyItem> |
83
|
|
|
*/ |
84
|
21 |
|
private static function getMethodsAndPropertiesFromConfig(array $config): array |
85
|
|
|
{ |
86
|
21 |
|
$methodsAndProperties = []; |
87
|
|
|
|
88
|
|
|
/** @var mixed $value */ |
89
|
21 |
|
foreach ($config as $key => $value) { |
90
|
21 |
|
if ($key === self::CONSTRUCTOR) { |
91
|
9 |
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
21 |
|
if (count($methodArray = explode('()', $key)) === 2) { |
95
|
7 |
|
$methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value]; |
96
|
21 |
|
} elseif (count($propertyArray = explode('$', $key)) === 2) { |
97
|
3 |
|
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
21 |
|
return $methodsAndProperties; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @psalm-return class-string |
106
|
|
|
*/ |
107
|
22 |
|
public function getClass(): string |
108
|
|
|
{ |
109
|
22 |
|
return $this->class; |
110
|
|
|
} |
111
|
|
|
|
112
|
22 |
|
public function getConstructorArguments(): array |
113
|
|
|
{ |
114
|
22 |
|
return $this->constructorArguments; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @psalm-return array<string, MethodOrPropertyItem> |
119
|
|
|
*/ |
120
|
21 |
|
public function getMethodsAndProperties(): array |
121
|
|
|
{ |
122
|
21 |
|
return $this->methodsAndProperties; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws NotFoundException |
127
|
|
|
* @throws NotInstantiableException |
128
|
|
|
* @throws InvalidConfigException |
129
|
|
|
*/ |
130
|
19 |
|
public function resolve(ContainerInterface $container): object |
131
|
|
|
{ |
132
|
19 |
|
return ArrayDefinitionBuilder::getInstance()->build($container, $this->referenceContainer, $this); |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
public function merge(self $other): self |
136
|
|
|
{ |
137
|
2 |
|
$new = clone $this; |
138
|
2 |
|
$new->class = $other->class; |
139
|
2 |
|
$new->constructorArguments = $this->mergeArguments($this->constructorArguments, $other->constructorArguments); |
140
|
|
|
|
141
|
2 |
|
$methodsAndProperties = $this->methodsAndProperties; |
142
|
2 |
|
foreach ($other->methodsAndProperties as $key => $item) { |
143
|
1 |
|
if ($item[0] === self::TYPE_PROPERTY) { |
144
|
1 |
|
$methodsAndProperties[$key] = $item; |
145
|
1 |
|
} elseif ($item[0] === self::TYPE_METHOD) { |
146
|
|
|
/** @psalm-suppress MixedArgument, MixedAssignment */ |
147
|
1 |
|
$arguments = isset($methodsAndProperties[$key]) |
148
|
1 |
|
? $this->mergeArguments($methodsAndProperties[$key][2], $item[2]) |
149
|
1 |
|
: $item[2]; |
150
|
1 |
|
$methodsAndProperties[$key] = [$item[0], $item[1], $arguments]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
2 |
|
$new->methodsAndProperties = $methodsAndProperties; |
154
|
|
|
|
155
|
2 |
|
return $new; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
private function mergeArguments(array $selfArguments, array $otherArguments): array |
159
|
|
|
{ |
160
|
|
|
/** @var mixed $argument */ |
161
|
2 |
|
foreach ($otherArguments as $name => $argument) { |
162
|
|
|
/** @var mixed */ |
163
|
1 |
|
$selfArguments[$name] = $argument; |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
return $selfArguments; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|