|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Factory\Definition; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
|
8
|
|
|
|
|
9
|
|
|
use function array_key_exists; |
|
10
|
|
|
use function is_array; |
|
11
|
|
|
use function is_callable; |
|
12
|
|
|
use function is_object; |
|
13
|
|
|
use function is_string; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Normalizer definition from configuration to an instance of {@see DefinitionInterface}. |
|
17
|
|
|
* |
|
18
|
|
|
* @psalm-import-type ArrayDefinitionConfig from ArrayDefinition |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Normalizer |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Normalize definition to an instance of {@see DefinitionInterface}. |
|
24
|
|
|
* Definition may be defined multiple ways: |
|
25
|
|
|
* - class name, |
|
26
|
|
|
* - string as reference to another class or alias, |
|
27
|
|
|
* - instance of {@see ReferenceInterface}, |
|
28
|
|
|
* - callable, |
|
29
|
|
|
* - array, |
|
30
|
|
|
* - ready object. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $definition The definition for normalization. |
|
33
|
|
|
* @param string $class The class name of the object to be defined (optional). It is used in two cases. |
|
34
|
|
|
* - The definition is a string, and class name equals to definition. Returned `ArrayDefinition` with defined |
|
35
|
|
|
* class. |
|
36
|
|
|
* - The definition is an array without class name. Class name will be added to array and `ArrayDefinition` |
|
37
|
|
|
* will be returned. |
|
38
|
|
|
* |
|
39
|
|
|
* @throws InvalidConfigException If configuration is not valid. |
|
40
|
|
|
* |
|
41
|
|
|
* @return DefinitionInterface Normalized definition as an object. |
|
42
|
|
|
*/ |
|
43
|
74 |
|
public static function normalize($definition, string $class = null): DefinitionInterface |
|
44
|
|
|
{ |
|
45
|
|
|
// Reference |
|
46
|
74 |
|
if ($definition instanceof ReferenceInterface) { |
|
47
|
4 |
|
return $definition; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
73 |
|
if (is_string($definition)) { |
|
51
|
|
|
// Current class |
|
52
|
|
|
if ( |
|
53
|
34 |
|
$class === $definition || |
|
54
|
34 |
|
($class === null && class_exists($definition)) |
|
55
|
|
|
) { |
|
56
|
|
|
/** @psalm-var class-string $definition */ |
|
57
|
12 |
|
return ArrayDefinition::fromPreparedData($definition); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Reference to another class or alias |
|
61
|
23 |
|
return Reference::to($definition); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Callable definition |
|
65
|
57 |
|
if (is_callable($definition, true)) { |
|
66
|
13 |
|
return new CallableDefinition($definition); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Array definition |
|
70
|
49 |
|
if (is_array($definition)) { |
|
71
|
39 |
|
$config = $definition; |
|
72
|
39 |
|
if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) { |
|
73
|
3 |
|
if ($class === null) { |
|
74
|
1 |
|
throw new InvalidConfigException('Array definition should contain the key "class": ' . var_export($definition, true)); |
|
75
|
|
|
} |
|
76
|
2 |
|
$config[ArrayDefinition::CLASS_NAME] = $class; |
|
77
|
|
|
} |
|
78
|
|
|
/** @psalm-var ArrayDefinitionConfig $config */ |
|
79
|
38 |
|
return ArrayDefinition::fromConfig($config); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Ready object |
|
83
|
11 |
|
if (is_object($definition) && !($definition instanceof DefinitionInterface)) { |
|
84
|
8 |
|
return new ValueDefinition($definition); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
throw new InvalidConfigException('Invalid definition: ' . var_export($definition, true)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|