1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Mike van Riel <[email protected]> |
11
|
|
|
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
13
|
|
|
* @link http://phpdoc.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace phpDocumentor\Configuration\Factory; |
17
|
|
|
|
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use phpDocumentor\Dsn; |
20
|
|
|
use phpDocumentor\Path; |
21
|
|
|
use SimpleXMLElement; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* phpDocumentor2 strategy for converting the configuration xml to an array. |
25
|
|
|
*/ |
26
|
|
|
final class Version2 implements Strategy |
27
|
|
|
{ |
28
|
|
|
/** @var string[] */ |
29
|
|
|
private $extensions = ['php', 'php3', 'phtml']; |
30
|
|
|
|
31
|
|
|
/** @var string[] */ |
32
|
|
|
private $markers = ['TODO', 'FIXME']; |
33
|
|
|
|
34
|
|
|
/** @var string[] */ |
35
|
|
|
private $visibility = ['public', 'protected', 'private']; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private $defaultPackageName = 'Default'; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
private $template = 'clean'; |
42
|
|
|
|
43
|
|
|
/** @var bool */ |
44
|
|
|
private $ignoreHidden = true; |
45
|
|
|
|
46
|
|
|
/** @var bool */ |
47
|
|
|
private $ignoreSymlinks = true; |
48
|
|
|
|
49
|
|
|
/** @var string[] */ |
50
|
|
|
private $ignorePaths = []; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
private $outputDirectory = 'file://build/docs'; |
54
|
|
|
|
55
|
|
|
/** @var string[] */ |
56
|
|
|
private $directories = ['src']; |
57
|
|
|
|
58
|
|
|
private $includeSource = false; |
59
|
|
|
|
60
|
6 |
|
public function convert(SimpleXMLElement $phpDocumentor): array |
61
|
|
|
{ |
62
|
6 |
|
$this->validate($phpDocumentor); |
63
|
|
|
|
64
|
6 |
|
$outputDirectory = $this->buildOutputDirectory($phpDocumentor); |
65
|
6 |
|
$cacheDirectory = $this->buildCacheDirectory($phpDocumentor); |
66
|
|
|
|
67
|
|
|
return [ |
68
|
6 |
|
'phpdocumentor' => [ |
69
|
6 |
|
'title' => ((string)$phpDocumentor->title) ?: 'my-doc', |
70
|
|
|
'use-cache' => true, |
71
|
|
|
'paths' => [ |
72
|
6 |
|
'output' => new Dsn($outputDirectory), |
73
|
6 |
|
'cache' => new Path($cacheDirectory), |
74
|
|
|
], |
75
|
|
|
'versions' => [ |
76
|
|
|
'1.0.0' => [ |
77
|
6 |
|
'folder' => '', |
78
|
|
|
'api' => [ |
79
|
|
|
0 => [ |
80
|
6 |
|
'encoding' => $this->buildEncoding($phpDocumentor), |
81
|
|
|
'ignore-tags' => [], |
82
|
6 |
|
'format' => 'php', |
83
|
|
|
'validate' => false, |
84
|
|
|
'source' => [ |
85
|
6 |
|
'dsn' => new Dsn('file://' . getcwd()), |
86
|
6 |
|
'paths' => $this->buildSourcePaths($phpDocumentor), |
87
|
|
|
], |
88
|
|
|
'ignore' => [ |
89
|
6 |
|
'hidden' => $this->buildIgnoreHidden($phpDocumentor), |
90
|
6 |
|
'symlinks' => $this->buildIgnoreSymlinks($phpDocumentor), |
91
|
6 |
|
'paths' => $this->buildIgnorePaths($phpDocumentor), |
92
|
|
|
], |
93
|
6 |
|
'extensions' => $this->buildExtensions($phpDocumentor), |
94
|
6 |
|
'visibility' => $this->buildVisibility($phpDocumentor), |
95
|
6 |
|
'include-source' => $this->buildIncludeSourcecode($phpDocumentor), |
96
|
6 |
|
'default-package-name' => $this->buildDefaultPackageName($phpDocumentor), |
97
|
6 |
|
'markers' => $this->buildMarkers($phpDocumentor), |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
'templates' => [ |
103
|
|
|
[ |
104
|
6 |
|
'name' => $this->buildTemplate($phpDocumentor), |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
], |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function supports(SimpleXMLElement $phpDocumentor): bool |
112
|
|
|
{ |
113
|
1 |
|
return isset($phpDocumentor->attributes()->version) === false |
114
|
1 |
|
|| $phpDocumentor->attributes()->version == '2'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Loops over a node and fills an array with the found children. |
119
|
|
|
*/ |
120
|
1 |
|
private function buildArrayFromNode(SimpleXMLElement $node): array |
121
|
|
|
{ |
122
|
1 |
|
$array = []; |
123
|
1 |
|
foreach ($node->children() as $child) { |
124
|
1 |
|
if ((string) $child !== '') { |
125
|
1 |
|
$array[] = (string) $child; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return $array; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Builds the extensions part of the array from the configuration xml. |
134
|
|
|
* |
135
|
|
|
* @return string[] |
136
|
|
|
*/ |
137
|
6 |
|
private function buildExtensions(SimpleXMLElement $phpDocumentor): array |
138
|
|
|
{ |
139
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
140
|
|
|
return $this->extensions; |
141
|
|
|
} |
142
|
|
|
|
143
|
6 |
|
if ((array) $phpDocumentor->parser->extensions === []) { |
144
|
5 |
|
return $this->extensions; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
return $this->buildArrayFromNode($phpDocumentor->parser->extensions); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Builds the markers part of the array from the configuration xml. |
152
|
|
|
* |
153
|
|
|
* @return string[] |
154
|
|
|
*/ |
155
|
6 |
|
private function buildMarkers(SimpleXMLElement $phpDocumentor): array |
156
|
|
|
{ |
157
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
158
|
|
|
return $this->markers; |
159
|
|
|
} |
160
|
|
|
|
161
|
6 |
|
if ((array) $phpDocumentor->parser->markers === []) { |
162
|
5 |
|
return $this->markers; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
return $this->buildArrayFromNode($phpDocumentor->parser->markers); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Builds whether the source code should be part of the output. |
170
|
|
|
*/ |
171
|
6 |
|
private function buildIncludeSourcecode(SimpleXMLElement $phpDocumentor): bool |
172
|
|
|
{ |
173
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
174
|
|
|
return $this->includeSource; |
175
|
|
|
} |
176
|
|
|
|
177
|
6 |
|
return (bool) $phpDocumentor->parser->{'include-source'}; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Builds the visibility part of the array from the configuration xml. |
182
|
|
|
* |
183
|
|
|
* @return string[] |
184
|
|
|
*/ |
185
|
6 |
|
private function buildVisibility(SimpleXMLElement $phpDocumentor): array |
186
|
|
|
{ |
187
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
188
|
|
|
return $this->visibility; |
189
|
|
|
} |
190
|
|
|
|
191
|
6 |
|
if ((string) $phpDocumentor->parser->visibility === '') { |
192
|
5 |
|
return $this->visibility; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
return explode(',', (string) $phpDocumentor->parser->visibility); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Builds the defaultPackageName part of the array from the configuration xml. |
200
|
|
|
*/ |
201
|
6 |
|
private function buildDefaultPackageName(SimpleXMLElement $phpDocumentor): string |
202
|
|
|
{ |
203
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
204
|
|
|
return $this->defaultPackageName; |
205
|
|
|
} |
206
|
|
|
|
207
|
6 |
|
if ((string) $phpDocumentor->parser->{'default-package-name'} === '') { |
208
|
|
|
return $this->defaultPackageName; |
209
|
|
|
} |
210
|
|
|
|
211
|
6 |
|
return (string) $phpDocumentor->parser->{'default-package-name'}; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Builds the template part of the array from the configuration xml. |
216
|
|
|
*/ |
217
|
6 |
|
private function buildTemplate(SimpleXMLElement $phpDocumentor): string |
218
|
|
|
{ |
219
|
6 |
|
if ((array) $phpDocumentor->transformations === []) { |
220
|
|
|
return $this->template; |
221
|
|
|
} |
222
|
|
|
|
223
|
6 |
|
if ((string) $phpDocumentor->transformations->template === '') { |
224
|
6 |
|
return $this->template; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return (string) $phpDocumentor->transformations->template->attributes()->name; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Builds the ignore-hidden part of the array from the configuration xml. |
232
|
|
|
*/ |
233
|
6 |
|
private function buildIgnoreHidden(SimpleXMLElement $phpDocumentor): bool |
234
|
|
|
{ |
235
|
6 |
|
if ((array) $phpDocumentor->files === []) { |
236
|
|
|
return $this->ignoreHidden; |
237
|
|
|
} |
238
|
|
|
|
239
|
6 |
|
if ((string) $phpDocumentor->files->{'ignore-hidden'} === '') { |
240
|
5 |
|
return $this->ignoreHidden; |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
return filter_var($phpDocumentor->files->{'ignore-hidden'}, FILTER_VALIDATE_BOOLEAN); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Builds the ignore-symlinks part of the array from the configuration xml. |
248
|
|
|
*/ |
249
|
6 |
|
private function buildIgnoreSymlinks(SimpleXMLElement $phpDocumentor): bool |
250
|
|
|
{ |
251
|
6 |
|
if ((array) $phpDocumentor->files === []) { |
252
|
|
|
return $this->ignoreSymlinks; |
253
|
|
|
} |
254
|
|
|
|
255
|
6 |
|
if ((string) $phpDocumentor->files->{'ignore-symlinks'} === '') { |
256
|
5 |
|
return $this->ignoreSymlinks; |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
return filter_var($phpDocumentor->files->{'ignore-symlinks'}, FILTER_VALIDATE_BOOLEAN); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Builds the ignorePaths part of the array from the configuration xml. |
264
|
|
|
* |
265
|
|
|
* @return string[] |
266
|
|
|
*/ |
267
|
6 |
|
private function buildIgnorePaths(SimpleXMLElement $phpDocumentor): array |
268
|
|
|
{ |
269
|
6 |
|
if ((array) $phpDocumentor->files === []) { |
270
|
|
|
return $this->ignorePaths; |
271
|
|
|
} |
272
|
6 |
|
$ignorePaths = []; |
273
|
6 |
|
foreach ($phpDocumentor->files->children() as $child) { |
274
|
6 |
|
if ($child->getName() === 'ignore') { |
275
|
6 |
|
$ignorePaths[] = (string) $child; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
6 |
|
if (count($ignorePaths) === 0) { |
280
|
5 |
|
return $this->ignorePaths; |
281
|
|
|
} |
282
|
|
|
|
283
|
1 |
|
return $ignorePaths; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Builds the outputDirectory part of the array from the configuration xml. |
288
|
|
|
*/ |
289
|
6 |
|
private function buildOutputDirectory(SimpleXMLElement $phpDocumentor): string |
290
|
|
|
{ |
291
|
6 |
|
if ((array) $phpDocumentor->transformer === []) { |
292
|
|
|
return $this->outputDirectory; |
293
|
|
|
} |
294
|
|
|
|
295
|
6 |
|
if ((string) $phpDocumentor->transformer->target === '') { |
296
|
|
|
return $this->outputDirectory; |
297
|
|
|
} |
298
|
|
|
|
299
|
6 |
|
return (string) $phpDocumentor->transformer->target; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Builds the directories that are used in the sourcePaths. |
304
|
|
|
* |
305
|
|
|
* @return string[] |
306
|
|
|
*/ |
307
|
6 |
|
private function buildDirectories(SimpleXMLElement $phpDocumentor): array |
308
|
|
|
{ |
309
|
6 |
|
if ((array) $phpDocumentor->files === []) { |
310
|
|
|
return $this->directories; |
311
|
|
|
} |
312
|
|
|
|
313
|
6 |
|
if ((string) $phpDocumentor->files->directory === '') { |
314
|
|
|
return $this->directories; |
315
|
|
|
} |
316
|
|
|
|
317
|
6 |
|
return (array) $phpDocumentor->files->directory; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Builds the sourcePaths part of the array from the configuration xml. |
322
|
|
|
* |
323
|
|
|
* @return Path[] |
324
|
|
|
*/ |
325
|
6 |
|
private function buildSourcePaths(SimpleXMLElement $phpDocumentor): array |
326
|
|
|
{ |
327
|
6 |
|
$sourcePaths = []; |
328
|
6 |
|
$directories = $this->buildDirectories($phpDocumentor); |
329
|
|
|
|
330
|
6 |
|
foreach ($directories as $directory) { |
331
|
6 |
|
$sourcePaths[] = (new Dsn($directory))->getPath(); |
332
|
|
|
} |
333
|
|
|
|
334
|
6 |
|
return $sourcePaths; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Builds the outputDirectory part of the array from the configuration xml. |
339
|
|
|
*/ |
340
|
6 |
|
private function buildCacheDirectory(SimpleXMLElement $phpDocumentor): string |
341
|
|
|
{ |
342
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
343
|
|
|
return '/tmp/phpdoc-doc-cache'; |
344
|
|
|
} |
345
|
|
|
|
346
|
6 |
|
if ((string) $phpDocumentor->parser->target === '') { |
347
|
3 |
|
return '/tmp/phpdoc-doc-cache'; |
348
|
|
|
} |
349
|
|
|
|
350
|
3 |
|
return (string) $phpDocumentor->parser->target; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Validates if the xml has a root element which name is phpdocumentor. |
355
|
|
|
* |
356
|
|
|
* @throws InvalidArgumentException if the root element of the xml is not phpdocumentor. |
357
|
|
|
*/ |
358
|
7 |
|
private function validate(SimpleXMLElement $xml): void |
359
|
|
|
{ |
360
|
7 |
|
if ($xml->getName() !== 'phpdocumentor') { |
361
|
1 |
|
throw new InvalidArgumentException( |
362
|
1 |
|
sprintf('Root element name should be phpdocumentor, %s found', $xml->getName()) |
363
|
|
|
); |
364
|
|
|
} |
365
|
6 |
|
} |
366
|
|
|
|
367
|
6 |
|
private function buildEncoding(SimpleXMLElement $phpDocumentor) |
368
|
|
|
{ |
369
|
6 |
|
if ((array) $phpDocumentor->parser === []) { |
370
|
|
|
return 'utf-8'; |
371
|
|
|
} |
372
|
|
|
|
373
|
6 |
|
if ((string) $phpDocumentor->parser->encoding === '') { |
374
|
|
|
return 'utf-8'; |
375
|
|
|
} |
376
|
|
|
|
377
|
6 |
|
return (string) $phpDocumentor->parser->encoding; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|