2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ClassInfo {
|
|
|
|
/**
|
|
|
|
* Returns true if the manifest has actually been built.
|
|
|
|
*/
|
|
|
|
static function ready() {
|
|
|
|
global $_ALL_CLASSES;
|
2008-02-25 03:10:37 +01:00
|
|
|
return $_ALL_CLASSES && $_ALL_CLASSES['hastable'];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Improve documentation
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function allClasses() {
|
|
|
|
global $_ALL_CLASSES;
|
|
|
|
return $_ALL_CLASSES['exists'];
|
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Improve documentation
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function exists($class) {
|
|
|
|
global $_ALL_CLASSES;
|
|
|
|
return isset($_ALL_CLASSES['exists'][$class]) ? $_ALL_CLASSES['exists'][$class] : null;
|
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Improve documentation
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
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
|
2008-04-14 06:49:08 +02:00
|
|
|
*
|
|
|
|
* @param mixed $class string of the classname or instance of the class
|
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function dataClassesFor($class) {
|
|
|
|
global $_ALL_CLASSES;
|
2008-04-14 06:49:08 +02:00
|
|
|
if (is_object($class)) $class = get_class($class);
|
|
|
|
|
2008-07-16 05:29:20 +02:00
|
|
|
$dataClasses = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
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.
|
2008-04-14 06:49:08 +02:00
|
|
|
*
|
|
|
|
* @param mixed $class string of the classname or instance of the class
|
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function baseDataClass($class) {
|
|
|
|
global $_ALL_CLASSES;
|
2008-04-14 06:49:08 +02:00
|
|
|
if (is_object($class)) $class = get_class($class);
|
2007-07-19 12:40:28 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2008-04-14 06:49:08 +02:00
|
|
|
* Returns a list of classes that inherit from the given class.
|
|
|
|
*
|
|
|
|
* @param mixed $class string of the classname or instance of the class
|
|
|
|
* @return array
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function subclassesFor($class){
|
|
|
|
global $_ALL_CLASSES;
|
2008-04-14 06:49:08 +02:00
|
|
|
if (is_object($class)) $class = get_class($class);
|
2007-07-19 12:40:28 +02:00
|
|
|
$subclasses = isset($_ALL_CLASSES['children'][$class]) ? $_ALL_CLASSES['children'][$class] : null;
|
|
|
|
if(isset($subclasses)) array_unshift($subclasses, $class);
|
|
|
|
else $subclasses[$class] = $class;
|
|
|
|
return $subclasses;
|
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* @todo Improve documentation
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function ancestry($class, $onlyWithTables = false) {
|
|
|
|
global $_ALL_CLASSES;
|
2008-08-12 04:51:33 +02:00
|
|
|
|
|
|
|
if(is_object($class)) $class = $class->class;
|
|
|
|
else if(!is_string($class)) user_error("Bad class value " . var_export($class, true) . " passed to ClassInfo::ancestry()", E_USER_WARNING);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = $_ALL_CLASSES['parents'][$class];
|
|
|
|
$items[$class] = $class;
|
|
|
|
if($onlyWithTables) foreach($items as $item) {
|
2008-08-08 05:52:48 +02:00
|
|
|
if(!isset($_ALL_CLASSES['hastable'][$item]) || !$_ALL_CLASSES['hastable'][$item]) unset($items[$item]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* @return array A self-keyed array of class names. Note that this is only available with Silverstripe
|
|
|
|
* classes and not built-in PHP classes.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function implementorsOf($interfaceName) {
|
2008-02-25 03:10:37 +01:00
|
|
|
global $_ALL_CLASSES;
|
|
|
|
return (isset($_ALL_CLASSES['implementors'][$interfaceName])) ? $_ALL_CLASSES['implementors'][$interfaceName] : false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given class implements the given interface
|
|
|
|
*/
|
|
|
|
static function classImplements($className, $interfaceName) {
|
|
|
|
global $_ALL_CLASSES;
|
|
|
|
return isset($_ALL_CLASSES['implementors'][$interfaceName]) ? in_array($className, $_ALL_CLASSES['implementors'][$interfaceName]) : false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-08 05:52:48 +02:00
|
|
|
?>
|