1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2014-05-14 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\CodeGenerator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FileGenerator |
11
|
|
|
* @package Net\Bazzline\Component\CodeGenerator |
12
|
|
|
*/ |
13
|
|
|
class FileGenerator extends AbstractDocumentedGenerator |
14
|
|
|
{ |
15
|
|
|
/** @var boolean */ |
16
|
|
|
private $addEmptyLine; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ConstantGenerator $constant |
20
|
|
|
* @return $this |
21
|
|
|
*/ |
22
|
|
|
public function addConstant(ConstantGenerator $constant) |
23
|
|
|
{ |
24
|
|
|
$this->addGeneratorProperty('constants', $constant); |
25
|
|
|
|
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param PropertyGenerator $property |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function addProperty(PropertyGenerator $property) |
34
|
|
|
{ |
35
|
|
|
$this->addGeneratorProperty('properties', $property); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ClassGenerator $class |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function addClass(ClassGenerator $class) |
45
|
|
|
{ |
46
|
|
|
$this->addGeneratorProperty('classes', $class); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param InterfaceGenerator $interface |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function addInterface(InterfaceGenerator $interface) |
56
|
|
|
{ |
57
|
|
|
$this->addGeneratorProperty('interfaces', $interface); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param MethodGenerator $method |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function addMethod(MethodGenerator $method) |
67
|
|
|
{ |
68
|
|
|
$this->addGeneratorProperty('methods', $method); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param TraitGenerator $trait |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function addTrait(TraitGenerator $trait) |
78
|
|
|
{ |
79
|
|
|
$this->addGeneratorProperty('traits', $trait->getName()); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function markAsExecutable() |
85
|
|
|
{ |
86
|
|
|
$this->addGeneratorProperty('is_executable', true, false); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int|string|array $content |
93
|
|
|
* @return $this |
94
|
|
|
* @todo add support for Generator as type |
95
|
|
|
*/ |
96
|
|
|
public function addFileContent($content) |
97
|
|
|
{ |
98
|
|
|
if (!is_array($content)) { |
99
|
|
|
$content = [ |
100
|
|
|
$content |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ($content as $partial) { |
105
|
|
|
$this->addGeneratorProperty('content', $partial); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @throws InvalidArgumentException|RuntimeException |
113
|
|
|
* @return string |
114
|
|
|
* @todo implement exception throwing if mandatory parameter is missing |
115
|
|
|
*/ |
116
|
|
|
public function generate() |
117
|
|
|
{ |
118
|
|
|
if ($this->canBeGenerated()) { |
119
|
|
|
$this->resetContent(); |
120
|
|
|
$this->addEmptyLine = false; |
121
|
|
|
$isExecutable = $this->getGeneratorProperty('is_executable', false); |
122
|
|
|
if ($isExecutable) { |
123
|
|
|
$this->addContent('#!/bin/php'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->addContent('<?php'); |
127
|
|
|
$this->generateContent(); |
128
|
|
|
$this->generateConstants(); |
129
|
|
|
$this->generateProperties(); |
130
|
|
|
$this->generateTraits(); |
131
|
|
|
$this->generateMethods(); |
132
|
|
|
$this->generateInterfaces(); |
133
|
|
|
$this->generateClasses(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->generateStringFromContent(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function generateContent() |
140
|
|
|
{ |
141
|
|
|
$content = $this->getGeneratorProperty('content', []); |
142
|
|
|
|
143
|
|
|
foreach ($content as $partial) { |
|
|
|
|
144
|
|
|
$this->addContent($partial); |
145
|
|
|
} |
146
|
|
|
$this->addEmptyLine = true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function generateConstants() |
150
|
|
|
{ |
151
|
|
|
/** @var null|ConstantGenerator[] $constants */ |
152
|
|
|
$constants = $this->getGeneratorProperty('constants'); |
153
|
|
|
|
154
|
|
|
$this->addContentFromCollectionTwo($constants); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function generateProperties() |
158
|
|
|
{ |
159
|
|
|
/** @var null|PropertyGenerator[] $properties */ |
160
|
|
|
$properties = $this->getGeneratorProperty('properties'); |
161
|
|
|
|
162
|
|
|
$this->addContentFromCollectionTwo($properties); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function generateTraits() |
166
|
|
|
{ |
167
|
|
|
/** @var null|array $traits */ |
168
|
|
|
$traits = $this->getGeneratorProperty('traits'); |
169
|
|
|
|
170
|
|
|
$this->addContentFromCollectionTwo($traits); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function generateMethods() |
174
|
|
|
{ |
175
|
|
|
/** @var null|MethodGenerator[] $methods */ |
176
|
|
|
$methods = $this->getGeneratorProperty('methods'); |
177
|
|
|
|
178
|
|
|
$this->addContentFromCollectionTwo($methods); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function generateClasses() |
182
|
|
|
{ |
183
|
|
|
/** @var null|ClassGenerator[] $classes */ |
184
|
|
|
$classes = $this->getGeneratorProperty('classes'); |
185
|
|
|
|
186
|
|
|
$this->addContentFromCollection($classes); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function generateInterfaces() |
190
|
|
|
{ |
191
|
|
|
/** @var null|InterfaceGenerator[] $interfaces */ |
192
|
|
|
$interfaces = $this->getGeneratorProperty('interfaces'); |
193
|
|
|
|
194
|
|
|
$this->addContentFromCollection($interfaces); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param null|array $collection |
199
|
|
|
* @todo figure out difference between addContentFromCollection and |
200
|
|
|
* addContentFromCollectionTwo and express difference with fitting names |
201
|
|
|
*/ |
202
|
|
|
private function addContentFromCollection($collection) |
203
|
|
|
{ |
204
|
|
|
if (is_array($collection)) { |
205
|
|
|
if ($this->addEmptyLine) { |
206
|
|
|
$this->addContent(''); |
207
|
|
|
} |
208
|
|
|
$keys = array_keys($collection); |
209
|
|
|
$lastKey = array_pop($keys); |
210
|
|
|
|
211
|
|
|
foreach($collection as $key => $item) { |
212
|
|
|
$this->addGeneratorAsContent($item); |
213
|
|
|
if ($key !== $lastKey) { |
214
|
|
|
$this->addContent(''); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param null|array $collection |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
private function addContentFromCollectionTwo($collection) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
if (is_array($collection)) { |
226
|
|
|
if ($this->addEmptyLine) { |
227
|
|
|
$this->addContent(''); |
228
|
|
|
} |
229
|
|
|
$lastKey = $this->getLastArrayKey($collection); |
230
|
|
|
foreach($collection as $key => $item) { |
231
|
|
|
$this->addGeneratorAsContent($item); |
232
|
|
|
if ($key !== $lastKey) { |
233
|
|
|
$this->addContent(''); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
$this->addEmptyLine = true; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.