PHP Classes

File: example2.php

Recommend this page to a friend!
  Classes of Colin McKinnon   Code Graph   example2.php   Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: Usage example
Class: Code Graph
Generate call graphs of PHP code with GraphViz
Author: By
Last change:
Date: 13 years ago
Size: 7,569 bytes
 

Contents

Class file image Download
<?php

/**
  * example2.php
  * formerly parse_gen.php
  *
  * code to create a node graph of a PHP source file
  * C. McKinnon 4 feb 2011
  *
  * this script shows how to set the options on the generated graph
  */

/**
  * init
  */
require_once('dotwriter.inc.php');

set_time_limit(600);
$process_errors=array(); // error handler writes errors here for display later

$controller=new dotrunner();

/**
 * main
 */
if (check_params($controller)) {
        if (
not_cached($_REQUEST['src_file'])) {
               
set_error_handler('error_logging');
       
$source_code=file_get_contents($_REQUEST['src_file']);
               
$outputFileName=$controller->genGraph(
           
$source_code,basename($_REQUEST['src_file']));
               
restore_error_handler();
        }
}
do_page($controller);
$controller->cleanUp();


// --------------------------- end ---------------------------

exit;

/**
 * initialize the dotrunner obj with paramters from $_REQUEST
 */
function check_params($dstObj)
{
        if (
$_REQUEST['src_file'] && $_REQUEST['opt_output']
                        &&
$_REQUEST['opt_undef'] && $_REQUEST['opt_builtin']) {
                if (
file_exists($_REQUEST['src_file'])) {
                        if (
strtoupper(substr($_REQUEST['opt_output'],0,1))=='V') {
                               
$dstObj->options['output']='Vertical';
                        } else {
                               
$dstObj->options['output']='Horizontal';
                        }
                        if (
$_REQUEST['opt_undef']=='YES') {
                               
$dstObj->options['exclude_undefined']=true;
                        } else {
                               
$dstObj->options['exclude_undefined']=false;
                        }
                        if (
$_REQUEST['opt_builtin']=='YES') {
                               
$dstObj->options['exclude_builtins']=true;
                        } else {
                               
$dstObj->options['exclude_builtins']=false;
                        }
           
$dstObj->outputFileName=get_dest_file($_REQUEST['src_file']) . '.jpg';
                        return(
true);
                } else {
                       
trigger_error("File not found");
                }
        }
        return(
false);
}


function
do_page($controller)
{
        print
"<body>\n";
       
do_form();
       
do_result($controller->tmpfile,$controller->cmd, $controller->dotCmdOutput,
           
$_REQUEST['src_file'], $controller->options,
           
$controller->exclude_fns, $controller->builtins,
           
$controller);
        print
"</body>";
}
function
do_form()
{

        print
"<form method='GET'>\n";
        print
"file to parse: <input type='text' name='src_file' value='" . $_REQUEST['src_file'] . "' size='80'><br />\n";
        print
"output vertical or horizontal? <select name='opt_output'>
                <option selected>Vertical</option>
                <option>Horizontal</option>
                </select><br />\n"
;
        print
"exclude undefined functions? <select name='opt_undef'>
                        <option selected>YES</option>
                        <option>NO</option></select><br />\n"
;
        print
"exclude builtin functions? <select name='opt_builtin'>
                     <option selected>YES</option>
                         <option>NO</option></select><br />\n"
;
        print
"<input type='submit' name='submit' value='submit'></form>\n";
}

function
do_result($tmpfile,$cmd,$output,$src_file,$options,$exclude_fns, $builtins, $ctrl)
{
        if (
$_REQUEST['src_file']) {
               
show_errors();
               
$destfile=get_dest_file($_REQUEST['src_file']) . '.jpg';
                if (
file_exists($destfile)) {
                        print
"<h3>$src_file</h3>\n";
                        list(
$lines_processed)=get_lines(false,false,$lines_processed);
                        if (
$lines_processed) {
                                print
"parsed $lines_processed lines from input file<br />\n";
                        }
                        print
"<br /><img src='$destfile' alt='callgraph'>\n";
            print
"<br />This graph was created from the following .dot file:\n";
                } else {
                        print
"expected output $destfile not created<br />\n";
                        print
"failed to generate for some reason";
            print
"<br />dot file shown below:\n";
                }
        print
"<code><pre>" . @file_get_contents($tmpfile) . "</pre></code>\n";
        } else {
                print
"<h3>Key</h3><br /><img src='key.jpg' alt='key'>\n";
        }
}

function
show_errors()
{
        global
$process_errors;
       
$colors=array(
                       
E_ERROR => "FF1010", // red
                       
E_WARNING => "FF3333", // yellow
                       
E_PARSE => "CC00FF", // unlikely
                       
E_NOTICE => "00FFFF",
                       
E_CORE_ERROR => "CC00FF",
                       
E_CORE_WARNING => "FF0000",
                       
E_COMPILE_ERROR => "FF0000",
                       
E_COMPILE_WARNING => "FF9933",
                       
E_USER_ERROR => "FF6666",
                       
E_USER_WARNING => "FFFF33",
                       
E_USER_NOTICE => "CCCCFF",
                       
E_STRICT => "FF99FF"
                       
);
        if (
count($process_errors)) {
                print
"<h3>Errors occurred during processing</h3>\n";
                print
"<table bgcolor='#000000'>";
                foreach(
$process_errors as $item) {
                        list(
$errno, $msg)=$item;
                       
$bg=$colors[$errno];
                        if (!
$bg) $bg='FFFFFF';
                        print
"<tr><td bgcolor='#$bg'>$errno</td><td bgcolor='#$bg'>$msg</td></tr>\n";
                }
                print
"</table>\n";
        }
}

function
error_logging($errno, $errmsg, $filename, $line, $vars)
{
        global
$process_errors;
       
$errortype = array (
                       
E_ERROR => "Error",
                       
E_WARNING => "Warning",
                       
E_PARSE => "Parsing Error",
                       
E_NOTICE => "Notice",
                       
E_CORE_ERROR => "Core Error",
                       
E_CORE_WARNING => "Core Warning",
                       
E_COMPILE_ERROR => "Compile Error",
                       
E_COMPILE_WARNING => "Compile Warning",
                       
E_USER_ERROR => "User Error",
                       
E_USER_WARNING => "User Warning",
                       
E_USER_NOTICE => "User Notice",
                       
E_STRICT => "Runtime Notice"
                       
);
       
$process_errors[]=array($errno, $errortype[$errno] . " $errmsg at $line in $filename");
}

function
get_dest_file($src_file)
{
       
$src_file=serialize($_GET); // different graphs for difft params
       
$dest="graphs/" . md5($src_file);
        return(
$dest);
}

function
clean_cache()
{
// garbage collectio %%% needs to be safer
       
$base=dirname(__FILE__);
        if (
is_dir($base . '/graphs')) {
          
$cmd="find " . $base . '/graphs/' . " -name \*.jpg -mtime +30 -exec rm -f {} \;";
          
$chk=`echo "$cmd" | at now`;
        }
}
function
not_cached($src_file)
{
       
clean_cache();
       
$destfile=get_dest_file($src_file) . '.jpg';
       
$cachetime=(integer)@filemtime($destfile);
       
$codetime=(integer)@filemtime($src_file);
        return(
$cachetime<$codetime);
}