1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace gossi\codegen\model; |
18
|
|
|
|
19
|
|
|
use gossi\docblock\Docblock; |
20
|
|
|
use gossi\codegen\model\parts\QualifiedNameTrait; |
21
|
|
|
use gossi\codegen\model\parts\DocblockTrait; |
22
|
|
|
use gossi\codegen\model\parts\LongDescriptionTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Represents an abstract php structure (class, trait or interface). |
26
|
|
|
* |
27
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractPhpStruct extends AbstractModel implements NamespaceInterface, DocblockInterface { |
30
|
|
|
|
31
|
|
|
use QualifiedNameTrait; |
32
|
|
|
use DocblockTrait; |
33
|
|
|
use LongDescriptionTrait; |
34
|
|
|
|
35
|
|
|
protected static $phpParser; |
36
|
|
|
|
37
|
|
|
private $useStatements = []; |
38
|
|
|
|
39
|
|
|
private $requiredFiles = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @var PhpMethod[] |
44
|
|
|
*/ |
45
|
|
|
private $methods = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates a new struct |
49
|
|
|
* |
50
|
|
|
* @param string $name the fqcn |
51
|
|
|
* @return static |
52
|
|
|
*/ |
53
|
10 |
|
public static function create($name = null) { |
54
|
10 |
|
return new static($name); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
* @return PhpMethod |
60
|
|
|
*/ |
61
|
5 |
|
protected static function createMethod(\ReflectionMethod $method) { |
62
|
5 |
|
return PhpMethod::fromReflection($method); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* @return PhpProperty |
68
|
|
|
*/ |
69
|
4 |
|
protected static function createProperty(\ReflectionProperty $property) { |
70
|
4 |
|
return PhpProperty::fromReflection($property); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Creates a new struct |
75
|
|
|
* |
76
|
|
|
* @param string $name the fqcn |
77
|
|
|
*/ |
78
|
51 |
|
public function __construct($name = null) { |
79
|
51 |
|
$this->setQualifiedName($name); |
80
|
51 |
|
$this->docblock = new Docblock(); |
81
|
51 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets requried files |
85
|
|
|
* |
86
|
|
|
* @param array $files |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
1 |
|
public function setRequiredFiles(array $files) { |
90
|
1 |
|
$this->requiredFiles = $files; |
91
|
|
|
|
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Adds a new required file |
97
|
|
|
* |
98
|
|
|
* @param string $file |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
1 |
|
public function addRequiredFile($file) { |
102
|
1 |
|
$this->requiredFiles[] = $file; |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns required files |
109
|
|
|
* |
110
|
|
|
* @return array collection of filenames |
111
|
|
|
*/ |
112
|
14 |
|
public function getRequiredFiles() { |
113
|
14 |
|
return $this->requiredFiles; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets use statements |
118
|
|
|
* |
119
|
|
|
* @see #addUseStatement |
120
|
|
|
* @see #declareUses() |
121
|
|
|
* @param array $useStatements |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
19 |
|
public function setUseStatements(array $useStatements) { |
125
|
19 |
|
$this->useStatements = []; |
126
|
19 |
|
foreach ($useStatements as $alias => $useStatement) { |
127
|
6 |
|
$this->addUseStatement($useStatement, $alias); |
128
|
19 |
|
} |
129
|
|
|
|
130
|
19 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Adds a use statement with an optional alias |
135
|
|
|
* |
136
|
|
|
* @param string $qualifiedName |
137
|
|
|
* @param null|string $alias |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
11 |
|
public function addUseStatement($qualifiedName, $alias = null) { |
141
|
11 |
|
if (!is_string($alias)) { |
142
|
9 |
|
if (false === strpos($qualifiedName, '\\')) { |
143
|
1 |
|
$alias = $qualifiedName; |
144
|
1 |
|
} else { |
145
|
8 |
|
$alias = substr($qualifiedName, strrpos($qualifiedName, '\\') + 1); |
146
|
|
|
} |
147
|
9 |
|
} |
148
|
|
|
|
149
|
11 |
|
$this->useStatements[$alias] = $qualifiedName; |
150
|
|
|
|
151
|
11 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Declares multiple use statements at once. |
156
|
|
|
* |
157
|
|
|
* @param ... use statements multiple qualified names |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
1 |
|
public function declareUses() { |
161
|
1 |
|
foreach (func_get_args() as $name) { |
162
|
1 |
|
$this->declareUse($name); |
163
|
1 |
|
} |
164
|
1 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Declares a "use $fullClassName" with " as $alias" if $alias is available, |
169
|
|
|
* and returns its alias (or not qualified classname) to be used in your actual |
170
|
|
|
* php code. |
171
|
|
|
* |
172
|
|
|
* If the class has already been declared you get only the set alias. |
173
|
|
|
* |
174
|
|
|
* @param string $qualifiedName |
175
|
|
|
* @param null|string $alias |
176
|
|
|
* @return string the used alias |
177
|
|
|
*/ |
178
|
1 |
|
public function declareUse($qualifiedName, $alias = null) { |
179
|
1 |
|
$qualifiedName = trim($qualifiedName, '\\'); |
180
|
1 |
|
if (!$this->hasUseStatement($qualifiedName)) { |
181
|
1 |
|
$this->addUseStatement($qualifiedName, $alias); |
182
|
1 |
|
} |
183
|
1 |
|
return $this->getUseAlias($qualifiedName); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns whether the given use statement is present |
188
|
|
|
* |
189
|
|
|
* @param string $qualifiedName |
190
|
|
|
* @return boolean |
191
|
|
|
*/ |
192
|
3 |
|
public function hasUseStatement($qualifiedName) { |
193
|
3 |
|
$flipped = array_flip($this->useStatements); |
194
|
3 |
|
return isset($flipped[$qualifiedName]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the usable alias for a qualified name |
199
|
|
|
* |
200
|
|
|
* @param string $qualifiedName |
201
|
|
|
* @return string the alias |
202
|
|
|
*/ |
203
|
2 |
|
public function getUseAlias($qualifiedName) { |
204
|
2 |
|
$flipped = array_flip($this->useStatements); |
205
|
2 |
|
return $flipped[$qualifiedName]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Removes a use statement |
210
|
|
|
* |
211
|
|
|
* @param string $qualifiedName |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
3 |
|
public function removeUseStatement($qualifiedName) { |
215
|
3 |
|
$offset = array_search($qualifiedName, $this->useStatements); |
216
|
3 |
|
if ($offset !== null) { |
217
|
3 |
|
unset($this->useStatements[$offset]); |
218
|
3 |
|
} |
219
|
3 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns all use statements |
224
|
|
|
* |
225
|
|
|
* @return array collection of use statements |
226
|
|
|
*/ |
227
|
15 |
|
public function getUseStatements() { |
228
|
15 |
|
return $this->useStatements; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Sets a collection of methods |
233
|
|
|
* |
234
|
|
|
* @param PhpMethod[] $methods |
235
|
|
|
* @return $this |
236
|
|
|
*/ |
237
|
1 |
|
public function setMethods(array $methods) { |
238
|
1 |
|
foreach ($this->methods as $method) { |
239
|
1 |
|
$method->setParent(null); |
240
|
1 |
|
} |
241
|
|
|
|
242
|
1 |
|
$this->methods = []; |
243
|
1 |
|
foreach ($methods as $method) { |
244
|
1 |
|
$this->setMethod($method); |
245
|
1 |
|
} |
246
|
|
|
|
247
|
1 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Adds a method |
252
|
|
|
* |
253
|
|
|
* @param PhpMethod $method |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
20 |
|
public function setMethod(PhpMethod $method) { |
257
|
20 |
|
$method->setParent($this); |
258
|
20 |
|
$this->methods[$method->getName()] = $method; |
259
|
|
|
|
260
|
20 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Removes a method |
265
|
|
|
* |
266
|
|
|
* @param string|PhpMethod $nameOrMethod method name or Method instance |
267
|
|
|
* @throws \InvalidArgumentException if the method cannot be found |
268
|
|
|
* @return $this |
269
|
|
|
*/ |
270
|
2 |
|
public function removeMethod($nameOrMethod) { |
271
|
2 |
|
if ($nameOrMethod instanceof PhpMethod) { |
272
|
1 |
|
$nameOrMethod = $nameOrMethod->getName(); |
273
|
1 |
|
} |
274
|
|
|
|
275
|
2 |
|
if (!array_key_exists($nameOrMethod, $this->methods)) { |
276
|
1 |
|
throw new \InvalidArgumentException(sprintf('The method "%s" does not exist.', $nameOrMethod)); |
277
|
|
|
} |
278
|
1 |
|
$m = $this->methods[$nameOrMethod]; |
279
|
1 |
|
$m->setParent(null); |
280
|
1 |
|
unset($this->methods[$nameOrMethod]); |
281
|
|
|
|
282
|
1 |
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Checks whether a method exists or not |
287
|
|
|
* |
288
|
|
|
* @param string|PhpMethod $nameOrMethod method name or Method instance |
289
|
|
|
* @return boolean `true` if it exists and `false` if not |
290
|
|
|
*/ |
291
|
2 |
|
public function hasMethod($nameOrMethod) { |
292
|
2 |
|
if ($nameOrMethod instanceof PhpMethod) { |
293
|
1 |
|
$nameOrMethod = $nameOrMethod->getName(); |
294
|
1 |
|
} |
295
|
|
|
|
296
|
2 |
|
return isset($this->methods[$nameOrMethod]); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Returns a method |
301
|
|
|
* |
302
|
|
|
* @param string $nameOrMethod the methods name |
303
|
|
|
* @throws \InvalidArgumentException if the method cannot be found |
304
|
|
|
* @return PhpMethod |
305
|
|
|
*/ |
306
|
7 |
|
public function getMethod($nameOrMethod) { |
307
|
7 |
|
if ($nameOrMethod instanceof PhpMethod) { |
308
|
1 |
|
$nameOrMethod = $nameOrMethod->getName(); |
309
|
1 |
|
} |
310
|
|
|
|
311
|
7 |
|
if (!isset($this->methods[$nameOrMethod])) { |
312
|
2 |
|
throw new \InvalidArgumentException(sprintf('The method "%s" does not exist.', $nameOrMethod)); |
313
|
|
|
} |
314
|
|
|
|
315
|
6 |
|
return $this->methods[$nameOrMethod]; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Returns all methods |
320
|
|
|
* |
321
|
|
|
* @return PhpMethod[] collection of methods |
322
|
|
|
*/ |
323
|
14 |
|
public function getMethods() { |
324
|
14 |
|
return $this->methods; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Returns all method names |
329
|
|
|
* |
330
|
|
|
* @return string[] |
331
|
|
|
*/ |
332
|
1 |
|
public function getMethodNames() { |
333
|
1 |
|
return array_keys($this->methods); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Clears all methods |
338
|
|
|
* |
339
|
|
|
* @return $this |
340
|
|
|
*/ |
341
|
1 |
|
public function clearMethods() { |
342
|
1 |
|
foreach ($this->methods as $method) { |
343
|
1 |
|
$method->setParent(null); |
344
|
1 |
|
} |
345
|
1 |
|
$this->methods = []; |
346
|
|
|
|
347
|
1 |
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Generates a docblock from provided information |
352
|
|
|
* |
353
|
|
|
* @return $this |
354
|
|
|
*/ |
355
|
13 |
|
public function generateDocblock() { |
356
|
13 |
|
$docblock = $this->getDocblock(); |
357
|
13 |
|
$docblock->setShortDescription($this->getDescription()); |
358
|
13 |
|
$docblock->setLongDescription($this->getLongDescription()); |
359
|
|
|
|
360
|
13 |
|
foreach ($this->methods as $method) { |
361
|
9 |
|
$method->generateDocblock(); |
362
|
13 |
|
} |
363
|
|
|
|
364
|
13 |
|
return $this; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|