mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
0b1f297873
Conflicts: .travis.yml README.md admin/code/LeftAndMain.php admin/css/screen.css admin/scss/screen.scss api/RestfulService.php conf/ConfigureFromEnv.php control/injector/ServiceConfigurationLocator.php control/injector/SilverStripeServiceConfigurationLocator.php core/ClassInfo.php core/Object.php css/AssetUploadField.css css/ComplexTableField_popup.css dev/CSSContentParser.php dev/DevelopmentAdmin.php docs/en/changelogs/index.md docs/en/misc/contributing/code.md docs/en/reference/execution-pipeline.md filesystem/GD.php filesystem/ImagickBackend.php filesystem/Upload.php forms/Form.php forms/FormField.php forms/HtmlEditorConfig.php forms/gridfield/GridFieldDetailForm.php forms/gridfield/GridFieldSortableHeader.php lang/en.yml model/Aggregate.php model/DataList.php model/DataObject.php model/DataQuery.php model/Image.php model/MySQLDatabase.php model/SQLQuery.php model/fieldtypes/HTMLText.php model/fieldtypes/Text.php scss/AssetUploadField.scss search/filters/SearchFilter.php security/Authenticator.php security/LoginForm.php security/Member.php security/MemberAuthenticator.php security/MemberLoginForm.php security/Security.php tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php tests/control/HTTPTest.php tests/control/RequestHandlingTest.php tests/filesystem/UploadTest.php tests/forms/FormTest.php tests/forms/NumericFieldTest.php tests/model/DataListTest.php tests/model/DataObjectTest.php tests/model/TextTest.php tests/security/MemberAuthenticatorTest.php tests/security/SecurityDefaultAdminTest.php tests/view/SSViewerCacheBlockTest.php tests/view/SSViewerTest.php
289 lines
8.0 KiB
PHP
289 lines
8.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Provides introspection information about the class tree.
|
|
*
|
|
* It's a cached wrapper around the built-in class functions. SilverStripe uses
|
|
* class introspection heavily and without the caching it creates an unfortunate
|
|
* performance hit.
|
|
*
|
|
* @package framework
|
|
* @subpackage core
|
|
*/
|
|
class ClassInfo {
|
|
|
|
/**
|
|
* Wrapper for classes getter.
|
|
*/
|
|
public static function allClasses() {
|
|
return SS_ClassLoader::instance()->getManifest()->getClasses();
|
|
}
|
|
|
|
/**
|
|
* @todo Improve documentation
|
|
*/
|
|
public static function exists($class) {
|
|
return SS_ClassLoader::instance()->classExists($class);
|
|
}
|
|
|
|
/**
|
|
* Cache for {@link hasTable()}
|
|
*/
|
|
private static $_cache_all_tables = array();
|
|
|
|
/**
|
|
* @var Array Cache for {@link ancestry()}.
|
|
*/
|
|
private static $_cache_ancestry = array();
|
|
|
|
/**
|
|
* @todo Move this to SS_Database or DB
|
|
*/
|
|
public static function hasTable($class) {
|
|
// Cache the list of all table names to reduce on DB traffic
|
|
if(empty(self::$_cache_all_tables) && DB::is_active()) {
|
|
self::$_cache_all_tables = DB::get_schema()->tableList();
|
|
}
|
|
return !empty(self::$_cache_all_tables[strtolower($class)]);
|
|
}
|
|
|
|
public static function reset_db_cache() {
|
|
self::$_cache_all_tables = null;
|
|
self::$_cache_ancestry = array();
|
|
}
|
|
|
|
/**
|
|
* Returns the manifest of all classes which are present in the database.
|
|
*
|
|
* @param string $class Class name to check enum values for ClassName field
|
|
* @param boolean $includeUnbacked Flag indicating whether or not to include
|
|
* types that don't exist as implemented classes. By default these are excluded.
|
|
* @return array List of subclasses
|
|
*/
|
|
public static function getValidSubClasses($class = 'SiteTree', $includeUnbacked = false) {
|
|
$classes = DB::get_schema()->enumValuesForField($class, 'ClassName');
|
|
if (!$includeUnbacked) $classes = array_filter($classes, array('ClassInfo', 'exists'));
|
|
return $classes;
|
|
}
|
|
|
|
/**
|
|
* Returns an array of the current class and all its ancestors and children
|
|
* which require a DB table.
|
|
*
|
|
* @param string|object $class
|
|
* @todo Move this into data object
|
|
* @return array
|
|
*/
|
|
public static function dataClassesFor($class) {
|
|
$result = array();
|
|
|
|
if (is_object($class)) {
|
|
$class = get_class($class);
|
|
}
|
|
|
|
$classes = array_merge(
|
|
self::ancestry($class),
|
|
self::subclassesFor($class)
|
|
);
|
|
|
|
foreach ($classes as $class) {
|
|
if (DataObject::has_own_table($class)) $result[$class] = $class;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Returns the root class (the first to extend from DataObject) for the
|
|
* passed class.
|
|
*
|
|
* @param string|object $class
|
|
* @return string
|
|
*/
|
|
public static function baseDataClass($class) {
|
|
if (is_object($class)) $class = get_class($class);
|
|
|
|
if (!is_subclass_of($class, 'DataObject')) {
|
|
throw new InvalidArgumentException("$class is not a subclass of DataObject");
|
|
}
|
|
|
|
while ($next = get_parent_class($class)) {
|
|
if ($next == 'DataObject') {
|
|
return $class;
|
|
}
|
|
|
|
$class = $next;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a list of classes that inherit from the given class.
|
|
* The resulting array includes the base class passed
|
|
* through the $class parameter as the first array value.
|
|
*
|
|
* Example usage:
|
|
* <code>
|
|
* ClassInfo::subclassesFor('BaseClass');
|
|
* array(
|
|
* 0 => 'BaseClass',
|
|
* 'ChildClass' => 'ChildClass',
|
|
* 'GrandChildClass' => 'GrandChildClass'
|
|
* )
|
|
* </code>
|
|
*
|
|
* @param mixed $class string of the classname or instance of the class
|
|
* @return array Names of all subclasses as an associative array.
|
|
*/
|
|
public static function subclassesFor($class) {
|
|
$descendants = SS_ClassLoader::instance()->getManifest()->getDescendantsOf($class);
|
|
$result = array($class => $class);
|
|
|
|
if ($descendants) {
|
|
return $result + ArrayLib::valuekey($descendants);
|
|
} else {
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the passed class name along with all its parent class names in an
|
|
* array, sorted with the root class first.
|
|
*
|
|
* @param string $class
|
|
* @param bool $tablesOnly Only return classes that have a table in the db.
|
|
* @return array
|
|
*/
|
|
public static function ancestry($class, $tablesOnly = false) {
|
|
if (!is_string($class)) $class = get_class($class);
|
|
|
|
$cacheKey = $class . '_' . (string)$tablesOnly;
|
|
$parent = $class;
|
|
if(!isset(self::$_cache_ancestry[$cacheKey])) {
|
|
$ancestry = array();
|
|
do {
|
|
if (!$tablesOnly || DataObject::has_own_table($parent)) {
|
|
$ancestry[$parent] = $parent;
|
|
}
|
|
} while ($parent = get_parent_class($parent));
|
|
self::$_cache_ancestry[$cacheKey] = array_reverse($ancestry);
|
|
}
|
|
|
|
return self::$_cache_ancestry[$cacheKey];
|
|
}
|
|
|
|
/**
|
|
* @return array A self-keyed array of class names. Note that this is only available with Silverstripe
|
|
* classes and not built-in PHP classes.
|
|
*/
|
|
public static function implementorsOf($interfaceName) {
|
|
return SS_ClassLoader::instance()->getManifest()->getImplementorsOf($interfaceName);
|
|
}
|
|
|
|
/**
|
|
* Returns true if the given class implements the given interface
|
|
*/
|
|
public static function classImplements($className, $interfaceName) {
|
|
return in_array($className, SS_ClassLoader::instance()->getManifest()->getImplementorsOf($interfaceName));
|
|
}
|
|
|
|
/**
|
|
* Get all classes contained in a file.
|
|
* @uses ManifestBuilder
|
|
*
|
|
* @todo Doesn't return additional classes that only begin
|
|
* with the filename, and have additional naming separated through underscores.
|
|
*
|
|
* @param string $filePath Path to a PHP file (absolute or relative to webroot)
|
|
* @return array
|
|
*/
|
|
public static function classes_for_file($filePath) {
|
|
$absFilePath = Director::getAbsFile($filePath);
|
|
$matchedClasses = array();
|
|
$manifest = SS_ClassLoader::instance()->getManifest()->getClasses();
|
|
|
|
foreach($manifest as $class => $compareFilePath) {
|
|
if($absFilePath == $compareFilePath) $matchedClasses[] = $class;
|
|
}
|
|
|
|
return $matchedClasses;
|
|
}
|
|
|
|
/**
|
|
* Returns all classes contained in a certain folder.
|
|
*
|
|
* @todo Doesn't return additional classes that only begin
|
|
* with the filename, and have additional naming separated through underscores.
|
|
*
|
|
* @param string $folderPath Relative or absolute folder path
|
|
* @return array Array of class names
|
|
*/
|
|
public static function classes_for_folder($folderPath) {
|
|
$absFolderPath = Director::getAbsFile($folderPath);
|
|
$matchedClasses = array();
|
|
$manifest = SS_ClassLoader::instance()->getManifest()->getClasses();
|
|
|
|
foreach($manifest as $class => $compareFilePath) {
|
|
if(stripos($compareFilePath, $absFolderPath) === 0) $matchedClasses[] = $class;
|
|
}
|
|
|
|
return $matchedClasses;
|
|
}
|
|
|
|
private static $method_from_cache = array();
|
|
|
|
public static function has_method_from($class, $method, $compclass) {
|
|
if (!isset(self::$method_from_cache[$class])) self::$method_from_cache[$class] = array();
|
|
|
|
if (!array_key_exists($method, self::$method_from_cache[$class])) {
|
|
self::$method_from_cache[$class][$method] = false;
|
|
|
|
$classRef = new ReflectionClass($class);
|
|
|
|
if ($classRef->hasMethod($method)) {
|
|
$methodRef = $classRef->getMethod($method);
|
|
self::$method_from_cache[$class][$method] = $methodRef->getDeclaringClass()->getName();
|
|
}
|
|
}
|
|
|
|
return self::$method_from_cache[$class][$method] == $compclass;
|
|
}
|
|
|
|
/**
|
|
* Returns the table name in the class hierarchy which contains a given
|
|
* field column for a {@link DataObject}. If the field does not exist, this
|
|
* will return null.
|
|
*
|
|
* @param string $candidateClass
|
|
* @param string $fieldName
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function table_for_object_field($candidateClass, $fieldName) {
|
|
if(!$candidateClass || !$fieldName) {
|
|
return null;
|
|
}
|
|
|
|
$exists = class_exists($candidateClass);
|
|
|
|
while($candidateClass && $candidateClass != 'DataObject' && $exists) {
|
|
if(DataObject::has_own_table($candidateClass)) {
|
|
$inst = singleton($candidateClass);
|
|
|
|
if($inst->hasOwnTableDatabaseField($fieldName)) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
$candidateClass = get_parent_class($candidateClass);
|
|
$exists = class_exists($candidateClass);
|
|
}
|
|
|
|
if(!$candidateClass || !$exists) {
|
|
return null;
|
|
}
|
|
|
|
return $candidateClass;
|
|
}
|
|
}
|
|
|