Zend - The PHP Company




Miscellaneous

Add Code


Inheritance visualisation  

Type: class library
Added by: cuiztra
Entered: 27/05/2004
Last modified: 06/12/2003
Rating: - (fewer than 3 votes)
Views: 3467
Simple tool to help start documentation of object tree. It shows the inheritance diagram for all declared classes, and shows interface for the classes. Can be used dynamicaly (cached), or it can generate static html documents. In class representation inherited properties and methods are grey. It can be configured to display all declared classes, or only user defined classes, or user defined classes with necessary built in classes due to it's inheritance.


<?php if(!defined("CLASSDOC_CLASS_PHP")) { define("CLASSDOC_CLASS_PHP",true);

if(!
defined("NONE")) define("NONE"0);
if(!
defined("ALL")) define("ALL"1);
if(!
defined("NECESSARY")) define("NECESSARY"2);
if(!
defined("BUILTIN_CLASSES")) define("BUILTIN_CLASSES"ALL);
if(!
defined("TREE_URL")) define("TREE_URL""tree.php");
if(!
defined("STATIC_URL")) define("STATIC_URL""");
if(!
defined("STATIC_PREFIX")) define("STATIC_PREFIX""./static/");
if(!
defined("STATIC_BASE")) define("STATIC_BASE"STATIC_URL."index.html");
if(!
defined("TREE_FILE")) define("TREE_FILE""./tree.dat");
if(!
defined("TEMPLATE_FILE")) define("TEMPLATE_FILE""template.html");
if(!
defined("VERBOSE")) define("VERBOSE",5);

class 
classdoc
{
  private 
$name;
  private 
$children;
  private 
$methods;
  private 
$properties;

  function 
classdoc($name) {$this->setname($name); $this->findmethods(); $this->findproperties();}
  private function 
setname($name) {$this->name $name;}
  private function 
findmethods() {$this->methods get_class_methods($this->name);}
  private function 
findproperties() {$this->properties get_class_vars($this->name);}

  private function 
inheritedproperty($p) {return $this->parent() ? (in_array($pget_class_vars($this->parent())) ? "inherited" "original") : "original";}
  private function 
inheritedmethod($m) {return is_callable(array($this->parent(),$m)) ? "inherited" "original";}
  
  private function 
printmethods()
  { 
$r "<tr><td class='uml'>n";
    if(
$this->methods)
    foreach(
$this->methods as $m)
    
$r .= "<span class='".$this->inheritedmethod($m)."'>function $m()</span><br />n";
    return 
"$r</td></tr>n";
  }
  
  private function 
printproperties()
  { 
$r "<tr><td class='uml'>n";
    if(
$this->properties)
    foreach(
$this->properties as $p => $v)
    
$r .= "<span class='".$this->inheritedproperty($p)."'>var $p</span><br />n";
    return 
"$r</td></tr>n";
  }

  function 
addchild(classdoc $child) {$this->children[$child->getname()] = $child;}
  function 
parent() {return get_parent_class($this->name);}
  function 
getname() {return $this->name;}
  function 
getmethods() {return $this->methods;}
  function 
getproperties() {return $this->properties;}
  function 
url($n false) {if(!$n$n $this->getname(); return TREE_URL."?class=$n";}
  function 
ref($n false) {if(!$n$n $this->getname(); return "<a href='".$this->url($n)."'>$n</a>";}
  
  function 
printself()
  {
    return 
"<dt><table class='uml'><tr><th>class ".$this->getname()."</th></tr>n".$this->printproperties().$this->printmethods()."</table></dt>n";
  }
  

  function 
printtree()
  {
    
$r "--- ".$this->ref()."<br />n";
    if(
$this->children)
    {
      
$r .= "<table class='tree'><tr><td class='tree'>n";
      foreach(
$this->children as $n => $k$r .= $k->printtree();
      
$r .= "</td></tr></table>n";
    }
    return 
$r;
  }
  
  function 
printchildren()
  {
    
$ch = array();
    if(
$this->children) foreach($this->children as $n => $k$ch[] = $this->ref($k->getname());
    if(
$ch) return "Child classes:n".join(",n",$ch)."n<br /><br />n";
  }
  
  function 
printparent()
  {
    if(
$p $this->parent()) return "Parent class: ".$this->ref($p)."<br /><br />";
  }
};

class 
staticclassdoc extends classdoc
{
  function 
url($n false) {if(!$n$n $this->getname(); return STATIC_URL."ref.$n.html";}
}

class 
classtree
{
  protected 
$list;
  private 
$tree;
  
  function 
classtree($ca)
  {
    
$this->buildlist($ca);
    
$this->buildtree();
  }

  function 
newclass($class) {return new classdoc($class);}
    
  function 
buildlist($ca)
  {
    global 
$builtin$this->list = array();
    foreach(
$ca as $class) if((BUILTIN_CLASSES == ALL) || !array_search($class$builtin)) $this->list[$class] = $this->newclass($class);
  }
  
  function 
buildtree()
  {
    
$this->tree = array();
    foreach(
$this->list as $name => $class)
    {
      if((
$parent $class->parent()))
      {
        if(!
$this->list[$parent]) if(BUILTIN_CLASSES == NECESSARY$this->tree[$parent] = $this->list[$parent] = $this->newclass($parent);
        if(
$this->list[$parent]) $this->list[$parent]->addchild($class);
        else 
$this->tree[$name] = $class;
      }
      else 
$this->tree[$name] = $class;
    }
  }
  
  function 
url() {return TREE_URL;}
  
  function 
printleaf($class)
  {
    if(
$display $this->list[$class])
    {
      return 
"<h2>Class details</h2>n<a href='".$this->url()."'>Tree</a><br />n".
      
$display->printself().$display->printparent().$display->printchildren();
    }
  }
  
  function 
printtree()
  {
    
$r .= "<h2>Parenthesis tree</h2>n<dl>n";
    foreach(
$this->tree as $class$r .= $class->printtree();
    return 
$r."</dl>n";
  }
};

class 
onlinetree extends classtree
{
  function 
onlinetree() {$this->classtree(get_declared_classes());}
  
  function 
generate()
  {
    echo 
printtemplate($_GET["class"] ? $this->printleaf($_GET["class"]) : $this->printtree());
  }
}

class 
statictree extends onlinetree
{
  function 
newclass($class) {return new staticclassdoc($class);}
  
  function 
url() {return STATIC_BASE;}

  function 
message($msg$level 0) {if(VERBOSE >= $level) echo $msg; return false;}
  
  function 
savetemplate($file$data)
  {
    
$this->message("Opening output file... <br />n");
    if(!(
$f fopen(STATIC_PREFIX.$file"w")))
       return 
$this->message("<b>Failed to open file ".STATIC_PREFIX.$file." for writing!</b><br />n");
    
$this->message(STATIC_PREFIX.$file." opened for writing.<br />n");
    
fwrite($f$data);
    
$this->message("Data writen to file.<br /><br />n");
    return 
fclose($f);
  }
  
  function 
generate()
  {
    
$this->message("<br />Generating parenthesis tree...<br />n");
    
$this->savetemplate($this->url(), printtemplate($this->printtree()));
    
$this->message("<br />Generating details for classes...<br />n");
//    print_r($this);
    
foreach($this->list as $n => $c$this->savetemplate($c->url(), printtemplate($this->printleaf($n)));
  }
}

function 
treesave($tree$file TREE_FILE)
{
  if(!(
$f fopen($file"w"))) return false;
  
fwrite($f,serialize($tree));
  
fclose($f);
}

function 
treeload($file TREE_FILE)
{
  return 
unserialize(join("",file($file)));
}

function 
printtemplate($data)
{
  return 
str_replace("<content />"$datajoin("",file(TEMPLATE_FILE)));
}

$builtin get_declared_classes();

}


Usage Example


See the example


Rate This Script





Search



This Category All Categories