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