summaryrefslogtreecommitdiff
path: root/ext/spl/examples/class_tree.php
blob: a5d2d7b1ca40eb2b821cf101ddfcdae8e641270b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

/** @file   class_tree.php
 * @brief   Class Tree example
 * @ingroup Examples
 * @author  Marcus Boerger
 * @date    2003 - 2008
 * @version 1.1
 *
 * Usage: php class_tree.php \<class\>
 *
 * Simply specify the root class or interface to tree with parameter \<class\>.
 */

if ($argc < 2) {
	echo <<<EOF
Usage: php ${_SERVER['PHP_SELF']} <class>

Displays a graphical tree for the given <class>.

<class> The class or interface for which to generate the tree graph.


EOF;
	exit(1);
}

if (!class_exists("RecursiveTreeIterator", false)) require_once("recursivetreeiterator.inc");

/** \brief Collects sub classes for given class or interface
 */
class SubClasses extends RecursiveArrayIterator
{
	/** @param base  base class to collect sub classes for
	 * @param check_interfaces whether we deal with interfaces
	 */
	function __construct($base, $check_interfaces = false)
	{
		foreach(get_declared_classes() as $cname)
		{
			$parent = get_parent_class($cname);
			if (strcasecmp($parent, $base) == 0)
			{
				$this->offsetSet($cname, new SubClasses($cname));
			}
			if ($check_interfaces)
			{
				if ($parent)
				{
					$parent_imp = class_implements($parent);
				}
				foreach(class_implements($cname) as $iname)
				{
					if (strcasecmp($iname, $base) == 0)
					{
						if (!$parent || !in_array($iname, $parent_imp))
						{
							$this->offsetSet($cname, new SubClasses($cname));
						}
					}
				}
			}
		}
		if ($check_interfaces)
		{
			foreach(get_declared_interfaces() as $cname)
			{
				foreach(class_implements($cname) as $iname)
				{
					if (strcasecmp($iname, $base) == 0)
					{
						$this->offsetSet($cname, new SubClasses($cname, true));
					}
				}
			}
		}
		$this->uksort('strnatcasecmp');
	}

	/** @return key() since that is the name we need
	 */
	function current()
	{
		$result = parent::key();
		$parent = get_parent_class($result);
		if ($parent)
		{
			$interfaces = array_diff(class_implements($result), class_implements($parent));
			if ($interfaces)
			{
				$implements = array();
				foreach($interfaces as $interface)
				{
					$implements = array_merge($implements, class_implements($interface));
				}
				$interfaces = array_diff($interfaces, $implements);
				natcasesort($interfaces);
				$result .= ' (' . join(', ', $interfaces) . ')';
			}
		}
		return $result;
	}
}

$it = new RecursiveTreeIterator(new SubClasses($argv[1], true));

echo $argv[1]."\n";
foreach($it as $c=>$v)
{
	echo "$v\n";
}

?>