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