PHP Classes

File: example3.php

Recommend this page to a friend!
  Classes of Colin McKinnon   Code Graph   example3.php   Download  
File: example3.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: 3,431 bytes
 

Contents

Class file image Download
<?php
/**
 * example3.php
 *
 * C.McKinnon 4 Feb 2011
 *
 * generate a clientside mage map as well as the graph
 * the writer needs to provided a callback fn which takes
 * the filename and function name as arguments
 * and returns a URL
 * The example below should return a link to the phpxref documentation
 */
require_once("dotwriter.inc.php");

$basepath="http://example.com/phpxref/output/nav.html?my_project";
   
// above will change depending on how phpxref is installed
    // have a look at the URLs for your current phpxref for more details
$project_basepath=$_SERVER["DOCUMENT_ROOT"];
   
// this gets stripped from the file path - NB your document root might not
    // match the base path for your phpxref project!

$repdirs=parse_truncations();
   
// this deals with 'include' files declared for phpxref

print "<html>\n<body>\n";
if (
count($_POST)) {
    if (
file_exists($_POST['file'])) {
         
show_graph();
    } else {
        print
"file not found (" . $_POST['file'] . ")\n";
    }
}
show_form();
print
"\n</body>\n</html>\n";
register_shutdown_function('deltemps');
flush();
exit;

/**
 * this is the callback sent to the dotwriter
 *
 */
function callback($filename, $fn)
{
    global
$basepath, $repdirs, $project_basepath;
    if (
substr($filename, 0, strlen($project_basepath))==$project_basepath) {
       
$filename=substr($filename,strlen($project_basepath));
    } else {
        foreach (
$repdirs as $r) {
        if (
substr($filename, 0, strlen($r))==$r) {
           
$filename=substr($filename, strlen($r));
            break;
        }
        }
    }
    if (
substr($fn, 0, 2)=='::') $fn=substr($fn,2);
       
// in phpxref, everythings a function!
   
return $basepath . $filename . '.html#' . $fn;
}

function
parse_truncations()
{
   
// normally phpxref strips leading paths off files
    // found on the include path...
   
$paths=explode(':', ini_get('include_path'));
    foreach (
$paths as $key=>$val) {
        if (
$val=='.') unset($paths[$key]);
    }
    return
$paths;
}

function
show_form()
{
    print
"<h2>Enter the path to a file to parse</h2>\n";
    print
'
<form method="post"
enctype="multipart/form-data">
Filename:
<input type="text" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>'
;
   
}

function
show_graph()
{
    global
$imgname;
   
$c = new dotrunner();
   
$tmpname=tempnam('./graphs','graph');
   
$c->outputFileName=$tmpname . '.jpg';
   
$imgname='graphs/' . basename($c->outputFileName);
   
   
$c->genGraph(
       
file_get_contents($_POST['file']),
       
$_POST['file'],
       
'callback'); // final (optional) param is callback fn
            // its possible to specify an object + method too
            // see the PHP manual for call_user_function
   
if (file_exists($c->outputFileName)) {
        print
"<img src='" . $imgname . "' usemap='#" . $_POST['file'] . "'>\n";
       
// the image map name is the same as the title sent to the dotrunner
       
print file_get_contents($imgname . '.cmapx');
       
// and the image map is stored in a file with the same name
        // as the output img - but with an extra suffix
       
@unlink($imgname . '.cmapx');
    } else {
        print
"erm.....something went wrong?";
    }
   
// tempnam creates a file without a suffix - that needs rm'd
   
@unlink($tmpname);
   
$c->cleanUp();
}

function
deltemps()
{
    global
$imgname;
   
sleep(5); // wait for browser to load image
   
if (file_exists($imgname)) {
       
$msg="trying to unlink $imgname";
        @
unlink($imgname);
    } else {
       
$msg="Not deleting anything";
    }
   
$o=fopen($imgname.'.log', 'w');
    if (
$o) {
       
fputs($o, $msg);
       
fclose($o);
    }
}