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
|
|
|
* Class Definition represents a definition in a container |
17
|
|
|
* |
18
|
|
|
* @psalm-import-type ArrayDefinitionConfig from ArrayDefinition |
19
|
|
|
*/ |
20
|
|
|
class Normalizer |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Definition may be defined multiple ways. |
24
|
|
|
* Interface name as string: |
25
|
|
|
* |
26
|
|
|
* ```php |
27
|
|
|
* $container->set('interface_name', EngineInterface::class); |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* A closure: |
31
|
|
|
* |
32
|
|
|
* ```php |
33
|
|
|
* $container->set('closure', function($container) { |
34
|
|
|
* return new MyClass($container->get('db')); |
35
|
|
|
* }); |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* A callable array: |
39
|
|
|
* |
40
|
|
|
* ```php |
41
|
|
|
* $container->set('static_call', [MyClass::class, 'create']); |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* A definition array: |
45
|
|
|
* |
46
|
|
|
* ```php |
47
|
|
|
* $container->set('full_definition', [ |
48
|
|
|
* 'class' => EngineMarkOne::class, |
49
|
|
|
* '__construct()' => [42], |
50
|
|
|
* '$argName' => 'value', |
51
|
|
|
* 'setX()' => [42], |
52
|
|
|
* ]); |
53
|
|
|
* ``` |
54
|
|
|
* |
55
|
|
|
* @param mixed $definition |
56
|
|
|
* |
57
|
|
|
* @throws InvalidConfigException |
58
|
|
|
*/ |
59
|
34 |
|
public static function normalize($definition, string $id = null): DefinitionInterface |
60
|
|
|
{ |
61
|
|
|
// Reference |
62
|
34 |
|
if ($definition instanceof ReferenceInterface) { |
63
|
1 |
|
return $definition; |
64
|
|
|
} |
65
|
|
|
|
66
|
34 |
|
if (is_string($definition)) { |
67
|
|
|
// Current class |
68
|
|
|
if ( |
69
|
14 |
|
$id === $definition || |
70
|
14 |
|
($id === null && class_exists($definition)) |
71
|
|
|
) { |
72
|
|
|
/** @psalm-var class-string $definition */ |
73
|
6 |
|
return ArrayDefinition::fromPreparedData($definition); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Reference to another class or alias |
77
|
8 |
|
return Reference::to($definition); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Callable definition |
81
|
24 |
|
if (is_callable($definition, true)) { |
82
|
4 |
|
return new CallableDefinition($definition); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Array definition |
86
|
20 |
|
if (is_array($definition)) { |
87
|
18 |
|
$config = $definition; |
88
|
18 |
|
if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) { |
89
|
1 |
|
$config[ArrayDefinition::CLASS_NAME] = $id; |
90
|
|
|
} |
91
|
|
|
/** @psalm-var ArrayDefinitionConfig $config */ |
92
|
18 |
|
return ArrayDefinition::fromConfig($config); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Ready object |
96
|
2 |
|
if (is_object($definition)) { |
97
|
1 |
|
return new ValueDefinition($definition); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
throw new InvalidConfigException('Invalid definition:' . var_export($definition, true)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|