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 $yaml |
24
|
|
|
* |
25
|
|
|
* @return Definition |
26
|
|
|
* |
27
|
|
|
* @throws InvalidDefinitionException |
28
|
|
|
*/ |
29
|
20 |
|
public static function fromYaml(array $yaml) |
30
|
|
|
{ |
31
|
20 |
|
if (!isset($yaml['class']) && !isset($yaml['factory'])) { |
32
|
1 |
|
throw new InvalidDefinitionException("Missing required 'class' argument. Must be a FQCN."); |
33
|
|
|
} |
34
|
|
|
|
35
|
19 |
|
$definition = new self(); |
36
|
|
|
|
37
|
19 |
|
if (isset($yaml['class'])) { |
38
|
19 |
|
$definition->setClass($yaml['class']); |
39
|
|
|
} |
40
|
|
|
|
41
|
19 |
|
if (isset($yaml['factory'])) { |
42
|
|
|
$definition->setFactory($yaml['factory']); |
43
|
|
|
} |
44
|
|
|
|
45
|
19 |
|
$definition->setLazy(isset($yaml['lazy']) ? $yaml['lazy'] : false); |
46
|
19 |
|
$definition->setPublic(isset($yaml['public']) ? $yaml['public'] : true); |
47
|
|
|
|
48
|
19 |
|
if (isset($yaml['arguments'])) { |
49
|
19 |
|
$definition->setArguments($yaml['arguments']); |
50
|
|
|
} |
51
|
|
|
|
52
|
19 |
|
if (isset($yaml['calls'])) { |
53
|
19 |
|
foreach ($yaml['calls'] as $call) { |
54
|
19 |
|
$methodCall = MethodCall::fromYaml($call); |
55
|
19 |
|
$definition->addMethodCall($methodCall); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
19 |
|
return $definition; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
19 |
|
public function getClass() |
66
|
|
|
{ |
67
|
19 |
|
return $this->class; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $class |
72
|
|
|
* |
73
|
|
|
* @return Definition |
74
|
|
|
*/ |
75
|
19 |
|
public function setClass($class) |
76
|
|
|
{ |
77
|
19 |
|
$this->class = $class; |
78
|
|
|
|
79
|
19 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
19 |
|
public function isLazy() |
86
|
|
|
{ |
87
|
19 |
|
return $this->lazy; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param bool $lazy |
92
|
|
|
* |
93
|
|
|
* @return Definition |
94
|
|
|
*/ |
95
|
19 |
|
public function setLazy($lazy) |
96
|
|
|
{ |
97
|
19 |
|
$this->lazy = (bool)$lazy; |
98
|
|
|
|
99
|
19 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
19 |
|
public function isPublic() |
106
|
|
|
{ |
107
|
19 |
|
return $this->public; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param bool $public |
112
|
|
|
*/ |
113
|
19 |
|
public function setPublic($public) |
114
|
|
|
{ |
115
|
19 |
|
$this->public = $public; |
116
|
19 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
19 |
|
public function getArguments() |
122
|
|
|
{ |
123
|
19 |
|
return $this->arguments; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $arguments |
128
|
|
|
* |
129
|
|
|
* @return Definition |
130
|
|
|
*/ |
131
|
19 |
|
public function setArguments(array $arguments) |
132
|
|
|
{ |
133
|
19 |
|
$this->arguments = $arguments; |
134
|
|
|
|
135
|
19 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param MethodCall $methodCall |
140
|
|
|
* |
141
|
|
|
* @return Definition |
142
|
|
|
*/ |
143
|
19 |
|
public function addMethodCall(MethodCall $methodCall) |
144
|
|
|
{ |
145
|
19 |
|
$this->methodCalls[] = $methodCall; |
146
|
|
|
|
147
|
19 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return MethodCall[] |
152
|
|
|
*/ |
153
|
19 |
|
public function getMethodCalls() |
154
|
|
|
{ |
155
|
19 |
|
return $this->methodCalls; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param MethodCall[] $methodCalls |
160
|
|
|
* |
161
|
|
|
* @return Definition |
162
|
|
|
*/ |
163
|
|
|
public function setMethodCalls(array $methodCalls) |
164
|
|
|
{ |
165
|
|
|
foreach ($methodCalls as $methodCall) { |
166
|
|
|
$this->addMethodCall($methodCall); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
19 |
|
public function getFactory() |
176
|
|
|
{ |
177
|
19 |
|
return $this->factory; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array|string $factory |
182
|
|
|
* |
183
|
|
|
* @return Definition |
184
|
|
|
*/ |
185
|
|
|
public function setFactory($factory) |
186
|
|
|
{ |
187
|
|
|
if (is_string($factory)) { |
188
|
|
|
$factory = explode(':', $factory, 2); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->factory = $factory; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|