1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Container\Definition; |
4
|
|
|
|
5
|
|
|
use Palmtree\Container\Exception\InvalidDefinitionException; |
6
|
|
|
|
7
|
|
|
class Definition |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
private $class; |
11
|
|
|
/** @var bool */ |
12
|
|
|
private $lazy = false; |
13
|
|
|
/** @var bool */ |
14
|
|
|
private $public = true; |
15
|
|
|
/** @var array */ |
16
|
|
|
private $arguments = []; |
17
|
|
|
/** @var MethodCall[] */ |
18
|
|
|
private $methodCalls = []; |
19
|
|
|
/** @var array */ |
20
|
|
|
private $factory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array|null $yaml |
24
|
|
|
* |
25
|
|
|
* @return Definition |
26
|
|
|
* |
27
|
|
|
* @throws InvalidDefinitionException |
28
|
|
|
*/ |
29
|
25 |
|
public static function fromYaml($yaml, ?string $key = null): self |
30
|
|
|
{ |
31
|
25 |
|
if (!isset($yaml['class']) && !isset($yaml['factory'])) { |
32
|
24 |
|
if ($key !== null) { |
33
|
23 |
|
$yaml['class'] = $key; |
34
|
|
|
} else { |
35
|
1 |
|
throw new InvalidDefinitionException("Missing required 'class' argument. Must be a FQCN."); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
24 |
|
$definition = new self(); |
40
|
|
|
|
41
|
24 |
|
$definition->setClass($yaml['class'] ?? null) |
42
|
24 |
|
->setFactory($yaml['factory'] ?? []) |
43
|
24 |
|
->setLazy($yaml['lazy'] ?? false) |
44
|
24 |
|
->setPublic($yaml['public'] ?? true) |
45
|
24 |
|
->setArguments($yaml['arguments'] ?? []); |
46
|
|
|
|
47
|
24 |
|
foreach ($yaml['calls'] ?? [] as $call) { |
48
|
23 |
|
$methodCall = MethodCall::fromYaml($call); |
49
|
23 |
|
$definition->addMethodCall($methodCall); |
50
|
|
|
} |
51
|
|
|
|
52
|
24 |
|
return $definition; |
53
|
|
|
} |
54
|
|
|
|
55
|
25 |
|
public function getClass(): string |
56
|
|
|
{ |
57
|
25 |
|
return $this->class; |
58
|
|
|
} |
59
|
|
|
|
60
|
25 |
|
public function setClass(?string $class): self |
61
|
|
|
{ |
62
|
25 |
|
$this->class = $class; |
63
|
|
|
|
64
|
25 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
24 |
|
public function isLazy(): bool |
68
|
|
|
{ |
69
|
24 |
|
return $this->lazy; |
70
|
|
|
} |
71
|
|
|
|
72
|
24 |
|
public function setLazy(bool $lazy): self |
73
|
|
|
{ |
74
|
24 |
|
$this->lazy = $lazy; |
75
|
|
|
|
76
|
24 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
25 |
|
public function isPublic(): bool |
80
|
|
|
{ |
81
|
25 |
|
return $this->public; |
82
|
|
|
} |
83
|
|
|
|
84
|
24 |
|
public function setPublic(bool $public): self |
85
|
|
|
{ |
86
|
24 |
|
$this->public = $public; |
87
|
|
|
|
88
|
24 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
25 |
|
public function getArguments(): ?array |
92
|
|
|
{ |
93
|
25 |
|
return $this->arguments; |
94
|
|
|
} |
95
|
|
|
|
96
|
25 |
|
public function setArguments(array $arguments): self |
97
|
|
|
{ |
98
|
25 |
|
$this->arguments = $arguments; |
99
|
|
|
|
100
|
25 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
24 |
|
public function addMethodCall(MethodCall $methodCall): self |
104
|
|
|
{ |
105
|
24 |
|
$this->methodCalls[] = $methodCall; |
106
|
|
|
|
107
|
24 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return MethodCall[] |
112
|
|
|
*/ |
113
|
25 |
|
public function getMethodCalls(): array |
114
|
|
|
{ |
115
|
25 |
|
return $this->methodCalls; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param MethodCall[] $methodCalls |
120
|
|
|
* |
121
|
|
|
* @return Definition |
122
|
|
|
*/ |
123
|
1 |
|
public function setMethodCalls(array $methodCalls): self |
124
|
|
|
{ |
125
|
1 |
|
foreach ($methodCalls as $methodCall) { |
126
|
1 |
|
$this->addMethodCall($methodCall); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
25 |
|
public function getFactory(): ?array |
133
|
|
|
{ |
134
|
25 |
|
return $this->factory; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array|string $factory |
139
|
|
|
* |
140
|
|
|
* @return Definition |
141
|
|
|
*/ |
142
|
24 |
|
public function setFactory($factory): self |
143
|
|
|
{ |
144
|
24 |
|
if (\is_string($factory)) { |
145
|
23 |
|
$factory = explode(':', $factory, 2); |
146
|
|
|
} |
147
|
|
|
|
148
|
24 |
|
$this->factory = $factory; |
149
|
|
|
|
150
|
24 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|