/
ClassInfo.php
executable file
·140 lines (124 loc) · 3.77 KB
/
ClassInfo.php
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
<?php
/**
* @package sapphire
* @subpackage core
*/
/**
* Provides introspection information about the class tree.
* It's a cached wrapper around the built-in class functions. Sapphire uses class introspection heavily
* and without the caching it creates an unfortunate performance hit.
*
* @package sapphire
* @subpackage core
*/
class ClassInfo {
/**
* Returns true if the manifest has actually been built.
*/
static function ready() {
global $_ALL_CLASSES;
return $_ALL_CLASSES && $_ALL_CLASSES['hastable'];
}
/**
* @todo Improve documentation
*/
static function allClasses() {
global $_ALL_CLASSES;
return $_ALL_CLASSES['exists'];
}
/**
* @todo Improve documentation
*/
static function exists($class) {
global $_ALL_CLASSES;
return isset($_ALL_CLASSES['exists'][$class]) ? $_ALL_CLASSES['exists'][$class] : null;
}
/**
* @todo Improve documentation
*/
static function hasTable($class) {
global $_ALL_CLASSES;
return isset($_ALL_CLASSES['hastable'][$class]) ? $_ALL_CLASSES['hastable'][$class] : null;
}
/**
* Returns the manifest of all classes which are present in the database.
*/
static function getValidSubClasses(){
// Get the enum of all page types from the SiteTree table
$classnameinfo = DB::query("DESCRIBE SiteTree ClassName")->first();
preg_match_all("/'[^,]+'/", $classnameinfo["Type"], $matches);
foreach($matches[0] as $value) {
$classes[] = trim($value, "'");
}
return $classes;
}
/**
* Return the database tables linked to this class.
* Gets an array of the current class, it subclasses and its ancestors. It then filters that list
* to those with DB tables
*/
static function dataClassesFor($class) {
global $_ALL_CLASSES;
if(!$_ALL_CLASSES['parents'][$class]) user_error("ClassInfo::dataClassesFor() no parents for $class", E_USER_WARNING);
foreach($_ALL_CLASSES['parents'][$class] as $subclass) {
if(isset($_ALL_CLASSES['hastable'][$subclass])){
$dataClasses[] = $subclass;
}
}
if(isset($_ALL_CLASSES['hastable'][$class])) $dataClasses[] = $class;
if(isset($_ALL_CLASSES['children'][$class]))
foreach($_ALL_CLASSES['children'][$class] as $subclass)
{
if(isset($_ALL_CLASSES['hastable'][$subclass]))
{
$dataClasses[] = $subclass;
}
}
return $dataClasses;
}
/**
* Return the root data class for that class.
* This root table has a lot of special use in the DataObject system.
*/
static function baseDataClass($class) {
global $_ALL_CLASSES;
reset($_ALL_CLASSES['parents'][$class]);
while($val = next($_ALL_CLASSES['parents'][$class])) {
if($val == 'DataObject') break;
}
$baseDataClass = next($_ALL_CLASSES['parents'][$class]);
return $baseDataClass ? $baseDataClass : $class;
}
/**
* @todo Improve documentation
*/
static function subclassesFor($class){
global $_ALL_CLASSES;
$subclasses = isset($_ALL_CLASSES['children'][$class]) ? $_ALL_CLASSES['children'][$class] : null;
if(isset($subclasses)) array_unshift($subclasses, $class);
else $subclasses[$class] = $class;
return $subclasses;
}
/**
* @todo Improve documentation
*/
static function ancestry($class, $onlyWithTables = false) {
global $_ALL_CLASSES;
if(!is_string($class)) $class = $class->class;
$items = $_ALL_CLASSES['parents'][$class];
$items[$class] = $class;
if($onlyWithTables) foreach($items as $item) {
if(!$_ALL_CLASSES['hastable'][$item]) unset($items[$item]);
}
return $items;
}
/**
* @return array A self-keyed array of class names. Note that this is only available with Silverstripe
* classes and not built-in PHP classes.
*/
static function implementorsOf($interfaceName) {
global $_ALL_CLASSES;
return (isset($_ALL_CLASSES['implementors'][$interfaceName])) ? $_ALL_CLASSES['implementors'][$interfaceName] : false;
}
}
?>