silverstripe-framework/core/ClassInfo.php
Sam Minnee 5a98e83f7a API CHANGE: Updated DB::manipulate() to be more db-agnostic.
API CHANGE: Moved the meat of SQLQuery's SQL generation into Database::sqlQueryToString(), to be overloaded by different database implementations.
API CHANGE: Moved the implementation of ClassInfo::hasTable() and ClassInfo::getValidSubClasses() into Database::hasTable() and Database::enumValuesForField(), to be overloaded by different database implementations.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@66422 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-11-23 23:20:02 +00:00

174 lines
5.1 KiB
PHP
Executable File

<?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.
*
* @package sapphire
* @subpackage core
*/
class ClassInfo {
/**
* @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 Move this to Database or DB
*/
static function hasTable($class) {
if(DB::isActive()) {
return DB::getConn()->hasTable($class);
} else {
return false;
}
}
/**
* Returns the manifest of all classes which are present in the database.
*/
static function getValidSubClasses(){
return DB::getConn()->enumValuesForField("SiteTree", "ClassName");
}
/**
* 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
*
* @param mixed $class string of the classname or instance of the class
* @todo Move this into data object
* @return array
*/
static function dataClassesFor($class) {
global $_ALL_CLASSES;
if (is_object($class)) $class = get_class($class);
$dataClasses = array();
if(!$_ALL_CLASSES['parents'][$class]) user_error("ClassInfo::dataClassesFor() no parents for $class", E_USER_WARNING);
foreach($_ALL_CLASSES['parents'][$class] as $subclass) {
if(DataObject::has_own_table($subclass)) $dataClasses[] = $subclass;
}
if(DataObject::has_own_table($class)) $dataClasses[] = $class;
if(isset($_ALL_CLASSES['children'][$class]))
foreach($_ALL_CLASSES['children'][$class] as $subclass)
{
if(DataObject::has_own_table($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.
*
* @param mixed $class string of the classname or instance of the class
* @return array
*/
static function baseDataClass($class) {
global $_ALL_CLASSES;
if (is_object($class)) $class = get_class($class);
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;
}
/**
* 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
*/
static function subclassesFor($class){
global $_ALL_CLASSES;
if (is_object($class)) $class = get_class($class);
$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_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);
$items = $_ALL_CLASSES['parents'][$class];
$items[$class] = $class;
if($onlyWithTables) foreach($items as $item) {
if(!DataObject::has_own_table($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;
}
/**
* 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;
}
/**
* Get all classes contained in a file.
* @uses ManifestBuilder
*
* @param string $filePath Path to a PHP file (absolute or relative to webroot)
* @return array
*/
static function classes_for_file($filePath) {
$absFilePath = Director::getAbsFile($filePath);
global $_CLASS_MANIFEST;
$matchedClasses = array();
foreach($_CLASS_MANIFEST as $class => $compareFilePath) {
if($absFilePath == $compareFilePath) $matchedClasses[] = $class;
}
return $matchedClasses;
}
/////////////////////////////////////////////////////////////////////////////
// DEPRECATED
/**
* @deprecated Use Security::database_is_ready() instead.
*/
static function ready() {
user_error("ClassInfo::ready() deprectaed - use Security::database_is_ready()", E_USER_NOTICE);
return Security::database_is_ready();
}
}
?>