forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.php
598 lines (465 loc) · 15.1 KB
/
gen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>phaser doc gen</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 14px;
background-color: #fff;
color: #000;
}
textarea {
width: 100%;
height: 1000px;
}
</style>
</head>
<body>
<pre spellcheck="false">
<?php
class Block
{
public $start = 0;
public $end = 0;
public $code = '';
public $content;
public $isProperty = false;
public $isMethod = false;
public $isConst = false;
public $isClass = false;
public function __construct($start, $end, $code, $content)
{
$this->start = $start;
$this->end = $end;
$this->code = $code;
$this->content = $content;
// preg_match("/(@.*){(\S*)}(.*) -(.*)/", $input_line, $output_array);
// this one captures a property even with a * at the start
$this->isProperty = $this->getTypeBoolean('@property');
$this->isMethod = $this->getTypeBoolean('@method');
$this->isConst = $this->getTypeBoolean('@constant');
$this->isClass = $this->getTypeBoolean('@class');
}
public function getTypeBoolean($scan)
{
for ($i = 0; $i < count($this->content); $i++)
{
preg_match("/(@\w*)/", $this->content[$i], $output);
if ($output && $output[0] === $scan)
{
return true;
}
}
return false;
}
public function getLineContent($scan)
{
$line = $this->getLine($scan);
if ($line !== false)
{
$pattern = '/.*' . $scan . ' (.*)/';
preg_match($pattern, $line, $output);
if ($output)
{
return trim($output[1]);
}
}
return false;
}
public function getLine($scan)
{
$i = $this->getContentIndex($scan);
if ($i > -1)
{
return $this->content[$i];
}
else
{
return false;
}
}
public function getContentIndex($scan, $offset = 0)
{
for ($i = $offset; $i < count($this->content); $i++)
{
preg_match("/(@\w*)/", $this->content[$i], $output);
if ($output && $output[0] === $scan)
{
return $i;
}
}
return -1;
}
public function getLines($scan)
{
$output = [];
for ($i = 0; $i < count($this->content); $i++)
{
preg_match("/(@\w*)/", $this->content[$i], $line);
if ($line && $line[0] === $scan)
{
$output[] = $this->content[$i];
}
}
return $output;
}
public function cleanContent()
{
$output = [];
for ($i = 0; $i < count($this->content); $i++)
{
$line = trim($this->content[$i]);
// remove * from the start
if (substr($line, 0, 1) === '*')
{
$line = substr($line, 1);
$line = trim($line);
}
// Have we got a @ next?
if (substr($line, 0, 1) !== '@')
{
$output[] = $line;
}
}
// Trim off the final element if empty
if (count($output) > 0 && $output[count($output) - 1] === '')
{
array_pop($output);
}
return $output;
}
}
class Parameter
{
public $name; // rect, copy, etc
public $types = []; // an array containing all possible types it can be: string, number, etc
public $help = [];
public $optional = false;
public $default = false; // assigned value is the default value
public function __construct($line)
{
preg_match("/.*(@param)\s?{(\S*)} (\S*)( - ?)?(.*)/", $line, $output);
$this->types = explode('|', $output[2]);
$this->help = $output[5];
$name = $output[3];
if ($name[0] === '[')
{
$this->optional = true;
$name = substr($name, 1, -1);
// Default?
$equals = strpos($name, '=');
if ($equals > 0)
{
$name = substr($name, 0, $equals - 1);
$this->default = substr($name, $equals + 1);
}
}
$this->name = $name;
}
}
// Because we can't have a class called "Class", grrr...
class PhaserClass
{
public $name; // Phaser.Sprite
public $parameters = []; // an array containing the parameters
public $help = [];
public $extends = '';
public $hasConstructor = false;
public $isStatic = false;
public function __construct($block)
{
$this->name = $block->getLineContent('@class');
$params = $block->getLines('@param');
for ($i = 0; $i < count($params); $i++)
{
$this->parameters[] = new Parameter($params[$i]);
}
if ($block->getTypeBoolean('@extends'))
{
$this->extends = $block->getLineContent('@extends');
}
if ($block->getTypeBoolean('@constructor'))
{
$this->hasConstructor = true;
}
else
{
// Like Phaser.Math
$this->isStatic = true;
}
// This is a problem because the @classdesc block is often multi-line, but repeated in the clear content too.
// So all the classes probably need tidying up before this part will work correctly.
// $this->help = $block->cleanContent();
}
}
class Constant
{
public $name; // TEXTURE_ATLAS_JSON_ARRAY, PHYSICS_PHASER_JSON, etc
public $types = []; // an array containing the one single type the const can be
public $help = [];
public $line; // number, line number in the source file this is found on?
public function __construct($block)
{
// Because zero offset + allowing for final line
$this->line = $block->end + 2;
// Phaser.Cache.TEXTURE = 3;
$name = $block->code;
$period = strrpos($name, '.');
$equals = strrpos($name, '=');
if ($period > 0 && $equals > 0)
{
$len = $equals - $period - 1;
$name = substr($name, $period + 1, $len);
}
else if ($period > 0)
{
$name = substr($name, $period + 1, -1);
}
$this->name = trim($name);
$line = $block->getLine('@type');
if ($line && preg_match("/.*@type\s?{(\S*)}/", $line, $output))
{
$this->types = explode('|', $output[1]);
}
}
}
class ReturnType
{
public $types = []; // an array containing all possible types it can be: string, number, etc
public $help = [];
public function __construct($line)
{
if (preg_match("/.*@return\s?{(\S*)} ?(.*)/", $line, $output))
{
$this->types = explode('|', $output[1]);
$this->help = $output[2];
}
}
}
class Method
{
public $line; // number, line number in the source file this is found on?
public $name; // bringToTop, kill, etc
public $parameters = []; // an array containing the parameters
public $help = [];
public $returns = false;
public $isPublic = true;
public $isProtected = false;
public $isPrivate = false;
public function __construct($block)
{
// Because zero offset + allowing for final line
$this->line = $block->end + 2;
$name = $block->getLine('@method');
$equals = strpos($name, '#');
if ($equals > 0)
{
$name = substr($name, $equals + 1);
}
$this->name = $name;
$params = $block->getLines('@param');
for ($i = 0; $i < count($params); $i++)
{
$this->parameters[] = new Parameter($params[$i]);
}
if ($block->getTypeBoolean('@protected'))
{
$this->isPublic = false;
$this->isProtected = true;
}
else if ($block->getTypeBoolean('@private'))
{
$this->isPublic = false;
$this->isPrivate = true;
}
$this->help = $block->cleanContent();
if ($block->getTypeBoolean('@return'))
{
$this->returns = new ReturnType($block->getLine('@return'));
}
}
}
class Property
{
public $line; // number, line number in the source file this is found on?
public $name; // visible, name, parent
public $types = []; // an array containing all possible types it can be: string, number, etc
public $default = false; // assigned value is the default value
public $help = [];
public $inlineHelp = '';
public $isPublic = true;
public $isProtected = false;
public $isPrivate = false;
public $isReadOnly = false;
public function __construct($block)
{
// Because zero offset + allowing for final line
$this->line = $block->end + 2;
preg_match("/(@.*){(\S*)} (\S*)( - ?)?(.*)/", $block->getLine('@property'), $output);
$this->types = explode('|', $output[2]);
$this->name = $output[3];
$this->inlineHelp = $output[5];
if ($block->getTypeBoolean('@protected'))
{
$this->isPublic = false;
$this->isProtected = true;
}
else if ($block->getTypeBoolean('@private'))
{
$this->isPublic = false;
$this->isPrivate = true;
}
// Default?
if ($block->getTypeBoolean('@default'))
{
preg_match("/= (.*);/", $block->code, $defaultType);
if ($defaultType && isset($defaultType[1]))
{
$this->default = $defaultType[1];
}
}
$this->help = $block->cleanContent();
if ($block->getTypeBoolean('@readonly'))
{
$this->isReadOnly = true;
}
}
}
class ScanFile
{
public $filename = '';
public $js = '';
public $blocks;
public $consts = [];
public $properties = [];
public $methods = [];
public function __construct($file)
{
$this->filename = $file;
$js = file($file);
$scanningForOpen = true;
$scanningForClose = false;
$openLine = 0;
$closeLine = 0;
$chunk = [];
for ($i = 0; $i < count($js); $i++)
{
$line = trim($js[$i]);
if ($scanningForOpen && $line === "/**")
{
$scanningForOpen = false;
$scanningForClose = true;
$chunk = [];
$openLine = $i;
}
if ($scanningForClose && $line === "*/")
{
$scanningForOpen = true;
$scanningForClose = false;
$closeLine = $i;
// The first element is always the opening /** so remove it
array_shift($chunk);
$this->blocks[] = new Block($openLine, $closeLine, $js[$i + 1], $chunk);
}
else
{
$chunk[] = $line;
}
}
}
public function total()
{
return count($this->blocks);
}
}
// $blocks = [];
// $consts = [];
// $properties = [];
// $methods = [];
// $file = "../src/gameobjects/Sprite.js";
$file = "../src/loader/Cache.js";
/*
$js = file($file);
$scanningForOpen = true;
$scanningForClose = false;
$openLine = 0;
$closeLine = 0;
$chunk = [];
for ($i = 0; $i < count($js); $i++)
{
$line = trim($js[$i]);
if ($scanningForOpen && $line === "/**")
{
$scanningForOpen = false;
$scanningForClose = true;
$chunk = [];
$openLine = $i;
}
if ($scanningForClose && $line === "*/")
{
$scanningForOpen = true;
$scanningForClose = false;
$closeLine = $i;
// The first element is always the opening /** so remove it
array_shift($chunk);
$blocks[] = new Block($openLine, $closeLine, $js[$i + 1], $chunk);
}
else
{
$chunk[] = $line;
}
}
*/
// That's the whole file scanned, how many blocks did we get out of it?
echo count($blocks) . " blocks found\n\n";
echo "\nConstants:\n\n";
for ($i = 0; $i < count($blocks); $i++)
{
if ($blocks[$i]->isConst)
{
$const = new Constant($blocks[$i]);
echo "\n\n";
echo $const->name;
echo "\n";
print_r($const->types);
// print_r($method->help);
}
}
echo "\nMethods:\n\n";
for ($i = 0; $i < count($blocks); $i++)
{
if ($blocks[$i]->isMethod)
{
$method = new Method($blocks[$i]);
echo "\n\n";
echo $method->name;
echo "\n";
print_r($method->help);
print_r($method->parameters);
print_r($method->returns);
}
}
echo "\nProperties:\n\n";
for ($i = 0; $i < count($blocks); $i++)
{
if ($blocks[$i]->isProperty)
{
// First things first - we know it's a property
echo "\n\n";
$property = new Property($blocks[$i]);
echo $property->name . "\n";
// print_r($property->types);
echo "Source code line " . $property->line . "\n";
echo "Default: " . $property->default . "\n";
echo "Help: " . "\n";
print_r($property->help);
echo "\n\n";
// echo $blocks[$i]->start . "\n";
}
}
?>
</pre>
</body>
</html>