forked from BraveSirRobin/PhpExporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputWriter.class.php
526 lines (483 loc) · 16.5 KB
/
OutputWriter.class.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
<?php
/**
*
* Copyright (C) 2007 - 2011 Robin Harvey (harvey.robin@gmail.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Class to handle events during code parsing and output the
* intermediate XML format. Events are written to target XML
* files using the XMLWriter built-in object. This class contains
* logic to allow output on different 'granularities' - see the
* OUTPUT_* consts.
*
*/
class OutputWriter
{
const OUTPUT_CONCAT = 1;//Output in one big file
const OUTPUT_GRAN_FILE = 2;//One output file for each inputted file.
const OUTPUT_GRAN_CLASS = 3;//One output file for each inputted class.
/** singleton plumbing */
private static $inst = null;
private function __construct() {}
static function Object() {
return (is_null(self::$inst)) ? (self::$inst = new OutputWriter) : self::$inst;
}
/** Class proper */
private $in_capture = false;
private $w = null; //XMLWriter goes here.
/*
* Getters and setters for the output granularity and desitation
* flags.
*/
private $gran = self::OUTPUT_CONCAT;
private $tgt = './ParsedCode.xml';
function setOutputGran($gran) {
if ($this->in_capture) {
die("\nDo no change the output granularity during parsing!\n");
}
$this->gran = $gran;
}
function getOutputGran() {
return $this->gran;
}
function setOutputTarget($tgt) {
if ($this->in_capture) {
die("\nDo no change the output target during parsing!\n");
}
$this->tgt = $tgt;
}
function getOutputTarget() {
return $this->tgt;
}
/**
* Getters, setters and info methods for listing native classes,
* designed to work with the output of the built-in get_declared_xxx()
* functions.
* Usage: $obj->setNativeInterfaces(get_declared_interfaces())
* $obj->setNativeClasses(get_declared_classes())
*/
private $nclasses;
private $ninterfaces;
function setNativeInterfaces(array $arr) {
$this->ninterfaces = $arr;
}
function getNativeInterfaces() {
return $this->ninterfaces;
}
function isNativeInterface($ifc) {
return in_array($ifc, $this->ninterfaces);
}
function setNativeClasses(array $arr) {
$this->nclasses = $arr;
}
function getNativeClasses(array $arr) {
return $this->nclasses;
}
function isNativeClass($cls) {
return in_array($cls, $this->nclasses);
}
/**
* Convenience: check the outut target and output granularity match,
* and that the target is writable
*/
private function checkConfig() {
switch ($this->gran) {
case self::OUTPUT_GRAN_FILE:
case self::OUTPUT_GRAN_CLASS:
if (! is_dir($this->tgt)) {
return "Output dir {$this->tgt} is not a directory";
}
else if (! is_writable($this->tgt)) {
return "Output dir {$this->tgt} is not writable";
}
break;
default:
echo "WARNING: Unknown output granularity: {$this->gran}, defaulting to OUTPUT_CONCAT";
$this->gran = self::OUTPUT_CONCAT;
case self::OUTPUT_CONCAT:
if (is_file($this->tgt)) {
return "Output target {$this->tgt} already exists";
}
if (! is_writable(dirname($this->tgt))) {
return "Output Dir" . dirname($this->tgt) . "Is not writable";
}
break;
}
return false;
}
/**
* Call this whenever a file name is needed. Examines current
* granularity and ensures uniqueness. If the given event should not
* produce any output, false is returned
*/
private function getEventOutputTarget($ev, $src_name = '') {
$dir = ($this->gran == self::OUTPUT_CONCAT) ? dirname($this->tgt) : $this->tgt;
switch ($ev) {
case 'startOutput':
if ($this->gran == self::OUTPUT_CONCAT) {
return $this->tgt;
}
return false;
case 'startFile':
$ret = $dir . '/' . basename($src_name) . '_file.xml';
break;
case 'startClass':
$ret = $dir . '/' . basename($src_name) . '_class.xml';
break;
case 'startInterface':
$ret = $dir . '/' . basename($src_name) . '_interface.xml';
break;
default:
throw new Exception("Event '$ev' should not need to generate an output target.", 98758);
}
switch ($this->gran) {
case self::OUTPUT_CONCAT:
return false;
break;
case self::OUTPUT_GRAN_FILE:
return ($ev == 'startFile') ? $ret : false;
break;
case self::OUTPUT_GRAN_CLASS:
return ($ev == 'startClass' || $ev == 'startInterface') ? $ret : false;
break;
}
throw new Exception("Farking twotheimer shinkleplarp", 98665);
}
/**
* Convenience: start a new XMLWriter, making sure to flush the old one,
* if required.
*/
private function startNewWriter($tgt, $parse_target = false) {
if ($this->w instanceof XMLWriter) {
$this->w->flush();
}
$this->w = new XmlWriter;
$this->w->openURI($tgt);
$this->w->setIndent(true);
$this->w->setIndentString(' ');
$this->w->startDocument('1.0', 'UTF-8');
$this->w->startElement('parsed-code');
$this->w->writeAttribute('parse-date', date('Y-m-d\TH:i:s\Z'));
if ($parse_target) {
$this->w->writeAttribute('base', $parse_target);
}
}
/**
* Convenience
*/
private function genericEndObject() {
if ($this->w instanceof XMLWriter) {
$this->w->endElement();
}
}
/**
* Convenience function which strips leading whitespace from
* multi-line documentation comments before they're written
* to the output.
*/
private function cleanDocComment($comm) {
return preg_replace("/^(\s*)(.*$)/m", "$2", $comm);
}
/**
* Event handlers - called from instances of CodeParser
*/
private $curr_file;
function startOutput($parse_target) {
if ($this->in_capture) {
throw new Exception('Cannot start multiple output sessions simultaneously!', 7689);
}
if ($err = $this->checkConfig()) {
throw new Exception($err, 7809);
}
if ($tgt = $this->getEventOutputTarget('startOutput')) {
$this->in_capture = true;//Moved inside
$this->startNewWriter($tgt, $parse_target);
}
}
function endOutput() {
$this->genericEndObject();
if ($this->gran == self::OUTPUT_CONCAT) {
$this->w->flush();
$this->in_capture = false;
}
}
function startFile($file) {
if ($this->gran == self::OUTPUT_GRAN_CLASS) {
//File is omitted implicitly at this granularity
return;
}
if ($tgt = $this->getEventOutputTarget('startFile', $file)) {
$this->in_capture = true;
$this->startNewWriter($tgt);
}
$this->w->startElement('file');
$this->w->writeAttribute('href', $file);
}
function endFile() {
if ($this->gran == self::OUTPUT_GRAN_CLASS) {
//File is omitted implicitly at this granularity
return;
}
$this->genericEndObject();
if ($this->gran == self::OUTPUT_GRAN_FILE) {
$this->genericEndObject();
$this->w->flush();
$this->in_capture = false;
}
}
function startClass($data) {
if ($tgt = $this->getEventOutputTarget('startClass', $data->name)) {
$this->in_capture = true;
$this->startNewWriter($tgt);
}
$this->writeComments($data);
$this->w->startElement('class');
if ($data->line_number) {
$this->w->writeAttribute('line', $data->line_number);
}
$this->w->writeAttribute('name', $data->name);
if ($data->abstract_flag) {
$this->w->writeAttribute('abstract', 'true');
}
if ($data->ext) {
$this->w->writeAttribute('super-class', $data->ext);
}
if ($data->is_native) {
$this->w->writeAttribute('native', 'true');
}
if ($data->decl) {
$this->w->writeAttribute('declaration', trim($data->decl));
}
if ($data->impl) {
$this->w->startElement('implemented-interfaces');
foreach ($data->impl as $ifc) {
$this->w->writeElement('interface-ref', $ifc);
}
$this->w->endElement();
}
}
function endClass() {
$this->genericEndObject();
if ($this->gran == self::OUTPUT_GRAN_CLASS) {
$this->genericEndObject();
$this->w->flush();
$this->in_capture = false;
}
}
function startInterface($data) {
if ($tgt = $this->getEventOutputTarget('startInterface', $data->name)) {
$this->in_capture = true;
$this->startNewWriter($tgt);
}
$this->writeComments($data);
$this->w->startElement('interface');
if ($data->line_number) {
$this->w->writeAttribute('line', $data->line_number);
}
$this->w->writeAttribute('name', $data->name);
if ($data->abstract_flag) {
$this->w->writeAttribute('abstract', 'true');
}
if ($data->ext) {
$this->w->writeAttribute('super-class', $data->ext);
}
if ($data->is_native) {
$this->w->writeAttribute('native', 'true');
}
if ($data->decl) {
$this->w->writeAttribute('declaration', trim($data->decl));
}
if ($data->impl) {
$this->w->startElement('implemented-interfaces');
foreach ($data->impl as $ifc) {
$this->w->writeElement('interface', $ifc);
}
$this->w->endElement();
}
}
function endInterface() {
//$this->genericEndObject();
$this->genericEndObject();
if ($this->gran == self::OUTPUT_GRAN_CLASS) {
$this->genericEndObject();
$this->w->flush();
$this->in_capture = false;
}
}
function startFunction($data) {
if ($this->in_capture) {
$this->funcImpl($data, 'function');
}
}
function startMethod($data) {
if ($this->in_capture) {
$this->funcImpl($data, 'method');
}
else {
var_dump($data);
//die;
}
}
private function funcImpl($data, $type) {
$this->writeComments($data);
$this->w->startElement($type);
if ($data->line_number) {
$this->w->writeAttribute('line', $data->line_number);
}
$this->w->writeAttribute('name', $data->func_name);
if ($data->abstract_flag) {
$this->w->writeAttribute('abstract', 'true');
}
if ($data->static_flag) {
$this->w->writeAttribute('static', 'true');
}
if ($data->final_flag) {
$this->w->writeAttribute('final', 'true');
}
if ($data->protected_flag) {
$this->w->writeAttribute('protected', 'true');
}
if ($data->private_flag) {
$this->w->writeAttribute('private', 'true');
}
if ($data->decl) {
$this->w->writeAttribute('declaration', trim($data->decl));
}
if ($data->params) {
foreach ($data->params as $prm) {
$this->w->startElement('param');
$this->w->writeAttribute('name', $prm->var);
if ($prm->hint) {
$this->w->writeAttribute('type-hint', $prm->hint);
}
if ($prm->default) {
$this->w->writeAttribute('default', $prm->default);
}
if ($prm->by_ref_flag) {
$this->w->writeAttribute('by-ref', 'true');
}
$this->w->endElement();
}
}
}
function endFunction() {
if ($this->in_capture) {
$this->genericEndObject();
}
}
function endMethod() {
if ($this->in_capture) {
$this->genericEndObject();
}
}
function writeVariable() {
//Not implemented ar the CodeParser level
}
function writeClassConst($data) {
$this->writeComments($data);
$this->w->startElement('const');
if ($data->line_number) {
$this->w->writeAttribute('line', $data->line_number);
}
$this->w->writeAttribute('name', $data->name);
if ($data->private_flag) {
$this->w->writeAttribute('private', 'true');
}
if ($data->final_flag) {
$this->w->writeAttribute('final', 'true');
}
if ($data->static_flag) {
$this->w->writeAttribute('static', 'true');
}
if ($data->decl) {
$this->w->writeAttribute('declaration', trim($data->decl));
}
if ($data->value != '') {
$this->w->text($data->value);
}
$this->w->endElement();
}
function writeField($data) {
$this->writeComments($data);
$this->w->startElement('field');
if ($data->line_number) {
$this->w->writeAttribute('line', $data->line_number);
}
$this->w->writeAttribute('name', $data->name);
if ($data->private_flag) {
$this->w->writeAttribute('private', 'true');
}
if ($data->final_flag) {
$this->w->writeAttribute('final', 'true');
}
if ($data->static_flag) {
$this->w->writeAttribute('static', 'true');
}
if ($data->decl) {
$this->w->writeAttribute('declaration', trim($data->decl));
}
if ($data->value != '') {
$this->w->text($data->value);
}
$this->w->endElement();
}
function writeGlobalVariable($data) {
if ($this->gran == self::OUTPUT_GRAN_CLASS) {
return;
}
$this->writeComments($data);
$this->w->startElement('gvar');
if ($data->line_number) {
$this->w->writeAttribute('line', $data->line_number);
}
$this->w->writeAttribute('name', $data->name);
if ($data->decl) {
$this->w->writeAttribute('declaration', trim($data->decl));
}
if ($data->value != '') {
$this->w->text($data->value);
}
$this->w->endElement();
}
private function writeComments($data) {
if ($data->comments) {
foreach ($data->comments as $tok) {
switch ($tok[0]) {
case T_COMMENT:
$this->w->startElement('comment');
if ($data->line_number) {
$this->w->writeAttribute('line', $tok[2]);
}
$this->w->writeAttribute('style', 'inline');
$this->w->text($this->cleanDocComment(trim($tok[1])));
$this->w->endElement();
break;
case T_DOC_COMMENT:
$this->w->startElement('comment');
if ($data->line_number) {
$this->w->writeAttribute('line', $tok[2]);
}
$this->w->writeAttribute('style', 'documentation');
$this->w->text($this->cleanDocComment(trim($tok[1])));
$this->w->endElement();
break;
default:
trigger_error('Illegal token for comment: \'' . token_name($tok[0]) . '\'');
}
}
}
}
}
?>