2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2014-08-18 05:39:42 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Core;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Manifest\ClassLoader;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use ReflectionClass;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Provides introspection information about the class tree.
|
2014-08-18 05:39:42 +02:00
|
|
|
*
|
2015-02-27 01:10:32 +01:00
|
|
|
* It's a cached wrapper around the built-in class functions. SilverStripe uses
|
|
|
|
* class introspection heavily and without the caching it creates an unfortunate
|
2014-08-18 05:39:42 +02:00
|
|
|
* performance hit.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ClassInfo {
|
2014-08-18 05:39:42 +02:00
|
|
|
|
2012-04-17 04:46:07 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for classes getter.
|
2016-05-25 07:30:01 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function allClasses() {
|
2016-09-09 08:43:05 +02:00
|
|
|
return ClassLoader::instance()->getManifest()->getClasses();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
|
|
|
/**
|
2016-01-04 09:15:17 +01:00
|
|
|
* Returns true if a class or interface name exists.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return bool
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function exists($class) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return class_exists($class, false) || interface_exists($class, false) || ClassLoader::instance()->getItemPath($class);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-03-03 00:24:10 +01:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
/**
|
|
|
|
* Cache for {@link hasTable()}
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
private static $_cache_all_tables = array();
|
2012-01-02 14:01:16 +01:00
|
|
|
|
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @var array Cache for {@link ancestry()}.
|
2012-01-02 14:01:16 +01:00
|
|
|
*/
|
|
|
|
private static $_cache_ancestry = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @todo Move this to SS_Database or DB
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @return bool
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public static function hasTable($tableName) {
|
2013-06-21 00:32:08 +02:00
|
|
|
// 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();
|
2008-11-09 23:11:25 +01:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return !empty(self::$_cache_all_tables[strtolower($tableName)]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function reset_db_cache() {
|
2010-04-12 04:03:16 +02:00
|
|
|
self::$_cache_all_tables = null;
|
2012-01-02 14:01:16 +01:00
|
|
|
self::$_cache_ancestry = array();
|
2010-04-12 04:03:16 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the manifest of all classes which are present in the database.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 03:31:56 +02:00
|
|
|
* @param string $class Class name to check enum values for ClassName field
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param boolean $includeUnbacked Flag indicating whether or not to include
|
2013-06-21 00:32:08 +02:00
|
|
|
* types that don't exist as implemented classes. By default these are excluded.
|
|
|
|
* @return array List of subclasses
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-08-05 00:32:13 +02:00
|
|
|
public static function getValidSubClasses($class = 'SilverStripe\\CMS\\Model\\SiteTree', $includeUnbacked = false) {
|
2016-05-19 13:28:35 +02:00
|
|
|
if(is_string($class) && !class_exists($class)) return array();
|
2015-08-07 04:20:01 +02:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$class = self::class_name($class);
|
2016-08-05 00:32:13 +02:00
|
|
|
if ($includeUnbacked) {
|
|
|
|
$table = DataObject::getSchema()->tableName($class);
|
|
|
|
$classes = DB::get_schema()->enumValuesForField($table, 'ClassName');
|
|
|
|
} else {
|
|
|
|
$classes = static::subclassesFor($class);
|
|
|
|
}
|
2012-08-29 04:30:10 +02:00
|
|
|
return $classes;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-19 05:35:59 +01:00
|
|
|
* Returns an array of the current class and all its ancestors and children
|
2013-06-21 00:32:08 +02:00
|
|
|
* which require a DB table.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @todo Move this into {@see DataObjectSchema}
|
|
|
|
*
|
2016-08-23 04:32:26 +02:00
|
|
|
* @param string|object $nameOrObject Class or object instance
|
2008-04-14 06:49:08 +02:00
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-08-23 04:32:26 +02:00
|
|
|
public static function dataClassesFor($nameOrObject) {
|
|
|
|
if(is_string($nameOrObject) && !class_exists($nameOrObject)) {
|
|
|
|
return array();
|
|
|
|
}
|
2015-08-07 04:20:01 +02:00
|
|
|
|
2011-02-19 05:35:59 +01:00
|
|
|
$result = array();
|
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$class = self::class_name($nameOrObject);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-02-19 05:35:59 +01:00
|
|
|
$classes = array_merge(
|
|
|
|
self::ancestry($class),
|
2013-06-21 00:32:08 +02:00
|
|
|
self::subclassesFor($class)
|
|
|
|
);
|
2011-02-19 05:35:59 +01:00
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
2016-05-25 07:30:01 +02:00
|
|
|
if (DataObject::has_own_table($class)) {
|
|
|
|
$result[$class] = $class;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-02-19 05:35:59 +01:00
|
|
|
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-24 10:14:38 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @deprecated 4.0..5.0
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-03-24 10:14:38 +01:00
|
|
|
public static function baseDataClass($class) {
|
2016-05-25 07:09:29 +02:00
|
|
|
Deprecation::notice('5.0', 'Use DataObject::getSchema()->baseDataClass()');
|
|
|
|
return DataObject::getSchema()->baseDataClass($class);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-24 10:14:38 +01:00
|
|
|
|
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.
|
2009-02-02 00:49:53 +01:00
|
|
|
* The resulting array includes the base class passed
|
|
|
|
* through the $class parameter as the first array value.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-02 00:49:53 +01:00
|
|
|
* Example usage:
|
2009-03-22 23:59:14 +01:00
|
|
|
* <code>
|
2009-02-02 00:49:53 +01:00
|
|
|
* ClassInfo::subclassesFor('BaseClass');
|
|
|
|
* array(
|
2015-02-27 01:10:32 +01:00
|
|
|
* 'BaseClass' => 'BaseClass',
|
2009-02-02 00:49:53 +01:00
|
|
|
* 'ChildClass' => 'ChildClass',
|
|
|
|
* 'GrandChildClass' => 'GrandChildClass'
|
|
|
|
* )
|
2009-03-22 23:59:14 +01:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-23 04:32:26 +02:00
|
|
|
* @param string|object $nameOrObject The classname or object
|
2009-02-02 00:49:53 +01:00
|
|
|
* @return array Names of all subclasses as an associative array.
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2016-08-23 04:32:26 +02:00
|
|
|
public static function subclassesFor($nameOrObject) {
|
|
|
|
if(is_string($nameOrObject) && !class_exists($nameOrObject)) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return [];
|
|
|
|
}
|
2015-08-07 04:20:01 +02:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
//normalise class case
|
2016-08-23 04:32:26 +02:00
|
|
|
$className = self::class_name($nameOrObject);
|
2016-09-09 08:43:05 +02:00
|
|
|
$descendants = ClassLoader::instance()->getManifest()->getDescendantsOf($className);
|
2015-02-27 01:10:32 +01:00
|
|
|
$result = array($className => $className);
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2011-03-22 10:47:55 +01:00
|
|
|
if ($descendants) {
|
|
|
|
return $result + ArrayLib::valuekey($descendants);
|
|
|
|
} else {
|
|
|
|
return $result;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-22 10:47:55 +01:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
/**
|
|
|
|
* Convert a class name in any case and return it as it was defined in PHP
|
|
|
|
*
|
|
|
|
* eg: self::class_name('dataobJEct'); //returns 'DataObject'
|
|
|
|
*
|
|
|
|
* @param string|object $nameOrObject The classname or object you want to normalise
|
|
|
|
* @return string The normalised class name
|
|
|
|
*/
|
|
|
|
public static function class_name($nameOrObject) {
|
|
|
|
if (is_object($nameOrObject)) {
|
|
|
|
return get_class($nameOrObject);
|
|
|
|
}
|
|
|
|
$reflection = new ReflectionClass($nameOrObject);
|
|
|
|
return $reflection->getName();
|
|
|
|
}
|
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
2011-02-19 04:18:26 +01:00
|
|
|
* Returns the passed class name along with all its parent class names in an
|
|
|
|
* array, sorted with the root class first.
|
|
|
|
*
|
2016-08-23 04:32:26 +02:00
|
|
|
* @param string|object $nameOrObject Class or object instance
|
|
|
|
* @param bool $tablesOnly Only return classes that have a table in the db.
|
2011-02-19 04:18:26 +01:00
|
|
|
* @return array
|
2008-03-03 00:24:10 +01:00
|
|
|
*/
|
2016-08-23 04:32:26 +02:00
|
|
|
public static function ancestry($nameOrObject, $tablesOnly = false) {
|
|
|
|
if(is_string($nameOrObject) && !class_exists($nameOrObject)) {
|
|
|
|
return array();
|
|
|
|
}
|
2015-08-07 04:20:01 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$class = self::class_name($nameOrObject);
|
2012-01-02 14:01:16 +01:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$lClass = strtolower($class);
|
2012-01-02 14:01:16 +01:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$cacheKey = $lClass . '_' . (string)$tablesOnly;
|
2012-01-02 14:01:16 +01:00
|
|
|
$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));
|
2014-08-15 08:53:05 +02:00
|
|
|
self::$_cache_ancestry[$cacheKey] = array_reverse($ancestry);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-02-19 04:18:26 +01:00
|
|
|
|
2012-01-02 14:01:16 +01:00
|
|
|
return self::$_cache_ancestry[$cacheKey];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $interfaceName
|
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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function implementorsOf($interfaceName) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return ClassLoader::instance()->getManifest()->getImplementorsOf($interfaceName);
|
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
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param string $interfaceName
|
|
|
|
* @return bool
|
2008-08-09 04:16:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function classImplements($className, $interfaceName) {
|
2015-02-27 01:10:32 +01:00
|
|
|
return in_array($className, self::implementorsOf($interfaceName));
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
2009-08-05 06:01:22 +02:00
|
|
|
|
2008-10-17 17:18:26 +02:00
|
|
|
/**
|
|
|
|
* Get all classes contained in a file.
|
|
|
|
* @uses ManifestBuilder
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-07-01 00:08:59 +02:00
|
|
|
* @todo Doesn't return additional classes that only begin
|
|
|
|
* with the filename, and have additional naming separated through underscores.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-17 17:18:26 +02:00
|
|
|
* @param string $filePath Path to a PHP file (absolute or relative to webroot)
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function classes_for_file($filePath) {
|
2011-03-22 10:47:55 +01:00
|
|
|
$absFilePath = Director::getAbsFile($filePath);
|
2008-10-17 17:18:26 +02:00
|
|
|
$matchedClasses = array();
|
2016-09-09 08:43:05 +02:00
|
|
|
$manifest = ClassLoader::instance()->getManifest()->getClasses();
|
2011-03-22 10:47:55 +01:00
|
|
|
|
|
|
|
foreach($manifest as $class => $compareFilePath) {
|
2008-10-17 17:18:26 +02:00
|
|
|
if($absFilePath == $compareFilePath) $matchedClasses[] = $class;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-17 17:18:26 +02:00
|
|
|
return $matchedClasses;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-01 00:08:59 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-07-01 00:08:59 +02:00
|
|
|
* @param string $folderPath Relative or absolute folder path
|
|
|
|
* @return array Array of class names
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function classes_for_folder($folderPath) {
|
2011-03-22 10:47:55 +01:00
|
|
|
$absFolderPath = Director::getAbsFile($folderPath);
|
2009-07-01 00:08:59 +02:00
|
|
|
$matchedClasses = array();
|
2016-09-09 08:43:05 +02:00
|
|
|
$manifest = ClassLoader::instance()->getManifest()->getClasses();
|
2011-03-22 10:47:55 +01:00
|
|
|
|
|
|
|
foreach($manifest as $class => $compareFilePath) {
|
2009-07-01 00:08:59 +02:00
|
|
|
if(stripos($compareFilePath, $absFolderPath) === 0) $matchedClasses[] = $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matchedClasses;
|
|
|
|
}
|
2012-08-22 23:29:13 +02:00
|
|
|
|
|
|
|
private static $method_from_cache = array();
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function has_method_from($class, $method, $compclass) {
|
2015-02-27 01:10:32 +01:00
|
|
|
$lClass = strtolower($class);
|
|
|
|
$lMethod = strtolower($method);
|
|
|
|
$lCompclass = strtolower($compclass);
|
|
|
|
if (!isset(self::$method_from_cache[$lClass])) self::$method_from_cache[$lClass] = array();
|
2012-08-22 23:29:13 +02:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
if (!array_key_exists($lMethod, self::$method_from_cache[$lClass])) {
|
|
|
|
self::$method_from_cache[$lClass][$lMethod] = false;
|
2012-08-22 23:29:13 +02:00
|
|
|
|
|
|
|
$classRef = new ReflectionClass($class);
|
|
|
|
|
|
|
|
if ($classRef->hasMethod($method)) {
|
|
|
|
$methodRef = $classRef->getMethod($method);
|
2015-02-27 01:10:32 +01:00
|
|
|
self::$method_from_cache[$lClass][$lMethod] = $methodRef->getDeclaringClass()->getName();
|
2012-08-22 23:29:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
return strtolower(self::$method_from_cache[$lClass][$lMethod]) == $lCompclass;
|
2012-08-22 23:29:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-18 05:39:42 +02:00
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @deprecated 4.0..5.0
|
2014-08-18 05:39:42 +02:00
|
|
|
*/
|
|
|
|
public static function table_for_object_field($candidateClass, $fieldName) {
|
2016-05-25 07:09:29 +02:00
|
|
|
Deprecation::notice('5.0', 'Use DataObject::getSchema()->tableForField()');
|
|
|
|
return DataObject::getSchema()->tableForField($candidateClass, $fieldName);
|
2014-08-18 05:39:42 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip namespace from class
|
|
|
|
*
|
|
|
|
* @param string|object $nameOrObject Name of class, or instance
|
|
|
|
* @return string Name of class without namespace
|
|
|
|
*/
|
|
|
|
public static function shortName($nameOrObject) {
|
|
|
|
$reflection = new ReflectionClass($nameOrObject);
|
|
|
|
return $reflection->getShortName();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-02-12 21:22:11 +01:00
|
|
|
|