Location: PHPKode > scripts > Code Map > code-map/code_map.php
<?php
/* Code_Map class - can be used for viewed hierarchy of classes with methods and properties,functions,constants and variables.
for classes and functions also show location in files
work via 2 points - one on start selected code, second on finish
*/

/*Code_Map must be called with options:*/
#0-draw classes herarhy
#1-draw classes+methods
#2-draw classes+methods+properties
#3-draw classes+properties
#4-draw classes+functions+included_files
#5-draw classes+constants+variables

/*Use: put 2 points*/
/*
1.) include_once "code_map.php";

2.) $map->vars=get_defined_vars();
    $map->map($option);
there option is num 0,1,2,3,4,5
----------------------------------------

into map php after $map=new Code_Map;
of in you code after include_once "map.php"
use $map->my_ip[]='10.0.1.95';
for hidd map for another ip
on default - visible for everybody
*/

//first auto-points
$vars_before=get_defined_vars();

Class Code_Map
{
    var $old_map,$classes,$option,$old_constants,$vars,$my_ip,$files,$cache,$used_classes=array();
    function Code_Map()
        {
            //take first auto        -point
            $this->old_map=get_declared_classes();
            $this->old_constants=get_defined_constants();
        }
    function map($option=0)
        {
        //ip access-block
        if(isset($this->my_ip)&&(count($this->my_ip)>0))
                {
                if(!in_array($_SERVER['REMOTE_ADDR'],$this->my_ip))return false;
                }
        //make list of included files
        $this->files=get_included_files();
        //make code cache
        $this->make_cache();

        $this->option=$option;
        //get actual classes
        $classes=array_diff(get_declared_classes(),$this->old_map);
        sort($classes);
        $this->classes=$this->make_map($classes);
        //enter into rekurciv func for get and draw map
        ?>
        <style>
        .main_header{
        background-color:#FFFFDF;
        margin:0px;
        color:navy;
        text-align:center;
        }
        .class_ul{
        text-align:left;
        border-left:dotted red 1px;
        border-bottom:dotted red 1px;
        margin:0px;
        padding-left:15px;
        background-color:#FFFFDF;
        }
        .class_li{
        color:navy;
        margin-bottom:2px;
        }
        .methods_ol{
        text-align:left;
        border:solid #8EA7AF 1px;
        color:#8EA7AF;
        font-size:13px;
        }
        .methods_header{
        margin:0px;
        color:#9F7645;
        font-style:italic;
        font-size:10px;
        }
        .prop_ul{
        text-align:left;
        border:solid #CFFFE3 1px;
        color:#4F9F70;
        font-size:13px;
        }
        .adress{
        font-size:10px;
        background-color:#FFEFF2;
        }
        .files_ol{
        text-align:left;
        background-color:#FFF;
        color:navy;
        border:solid navy 1px;
        }
        .some_header{
        margin:0px;
        font-style:italic;
        }
        .func_ul{
        text-align:left;
        border:solid black 1px;
        background-color:white;
        list-style:none;
        }
        .constants{
        text-align:left;
        border:solid navy 1px;
        background-color:#F6FFDF;
        list-style:none;
        font-size:13px;
        }
        </style>
        <?
        print "<h4 class='main_header'>Code Map</h4>";
        $this->draw_map($this->classes);

            if($this->option==4)
                {
                //draw functions
                $this->get_user_funcs();
                //draw included files
                $this->get_included();
                }elseif($this->option==5)
                {
                //draw constants
                $this->get_constants();
                //draw varibles
                //work only if used $map->vars=get_defined_vars(); for second point
                $this->get_vars();
                }
        }
   function make_map($classes)
        {
        /*func sort classes for tree- before classes with-out parent, after have parents*/
        foreach($classes as $class)
                {
                if(get_parent_class($class))$second[]=$class;
                else $first[]=$class;
                }
        if((isset($second))&&(isset($first)))$res=array_merge($first,$second);
        elseif(isset($second))$res=$second;
        elseif(isset($first))$res=$first;
        else return false;

        return $res;
        }
    function draw_map($classes)
        {
        //in use for recurcive draw classes-map tree
           if($classes!=false)
                {
                print "<ul class='class_ul'>";
                for($i=0;$i<count($classes);$i++)
                        {
                        #print_r($this->used_classes);
                        #
                                if(!in_array($classes[$i],$this->used_classes))
                                {
                                print "<li class='class_li'>Class ".$classes[$i];
                                //get file there declared
                                print $this->where_is_obj($classes[$i],"Class")."<br>";
                                //get class properties
                                if(($this->option>1)&&($this->option<4))$this->get_properties($classes[$i]);
                                //get class methods
                                if(($this->option>0)&&($this->option<3))$this->get_methods($classes[$i]);
                                //try get childrens of class
                                $childs=$this->get_childs($classes[$i]);
                                //jump again for childrens
                                if(count($childs)>0)$this->draw_map($childs);

                                }
                        //remember drawed class, for hide in future
                        $this->used_classes[]=$classes[$i];
                        }
                print "</ul>";
                }
        }
    function get_childs($class)
        {
        //return childrens of class, there $class if parent
            for($i=0;$i<count($this->classes);$i++)
                {
                   if(get_parent_class($this->classes[$i])==$class)
                        {
                        $childs[]=$this->classes[$i];
                        }
                }
        if((isset($childs))&&(count($childs)>0))return $childs;
        else return false;
        }
    function get_methods($class)
        {
        //draw methods of $class
        $funcs=get_class_methods($class);
                if(count($funcs)>0)
                {
                print "<ol class='methods_ol'>";
                print "<h5 class='methods_header'>Methods:</h5>";
                foreach($funcs as $func)
                        {
                        print "<li>".$func."</li>";
                        }
                print "</ol>";

                }else return false;
        }
    function get_properties($class)
        {
        //draw class properties
        $vars=get_class_vars($class);
                if(count($vars)>0)
                {
                print "<ul class='prop_ul'>";
                print "<h6 class='methods_header'>Properties</h6>";
                foreach($vars as $k=>$v)
                        {
                        if(is_array($v))
                                {
                                print "<li>".$k." = <pre style='color:red;'>";
                                print_r($v);
                                print "</pre></li>";
                                }else
                                print "<li>".$k." = <font style='color:red;'>".$v."</font></li>";
                        }
                print "</ul>";
                }
        }
    function make_cache()
        {
        //read code from included files and make code-cache
        foreach($this->files as $f)
                {
                $cache[$f]=file_get_contents($f);
                }
        $this->cache=$cache;
        }
     function where_is_obj($name,$elem)
        {
        //search into code-cache current class/func
        foreach($this->cache as $k => $v)
                {
                if(stristr($v,$elem." ".$name))return " <span class='adress'>{".$k."}</span>";
                }
        return false;
        }
    function get_included()
        {
        //draw included files
                $files=$this->files;
                print "<ol class='files_ol'>";
                print "<h6 class='some_header'>Included:</h6>";
                foreach($files as $f)
                        {
                        print "<li>".$f."</li>\n";
                        }
                print "<ol>";
        }

    function get_user_funcs()
        {
        //draw functions
        $funcs=get_defined_functions();
        print "<ul class='func_ul'>";
        print "<h5 class='some_header'>Used Funcs:</h5>";
                foreach($funcs['user'] as $fun)
                {
                print "<li>".$fun."()".$this->where_is_obj($fun,"function");
                print "</li>";
                }
        print "</ul>";
        }


   function get_constants()
        {
        //draw constants
        $const=array_diff(get_defined_constants(),$this->old_constants);
        print "<ul class='constants'>";
        print "<h6 class='some_header'>Constants:</h6>";
        foreach($const as $k=>$v)print "<li><span style='color:navy;'>$k</span>=</span style='color:red;'>$v</span></li>";
        print "</ul>";
        }
   function get_vars()
        {
        //draw array of variables
        global $vars_before;
        //filtr
        $res=array_diff($this->vars['GLOBALS'],$vars_before);
        unset($res['map']);

        print "<h6 class='some_header'>Variables:</h6>";
        print "<pre><textarea style='border:solid red 1px;background-color:#FFF;color:black;font-size:11px;width:100%;height:400px;'>";
        print_r($res);
        print "</textarea></pre>";
        }
}
$map=new Code_Map;
//if you add ip here .....so map will be viewed only for current ip ....if not - view for anybody
#$map->my_ip[]='10.0.1.95';

/*
Roman Shneer hide@address.com
-=SHaMaN=- 16-08-06
*/
?>
Return current item: Code Map