Menu

[r182]: / trunk / src / PHPCallGraphCli.php  Maximize  Restore  History

Download this file

247 lines (222 with data), 8.9 kB

  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
<?php
/**
* Console front-end to use PHPCallGraph from the command line
*
* PHP version 5
*
* This file is part of PHPCallGraph.
*
* PHPCallGraph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* PHPCallGraph 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package PHPCallGraph
* @author Falko Menge <fakko at users dot sourceforge dot net>
* @copyright 2007-2009 Falko Menge
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
*/
// include PHPCallGraph library
require_once 'PHPCallGraph.php';
/**
* console front-end to use PHPCallGraph from the command line
*
* This class requires eZ Components autoloader to be configured
* because it uses ezcConsoleTools
*/
class PHPCallGraphCli {
/**
* @return PHPCallGraphCli
*/
public function __construct() {
}
/**
* starts the command line interface for PHPCallGraph
* @return void
*/
public function run() {
$input = new ezcConsoleInput();
$formatOption = $input->registerOption(
new ezcConsoleOption(
'f',
'format',
ezcConsoleInput::TYPE_STRING
)
);
$formatOption->shorthelp = "Set output format. Can be 'txt', 'array', 'deadcode', 'umlgraph', 'cga' or one of the formats supported by dot, e.g. png, svg, pdf, ps, ... (see http://graphviz.org/doc/info/output.html)";
$outputfileOption = $input->registerOption(
new ezcConsoleOption(
'o',
'outputfile',
ezcConsoleInput::TYPE_STRING
)
);
$outputfileOption->shorthelp = 'Output file';
$outputDirOption = $input->registerOption(
new ezcConsoleOption(
'u',
'umloutputdir',
ezcConsoleInput::TYPE_STRING
)
);
$outputDirOption->shorthelp = 'UML output directory (only used for the umlgraph output format';
$recursiveOption = $input->registerOption(
new ezcConsoleOption(
'r',
'recursive',
ezcConsoleInput::TYPE_NONE
)
);
$recursiveOption->shorthelp = 'Analyze directories recursive';
$dotcommandOption = $input->registerOption(
new ezcConsoleOption(
'd',
'dotcommand',
ezcConsoleInput::TYPE_STRING
)
);
$dotcommandOption->shorthelp = 'Set dot command';
$ignoreOption = $input->registerOption(
new ezcConsoleOption(
'i',
'ignore',
ezcConsoleInput::TYPE_STRING
)
);
$ignoreOption->shorthelp = 'Set a regular expression for files or folders to ignore';
$noexternalcallsOption = $input->registerOption(
new ezcConsoleOption(
'n',
'noexternalcalls',
ezcConsoleInput::TYPE_NONE
)
);
$noexternalcallsOption->shorthelp = 'Do not show calls to methods or functions which are external to a class';
$phpfunctionsOption = $input->registerOption(
new ezcConsoleOption(
'p',
'phpfunctions',
ezcConsoleInput::TYPE_NONE
)
);
$phpfunctionsOption->shorthelp = 'Show calls to internal PHP functions';
$autoloadOption = $input->registerOption(
new ezcConsoleOption(
'a',
'autoload',
ezcConsoleInput::TYPE_STRING
)
);
$autoloadOption->shorthelp = 'Sets a PHP file with an autoload function which will be included into the sandbox of the InstantSVC CodeAnalyzer';
$verboseOption = $input->registerOption(
new ezcConsoleOption(
'v',
'verbose',
ezcConsoleInput::TYPE_NONE
)
);
$verboseOption->shorthelp = 'Verbose mode for text output format';
$debugOption = $input->registerOption(
new ezcConsoleOption(
'g',
'debug',
ezcConsoleInput::TYPE_NONE
)
);
$debugOption->shorthelp = 'Print debug information (helpful if you get no output at all, since it shows errors during code analysis)';
//TODO: provide option to disable color
//TODO: provide an option to enable the ezc autoloader
$helpOption = $input->registerOption(
new ezcConsoleOption(
'h',
'help'
)
);
$helpOption->isHelpOption = true; // if parameter is set, all options marked as mandatory may be skipped
$helpOption->shorthelp = 'Display help';
$input->argumentDefinition = new ezcConsoleArguments();
$input->argumentDefinition[0] = new ezcConsoleArgument(
'sources',
ezcConsoleInput::TYPE_STRING,
'Files and/or directories to analyze',
'',
true,
true
);
try {
$input->process();
} catch (ezcConsoleOptionException $e) {
die($e->getMessage() . "\nTry option -h to get a list of available options.\n");
} catch (ezcConsoleArgumentMandatoryViolationException $e) {
die($e->getMessage() . "\nTry option -h to get a list of available options.\n");
}
if ($helpOption->value === true) {
echo $input->getHelpText(
"\nPHPCallGraph v" . PHPCallGraph::VERSION . "\n\n"
. "A tool to generate static call graphs for PHP source code.\n"
. "The graphs can be leveraged to gain a better understanding of\n"
. "large software systems or even to debunk design flaws in them."
);
} else {
// configure output driver according to format option
switch ($formatOption->value) {
case 'cga':
require_once 'drivers/CgaStaticTraceDriver.php';
$driver = new CgaStaticTraceDriver();
break;
case 'array':
require_once 'drivers/ArrayDriver.php';
$driver = new ArrayDriver($verboseOption->value);
break;
case 'deadcode':
require_once 'drivers/DeadCodeDriver.php';
$driver = new DeadCodeDriver($verboseOption->value);
break;
case 'umlgraph':
require_once 'drivers/UmlGraphSequenceDiagramDriver.php';
$driver = new UmlGraphSequenceDiagramDriver();
if ($outputDirOption->value !== false) {
$driver->setOutputDirectory($outputDirOption->value);
}
break;
case false:
case 'txt':
require_once 'drivers/TextDriver.php';
$driver = new TextDriver($verboseOption->value);
break;
default:
require_once 'drivers/GraphVizDriver.php';
$driver = new GraphVizDriver();
$driver->setOutputFormat($formatOption->value);
if ($dotcommandOption->value !== false) {
$driver->setDotCommand($dotcommandOption->value);
}
}
// configure generator
$phpcg = new PHPCallGraph($driver);
if ($noexternalcallsOption->value !== false) {
$phpcg->setShowExternalCalls(false);
}
$phpcg->setShowInternalFunctions($phpfunctionsOption->value);
$phpcg->setDebug($debugOption->value);
$phpcg->setAutoloadFile($autoloadOption->value);
// start generation
$phpcg->parse($input->getArguments(), $recursiveOption->value, $ignoreOption->value);
// output result
if ($outputfileOption->value === false) {
echo $phpcg;
} else {
$phpcg->save($outputfileOption->value);
}
}
}
}
?>