SPL-StandardPHPLibrary
class_tree.php
Go to the documentation of this file.
1 <?php
2 
15 if ($argc < 2) {
16  echo <<<EOF
17 Usage: php ${_SERVER['PHP_SELF']} <class>
18 
19 Displays a graphical tree for the given <class>.
20 
21 <class> The class or interface for which to generate the tree graph.
22 
23 
24 EOF;
25  exit(1);
26 }
27 
28 if (!class_exists("RecursiveTreeIterator", false)) require_once("recursivetreeiterator.inc");
29 
33 {
37  function __construct($base, $check_interfaces = false)
38  {
39  foreach(get_declared_classes() as $cname)
40  {
41  $parent = get_parent_class($cname);
42  if (strcasecmp($parent, $base) == 0)
43  {
44  $this->offsetSet($cname, new SubClasses($cname));
45  }
46  if ($check_interfaces)
47  {
48  if ($parent)
49  {
50  $parent_imp = class_implements($parent);
51  }
52  foreach(class_implements($cname) as $iname)
53  {
54  if (strcasecmp($iname, $base) == 0)
55  {
56  if (!$parent || !in_array($iname, $parent_imp))
57  {
58  $this->offsetSet($cname, new SubClasses($cname));
59  }
60  }
61  }
62  }
63  }
64  if ($check_interfaces)
65  {
66  foreach(get_declared_interfaces() as $cname)
67  {
68  foreach(class_implements($cname) as $iname)
69  {
70  if (strcasecmp($iname, $base) == 0)
71  {
72  $this->offsetSet($cname, new SubClasses($cname, true));
73  }
74  }
75  }
76  }
77  $this->uksort('strnatcasecmp');
78  }
79 
82  function current()
83  {
84  $result = parent::key();
85  $parent = get_parent_class($result);
86  if ($parent)
87  {
88  $interfaces = array_diff(class_implements($result), class_implements($parent));
89  if ($interfaces)
90  {
91  $implements = array();
92  foreach($interfaces as $interface)
93  {
94  $implements = array_merge($implements, class_implements($interface));
95  }
96  $interfaces = array_diff($interfaces, $implements);
97  natcasesort($interfaces);
98  $result .= ' (' . join(', ', $interfaces) . ')';
99  }
100  }
101  return $result;
102  }
103 }
104 
105 $it = new RecursiveTreeIterator(new SubClasses($argv[1], true));
106 
107 echo $argv[1]."\n";
108 foreach($it as $c=>$v)
109 {
110  echo "$v\n";
111 }
112 
113 ?>
uksort(mixed cmp_function)
Sort the entries by key using user defined function.
Definition: spl.php:785
RecursiveIteratorIterator to generate ASCII graphic trees for the entries in a RecursiveIterator.
offsetSet($index, $newval)
Definition: spl.php:808
__construct($base, $check_interfaces=false)
Definition: class_tree.php:37
$it
Definition: class_tree.php:105
A recursive array iterator.
Collects sub classes for given class or interface.
Definition: class_tree.php:32
natcasesort()
Sort the entries by values using case insensitive &quot;natural order&quot; algorithm.
Definition: spl.php:793