forked from BraveSirRobin/PhpExporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_parser.php
executable file
·171 lines (150 loc) · 5.43 KB
/
code_parser.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
#! /usr/bin/php
<?php
/**
*
* Copyright (C) 2007 - 2011 Robin Harvey ([email protected])
*
* 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
*/
/** Get a reliable list of native classes and interfaces **/
$classes = get_declared_classes();
$interfaces = get_declared_interfaces();
require(dirname(__FILE__) . '/CodeParser.class.php');
require(dirname(__FILE__) . '/OutputWriter.class.php');
class CLICommand
{
private $default_file_output = 'ParsedCode.xml';
private $default_dir_output = 'parsedcode.d';
private $gran = OutputWriter::OUTPUT_CONCAT;
private $out;
private $tgt;
function run() {
global $classes, $interfaces;
$p = new CodeParser;
OutputWriter::Object()->setNativeInterfaces($interfaces);
OutputWriter::Object()->setNativeClasses($classes);
OutputWriter::Object()->setOutputGran($this->gran);
OutputWriter::Object()->setOutputTarget($this->out);
//if ($this->gran == OutputWriter::OUTPUT_GRAN_CLASS || $this->gran == OutputWriter::OUTPUT_GRAN_FILE) {
if (is_dir($this->tgt)) {
$p->parseRecursive($this->tgt);
}
else {
$p->parse($this->tgt);
}
}
/**
* Parse the commandline args.
*/
function __construct() {
global $argv;
$recv = '';
foreach ($argv as $i => $arg) {
if ($i == 0) {
continue;
}
if ($recv == '' && substr($arg, 0, 2) == '--') {
switch ($arg) {
case '--gran':
$recv = 'GRAN';
continue;
case '--out':
$recv = 'OUT';
continue;
case '--help':
die($this->usage);
default:
echo "\nUnknown Option '$arg'";
continue;
}
}
else if ($recv) {
switch ($recv) {
case 'GRAN':
if (strtolower($arg) == 'class') {
$this->gran = OutputWriter::OUTPUT_GRAN_CLASS;
}
else if (strtolower($arg) == 'file') {
$this->gran = OutputWriter::OUTPUT_GRAN_FILE;
}
else if (strtolower($arg) == 'concat') {
$this->gran = OutputWriter::OUTPUT_CONCAT;
}
else {
echo "\nWARNING: bad output granularity: $arg\n";
}
$recv = '';
continue;
case 'OUT':
$this->out = $arg;
$recv = '';
continue;
}
}
else if (! $this->tgt) {
$this->tgt = $arg;
}
else {
echo "\nBad command line args, ignoring $arg";
}
} //foreach
if (! $this->tgt) {
die("\nNo parse target, exiting\n");
}
//TODO: $pstyle not used.
if (is_dir($this->tgt)) {
$pstyle = 'recursive';
}
else if (is_file($this->tgt)) {
$pstyle = 'flat';
}
else {
die("\nBad parsing target {$this->tgt}\n");
}
//Sanify the output and granularity
if ($this->gran == OutputWriter::OUTPUT_GRAN_FILE || $this->gran == OutputWriter::OUTPUT_GRAN_CLASS) {
if (! $this->out) {
$this->out = $this->default_dir_output;
}
//Check for suitable output dir
if (is_dir($this->out)) {
die("\nOutput Dir {$this->out} already exists\n");
}
else if (is_file($this->out)) {
die("\nThe given output Dir '{$this->out}' is a file!\n");
}
else if (! @mkdir($this->out)) {
die("\nFailed to create output directory $dir\n");
}
}
else if ($this->gran == OutputWriter::OUTPUT_CONCAT) {
if (! $this->out) {
$this->out = $this->default_file_output;
}
if (is_file($this->out)) {
die("\nOutput file {$this->out} already exists\n");
}
else if (! is_writable(dirname($this->out))) {
die("\nCannot write in the specified output directory '" . dirname($this->out) . "'\n");
}
}
//Sanify the target
if (! (is_dir($this->tgt) || is_file($this->tgt))) {
die("\nParse target does not exist: {$this->tgt}\n");
}
}
private $usage = "USAGE:\ncode_parser [--gran concat|file|class] [--out file|dir] file|dir\n";
}
$c = new CLICommand;
$c->run();
?>