summaryrefslogtreecommitdiff
path: root/buildscripts/classtree/build.php
blob: bc95d1c8b9ce82751bf11b3445a6f1f0e5d008b9 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

$rootPath=dirname(__FILE__).'/../../framework';
require_once($rootPath.'/prado.php');
$exclusions=array(
	'prado.php',
	'pradolite.php',
	'PradoBase.php',
	'clientscripts.php',
	'.svn',
	'/I18N/core',
	'/3rdParty',
	);
$a=new ClassTreeBuilder($rootPath,$exclusions);
$a->buildTree();
$a->saveToFile('classes.data');

class ClassTreeBuilder
{
	const REGEX_RULES='/^\s*(abstract\s+)?class\s+(\w+)(\s+extends\s+(\w+)\s*|\s*)/msS';
	private $_basePath;
	private $_exclusions;
	private $_classes=array();

	public function __construct($basePath,$exclusions)
	{
		$this->_basePath=realpath($basePath);
		$this->_exclusions=array();
		foreach($exclusions as $exclusion)
		{
			if($exclusion[0]==='/')
				$this->_exclusions[realpath($basePath.'/'.$exclusion)]=true;
			else
				$this->_exclusions[$exclusion]=true;
		}
	}

	public function buildTree()
	{
		$sourceFiles=$this->getSourceFiles($this->_basePath);
		foreach($sourceFiles as $sourceFile)
			$this->parseFile($sourceFile);
		ksort($this->_classes);
		foreach(array_keys($this->_classes) as $className)
		{
			$parentClass=$this->_classes[$className]['ParentClass'];
			if(isset($this->_classes[$parentClass]))
				$this->_classes[$parentClass]['ChildClasses'][]=$className;
		}
		echo "\nClass tree built successfully. Total ".count($this->_classes)." classes found.\n";
	}

	public function saveToFile($fileName)
	{
		file_put_contents($fileName,serialize($this->_classes));
	}

	public function displayTree()
	{
		$this->displayTreeInternal(array_keys($this->_baseClasses),0);
	}

	public function displayTreeInternal($classNames,$level)
	{
		foreach($classNames as $className)
		{
			echo str_repeat(' ',$level*4);
			echo $className.':'.$this->_classes[$className]->Package."\n";
			$this->displayTreeInternal(array_keys($this->_classes[$className]->ChildClasses),$level+1);
		}
	}

	protected function parseFile($sourceFile)
	{
		include_once($sourceFile);
		$classFile=strtr(substr($sourceFile,strlen($this->_basePath)),'\\','/');
		echo "Parsing $classFile...\n";
		$content=file_get_contents($sourceFile);
		if(preg_match('/@package\s+([\w\.]+)\s*/msS',$content,$matches)>0)
			$package=$matches[1];
		else
			$package='';
		$n=preg_match_all(self::REGEX_RULES,$content,$matches,PREG_SET_ORDER);
		for($i=0;$i<$n;++$i)
		{
			$className=$matches[$i][2];
			if(isset($this->_classes[$className]))
				throw new Exception("Class $className is defined in both $sourceFile and ".$this->_classes[$className]->ClassFile);
			$c=new TComponentReflection($className);
			$properties=$c->getProperties();
			$this->parseMethodComments($properties);
			$events=$c->getEvents();
			$this->parseMethodComments($events);
			$methods=$c->getMethods();
			$this->parseMethodComments($methods);
			$this->_classes[$className]=array(
				'ClassFile'=>$classFile,
				'Package'=>$package,
				'ParentClass'=>isset($matches[$i][4])?$matches[$i][4]:'',
				'ChildClasses'=>array(),
				'Properties'=>$properties,
				'Events'=>$events,
				'Methods'=>$methods);
		}
	}

	protected function parseMethodComments(&$methods)
	{
		foreach(array_keys($methods) as $key)
		{
			$method=&$methods[$key];
			$comments=$method['comments'];
			$s='';
			foreach(explode("\n",$comments) as $line)
			{
				$line=trim($line);
				$line=trim($line,'/*');
				$s.=' '.$line;
			}
			$s=trim($s);
			$s=preg_replace('/\{@link.*?([\w\(\)]+)\}/i','$1',$s);
			$pos1=strpos($s,'@');
			$pos2=strpos($s,'.');
			if($pos1===false)
			{
				if($pos2!==false)
					$method['comments']=substr($s,0,$pos2);
				else
					$method['comments']=$s;
			}
			else if($pos1>0)
			{
				if($pos2 && $pos2<$pos1)	// use the first line as comment
					$method['comments']=substr($s,0,$pos2);
				else
					$method['comments']=substr($s,0,$pos1);
			}
			else
			{
				$matches=array();
				if(preg_match('/@return\s+[\w\|]+\s+([^\.]*)/',$s,$matches)>0)
					$method['comments']=$matches[1];
				else
					$method['comments']='';
			}
		}
	}

	protected function isValidPath($path)
	{
		if(is_dir($path))
			return !isset($this->_exclusions[basename($path)]) && !isset($this->_exclusions[$path]);
		else
			return basename($path)!==basename($path,'.php') && !isset($this->_exclusions[basename($path)]);
	}

	public function getSourceFiles($path)
	{
		$files=array();
		$folder=opendir($path);
		while($file=readdir($folder))
		{
			if($file==='.' || $file==='..')
				continue;
			$fullPath=realpath($path.'/'.$file);
			if($this->isValidPath($fullPath))
			{
				if(is_file($fullPath))
					$files[]=$fullPath;
				else
					$files=array_merge($files,$this->getSourceFiles($fullPath));
			}
		}
		closedir($folder);
		return $files;
	}

	public function saveAsTagLib($fileName)
	{
	}
}

?>