SPL-StandardPHPLibrary
class_tree.php
Go to the documentation of this file.
00001 <?php
00002 
00015 if ($argc < 2) {
00016     echo <<<EOF
00017 Usage: php ${_SERVER['PHP_SELF']} <class>
00018 
00019 Displays a graphical tree for the given <class>.
00020 
00021 <class> The class or interface for which to generate the tree graph.
00022 
00023 
00024 EOF;
00025     exit(1);
00026 }
00027 
00028 if (!class_exists("RecursiveTreeIterator", false)) require_once("recursivetreeiterator.inc");
00029 
00032 class SubClasses extends RecursiveArrayIterator
00033 {
00037     function __construct($base, $check_interfaces = false)
00038     {
00039         foreach(get_declared_classes() as $cname)
00040         {
00041             $parent = get_parent_class($cname);
00042             if (strcasecmp($parent, $base) == 0)
00043             {
00044                 $this->offsetSet($cname, new SubClasses($cname));
00045             }
00046             if ($check_interfaces)
00047             {
00048                 if ($parent)
00049                 {
00050                     $parent_imp = class_implements($parent);
00051                 }
00052                 foreach(class_implements($cname) as $iname)
00053                 {
00054                     if (strcasecmp($iname, $base) == 0)
00055                     {
00056                         if (!$parent || !in_array($iname, $parent_imp))
00057                         {
00058                             $this->offsetSet($cname, new SubClasses($cname));
00059                         }
00060                     }
00061                 }
00062             }
00063         }
00064         if ($check_interfaces)
00065         {
00066             foreach(get_declared_interfaces() as $cname)
00067             {
00068                 foreach(class_implements($cname) as $iname)
00069                 {
00070                     if (strcasecmp($iname, $base) == 0)
00071                     {
00072                         $this->offsetSet($cname, new SubClasses($cname, true));
00073                     }
00074                 }
00075             }
00076         }
00077         $this->uksort('strnatcasecmp');
00078     }
00079 
00082     function current()
00083     {
00084         $result = parent::key();
00085         $parent = get_parent_class($result);
00086         if ($parent)
00087         {
00088             $interfaces = array_diff(class_implements($result), class_implements($parent));
00089             if ($interfaces)
00090             {
00091                 $implements = array();
00092                 foreach($interfaces as $interface)
00093                 {
00094                     $implements = array_merge($implements, class_implements($interface));
00095                 }
00096                 $interfaces = array_diff($interfaces, $implements);
00097                 natcasesort($interfaces);
00098                 $result .= ' (' . join(', ', $interfaces) . ')';
00099             }
00100         }
00101         return $result;
00102     }
00103 }
00104 
00105 $it = new RecursiveTreeIterator(new SubClasses($argv[1], true));
00106 
00107 echo $argv[1]."\n";
00108 foreach($it as $c=>$v)
00109 {
00110     echo "$v\n";
00111 }
00112 
00113 ?>