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;
|
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
use Exception;
|
2017-09-19 06:55:39 +02:00
|
|
|
use ReflectionClass;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-19 00:51:35 +02:00
|
|
|
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;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-09-19 06:55:39 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
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
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class ClassInfo
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cache for {@link hasTable()}
|
2017-05-17 07:40:13 +02:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $_cache_all_tables = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-05-17 07:40:13 +02:00
|
|
|
* @internal
|
2016-11-29 00:31:16 +01:00
|
|
|
* @var array Cache for {@link ancestry()}.
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $_cache_ancestry = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
/**
|
|
|
|
* Cache for parse_class_spec
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $_cache_parse = [];
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
/**
|
|
|
|
* Cache for has_method_from
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $_cache_methods = [];
|
2017-09-19 06:55:39 +02:00
|
|
|
|
2017-07-05 16:15:08 +02:00
|
|
|
/**
|
|
|
|
* Cache for class_name
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $_cache_class_names = [];
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for classes getter.
|
|
|
|
*
|
|
|
|
* @return array List of all class names
|
|
|
|
*/
|
|
|
|
public static function allClasses()
|
|
|
|
{
|
|
|
|
return ClassLoader::inst()->getManifest()->getClassNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a class or interface name exists.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function exists($class)
|
|
|
|
{
|
|
|
|
return class_exists($class, false)
|
|
|
|
|| interface_exists($class, false)
|
|
|
|
|| ClassLoader::inst()->getItemPath($class);
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @todo Move this to SS_Database or DB
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function hasTable($tableName)
|
|
|
|
{
|
|
|
|
// 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($tableName)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function reset_db_cache()
|
|
|
|
{
|
|
|
|
self::$_cache_all_tables = null;
|
2020-04-20 19:58:09 +02:00
|
|
|
self::$_cache_ancestry = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-09-19 06:55:39 +02:00
|
|
|
public static function getValidSubClasses($class = SiteTree::class, $includeUnbacked = false)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if (is_string($class) && !class_exists($class)) {
|
2020-04-20 19:58:09 +02:00
|
|
|
return [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class = self::class_name($class);
|
|
|
|
if ($includeUnbacked) {
|
|
|
|
$table = DataObject::getSchema()->tableName($class);
|
|
|
|
$classes = DB::get_schema()->enumValuesForField($table, 'ClassName');
|
|
|
|
} else {
|
|
|
|
$classes = static::subclassesFor($class);
|
|
|
|
}
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of the current class and all its ancestors and children
|
|
|
|
* which require a DB table.
|
|
|
|
*
|
|
|
|
* @todo Move this into {@see DataObjectSchema}
|
|
|
|
*
|
|
|
|
* @param string|object $nameOrObject Class or object instance
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function dataClassesFor($nameOrObject)
|
|
|
|
{
|
|
|
|
if (is_string($nameOrObject) && !class_exists($nameOrObject)) {
|
2017-09-19 06:55:39 +02:00
|
|
|
return [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
// Get all classes
|
2016-11-29 00:31:16 +01:00
|
|
|
$class = self::class_name($nameOrObject);
|
|
|
|
$classes = array_merge(
|
|
|
|
self::ancestry($class),
|
|
|
|
self::subclassesFor($class)
|
|
|
|
);
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
// Filter by table
|
|
|
|
return array_filter($classes, function ($next) {
|
|
|
|
return DataObject::getSchema()->classHasTable($next);
|
|
|
|
});
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0
|
2017-09-19 06:55:39 +02:00
|
|
|
* @param string $class
|
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function baseDataClass($class)
|
|
|
|
{
|
|
|
|
Deprecation::notice('5.0', 'Use DataObject::getSchema()->baseDataClass()');
|
|
|
|
return DataObject::getSchema()->baseDataClass($class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-09-19 06:55:39 +02:00
|
|
|
* Note that keys are lowercase, while the values are correct case.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* <code>
|
|
|
|
* ClassInfo::subclassesFor('BaseClass');
|
|
|
|
* array(
|
2017-09-19 06:55:39 +02:00
|
|
|
* 'baseclass' => 'BaseClass',
|
|
|
|
* 'childclass' => 'ChildClass',
|
|
|
|
* 'grandchildclass' => 'GrandChildClass'
|
2016-11-29 00:31:16 +01:00
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string|object $nameOrObject The classname or object
|
2019-04-30 00:43:14 +02:00
|
|
|
* @param bool $includeBaseClass Whether to include the base class or not. Defaults to true.
|
2017-09-19 06:55:39 +02:00
|
|
|
* @return array List of class names with lowercase keys and correct-case values
|
2019-04-30 00:43:14 +02:00
|
|
|
* @throws \ReflectionException
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2019-04-30 00:43:14 +02:00
|
|
|
public static function subclassesFor($nameOrObject, $includeBaseClass = true)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if (is_string($nameOrObject) && !class_exists($nameOrObject)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
// Get class names
|
2016-11-29 00:31:16 +01:00
|
|
|
$className = self::class_name($nameOrObject);
|
2017-09-19 06:55:39 +02:00
|
|
|
$lowerClassName = strtolower($className);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
// Merge with descendants
|
|
|
|
$descendants = ClassLoader::inst()->getManifest()->getDescendantsOf($className);
|
|
|
|
return array_merge(
|
2019-04-30 00:43:14 +02:00
|
|
|
$includeBaseClass ? [$lowerClassName => $className] : [],
|
2017-09-19 06:55:39 +02:00
|
|
|
$descendants
|
|
|
|
);
|
2016-11-29 00:31:16 +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
|
2018-01-11 15:12:06 +01:00
|
|
|
* @throws \ReflectionException
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string The normalised class name
|
|
|
|
*/
|
|
|
|
public static function class_name($nameOrObject)
|
|
|
|
{
|
|
|
|
if (is_object($nameOrObject)) {
|
|
|
|
return get_class($nameOrObject);
|
|
|
|
}
|
2017-07-05 16:15:08 +02:00
|
|
|
|
|
|
|
$key = strtolower($nameOrObject);
|
|
|
|
if (!isset(static::$_cache_class_names[$key])) {
|
2017-09-19 06:55:39 +02:00
|
|
|
// Get manifest name
|
|
|
|
$name = ClassLoader::inst()->getManifest()->getItemName($nameOrObject);
|
|
|
|
|
|
|
|
// Use reflection for non-manifest classes
|
|
|
|
if (!$name) {
|
|
|
|
$reflection = new ReflectionClass($nameOrObject);
|
|
|
|
$name = $reflection->getName();
|
|
|
|
}
|
|
|
|
static::$_cache_class_names[$key] = $name;
|
2017-07-05 16:15:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return static::$_cache_class_names[$key];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the passed class name along with all its parent class names in an
|
|
|
|
* array, sorted with the root class first.
|
|
|
|
*
|
|
|
|
* @param string|object $nameOrObject Class or object instance
|
|
|
|
* @param bool $tablesOnly Only return classes that have a table in the db.
|
2017-09-19 06:55:39 +02:00
|
|
|
* @return array List of class names with lowercase keys and correct-case values
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function ancestry($nameOrObject, $tablesOnly = false)
|
|
|
|
{
|
|
|
|
if (is_string($nameOrObject) && !class_exists($nameOrObject)) {
|
2017-09-19 06:55:39 +02:00
|
|
|
return [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class = self::class_name($nameOrObject);
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
$lowerClass = strtolower($class);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
$cacheKey = $lowerClass . '_' . (string)$tablesOnly;
|
2016-11-29 00:31:16 +01:00
|
|
|
$parent = $class;
|
|
|
|
if (!isset(self::$_cache_ancestry[$cacheKey])) {
|
2017-09-19 06:55:39 +02:00
|
|
|
$ancestry = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
do {
|
|
|
|
if (!$tablesOnly || DataObject::getSchema()->classHasTable($parent)) {
|
2017-09-19 06:55:39 +02:00
|
|
|
$ancestry[strtolower($parent)] = $parent;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
} while ($parent = get_parent_class($parent));
|
|
|
|
self::$_cache_ancestry[$cacheKey] = array_reverse($ancestry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$_cache_ancestry[$cacheKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $interfaceName
|
2017-09-19 06:55:39 +02:00
|
|
|
* @return array A self-keyed array of class names with lowercase keys and correct-case values.
|
|
|
|
* Note that this is only available with Silverstripe classes and not built-in PHP classes.
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function implementorsOf($interfaceName)
|
|
|
|
{
|
2017-05-19 04:38:06 +02:00
|
|
|
return ClassLoader::inst()->getManifest()->getImplementorsOf($interfaceName);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given class implements the given interface
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param string $interfaceName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function classImplements($className, $interfaceName)
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$lowerClassName = strtolower($className);
|
|
|
|
$implementors = self::implementorsOf($interfaceName);
|
|
|
|
return isset($implementors[$lowerClassName]);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all classes contained in a file.
|
|
|
|
*
|
|
|
|
* @param string $filePath Path to a PHP file (absolute or relative to webroot)
|
2017-09-19 06:55:39 +02:00
|
|
|
* @return array Map of lowercase class names to correct class name
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function classes_for_file($filePath)
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$absFilePath = Director::getAbsFile($filePath);
|
|
|
|
$classManifest = ClassLoader::inst()->getManifest();
|
|
|
|
$classes = $classManifest->getClasses();
|
|
|
|
$classNames = $classManifest->getClassNames();
|
|
|
|
|
|
|
|
$matchedClasses = [];
|
|
|
|
foreach ($classes as $lowerClass => $compareFilePath) {
|
|
|
|
if (strcasecmp($absFilePath, $compareFilePath) === 0) {
|
|
|
|
$matchedClasses[$lowerClass] = $classNames[$lowerClass];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matchedClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all classes contained in a certain folder.
|
|
|
|
*
|
|
|
|
* @param string $folderPath Relative or absolute folder path
|
2017-09-19 06:55:39 +02:00
|
|
|
* @return array Map of lowercase class names to correct class name
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function classes_for_folder($folderPath)
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$absFolderPath = Director::getAbsFile($folderPath);
|
|
|
|
$classManifest = ClassLoader::inst()->getManifest();
|
|
|
|
$classes = $classManifest->getClasses();
|
|
|
|
$classNames = $classManifest->getClassNames();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
$matchedClasses = [];
|
|
|
|
foreach ($classes as $lowerClass => $compareFilePath) {
|
2016-11-29 00:31:16 +01:00
|
|
|
if (stripos($compareFilePath, $absFolderPath) === 0) {
|
2017-09-19 06:55:39 +02:00
|
|
|
$matchedClasses[$lowerClass] = $classNames[$lowerClass];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matchedClasses;
|
|
|
|
}
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
/**
|
|
|
|
* Determine if the given class method is implemented at the given comparison class
|
|
|
|
*
|
|
|
|
* @param string $class Class to get methods from
|
|
|
|
* @param string $method Method name to lookup
|
|
|
|
* @param string $compclass Parent class to test if this is the implementor
|
|
|
|
* @return bool True if $class::$method is declared in $compclass
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function has_method_from($class, $method, $compclass)
|
|
|
|
{
|
|
|
|
$lClass = strtolower($class);
|
|
|
|
$lMethod = strtolower($method);
|
|
|
|
$lCompclass = strtolower($compclass);
|
2017-09-19 06:55:39 +02:00
|
|
|
if (!isset(self::$_cache_methods[$lClass])) {
|
2020-04-20 19:58:09 +02:00
|
|
|
self::$_cache_methods[$lClass] = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
if (!array_key_exists($lMethod, self::$_cache_methods[$lClass])) {
|
|
|
|
self::$_cache_methods[$lClass][$lMethod] = false;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
$classRef = new ReflectionClass($class);
|
|
|
|
|
|
|
|
if ($classRef->hasMethod($method)) {
|
|
|
|
$methodRef = $classRef->getMethod($method);
|
2017-09-19 06:55:39 +02:00
|
|
|
self::$_cache_methods[$lClass][$lMethod] = $methodRef->getDeclaringClass()->getName();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 06:55:39 +02:00
|
|
|
return strtolower(self::$_cache_methods[$lClass][$lMethod]) === $lCompclass;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function table_for_object_field($candidateClass, $fieldName)
|
|
|
|
{
|
|
|
|
Deprecation::notice('5.0', 'Use DataObject::getSchema()->tableForField()');
|
|
|
|
return DataObject::getSchema()->tableForField($candidateClass, $fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2017-09-19 06:55:39 +02:00
|
|
|
$name = static::class_name($nameOrObject);
|
|
|
|
$parts = explode('\\', $name);
|
|
|
|
return end($parts);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-03-02 03:24:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to determine if the given object has a method
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* @param string $method
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function hasMethod($object, $method)
|
|
|
|
{
|
2020-10-23 05:33:56 +02:00
|
|
|
if (empty($object) || (!is_object($object) && !is_string($object))) {
|
2017-03-02 03:24:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (method_exists($object, $method)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return method_exists($object, 'hasMethod') && $object->hasMethod($method);
|
|
|
|
}
|
2017-05-17 07:40:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().
|
|
|
|
* Returns a 2-element array, with classname and arguments
|
|
|
|
*
|
|
|
|
* @param string $classSpec
|
|
|
|
* @return array
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function parse_class_spec($classSpec)
|
|
|
|
{
|
|
|
|
if (isset(static::$_cache_parse[$classSpec])) {
|
|
|
|
return static::$_cache_parse[$classSpec];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tokens = token_get_all("<?php $classSpec");
|
|
|
|
$class = null;
|
2020-04-20 19:58:09 +02:00
|
|
|
$args = [];
|
2017-05-17 07:40:13 +02:00
|
|
|
|
|
|
|
// Keep track of the current bucket that we're putting data into
|
|
|
|
$bucket = &$args;
|
2020-04-20 19:58:09 +02:00
|
|
|
$bucketStack = [];
|
2020-09-30 02:05:15 +02:00
|
|
|
$lastTokenWasNSSeparator = false;
|
2017-05-17 07:40:13 +02:00
|
|
|
$currentKey = null;
|
|
|
|
|
|
|
|
foreach ($tokens as $token) {
|
|
|
|
// $forceResult used to allow null result to be detected
|
|
|
|
$result = $forceResult = null;
|
|
|
|
$tokenName = is_array($token) ? $token[0] : $token;
|
|
|
|
|
|
|
|
// Get the class name
|
2020-08-29 02:08:24 +02:00
|
|
|
if (\defined('T_NAME_QUALIFIED') && is_array($token) &&
|
|
|
|
($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED)
|
|
|
|
) {
|
|
|
|
// PHP 8 exposes the FQCN as a single T_NAME_QUALIFIED or T_NAME_FULLY_QUALIFIED token
|
2020-09-30 02:05:15 +02:00
|
|
|
$class = $token[1];
|
2020-08-29 02:08:24 +02:00
|
|
|
} elseif ($class === null && is_array($token) && $token[0] === T_STRING) {
|
2017-05-17 07:40:13 +02:00
|
|
|
$class = $token[1];
|
|
|
|
} elseif (is_array($token) && $token[0] === T_NS_SEPARATOR) {
|
|
|
|
$class .= $token[1];
|
2020-09-30 02:05:15 +02:00
|
|
|
$lastTokenWasNSSeparator = true;
|
2017-10-05 06:23:02 +02:00
|
|
|
} elseif ($token === '.') {
|
|
|
|
// Treat service name separator as NS separator
|
|
|
|
$class .= '.';
|
2020-09-30 02:05:15 +02:00
|
|
|
$lastTokenWasNSSeparator = true;
|
|
|
|
} elseif ($lastTokenWasNSSeparator && is_array($token) && $token[0] === T_STRING) {
|
|
|
|
// Found another section of a namespaced class
|
2017-05-17 07:40:13 +02:00
|
|
|
$class .= $token[1];
|
2020-09-30 02:05:15 +02:00
|
|
|
$lastTokenWasNSSeparator = false;
|
2017-05-17 07:40:13 +02:00
|
|
|
// Get arguments
|
|
|
|
} elseif (is_array($token)) {
|
|
|
|
switch ($token[0]) {
|
|
|
|
case T_CONSTANT_ENCAPSED_STRING:
|
|
|
|
$argString = $token[1];
|
|
|
|
switch ($argString[0]) {
|
|
|
|
case '"':
|
|
|
|
$result = stripcslashes(substr($argString, 1, -1));
|
|
|
|
break;
|
|
|
|
case "'":
|
2017-10-05 06:23:02 +02:00
|
|
|
$result = str_replace(
|
|
|
|
["\\\\", "\\'"],
|
|
|
|
["\\", "'"],
|
|
|
|
substr($argString, 1, -1)
|
|
|
|
);
|
2017-05-17 07:40:13 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Bad T_CONSTANT_ENCAPSED_STRING arg $argString");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DNUMBER:
|
|
|
|
$result = (double)$token[1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LNUMBER:
|
|
|
|
$result = (int)$token[1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DOUBLE_ARROW:
|
|
|
|
// We've encountered an associative array (the array itself has already been
|
|
|
|
// added to the bucket), so the previous item added to the bucket is the key
|
|
|
|
end($bucket);
|
|
|
|
$currentKey = current($bucket);
|
|
|
|
array_pop($bucket);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
switch ($token[1]) {
|
|
|
|
case 'true':
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'false':
|
|
|
|
$result = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'null':
|
|
|
|
$result = null;
|
|
|
|
$forceResult = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Bad T_STRING arg '{$token[1]}'");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ARRAY:
|
2020-04-20 19:58:09 +02:00
|
|
|
$result = [];
|
2017-05-17 07:40:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($tokenName === '[') {
|
2020-04-20 19:58:09 +02:00
|
|
|
$result = [];
|
2017-10-05 06:23:02 +02:00
|
|
|
} elseif (($tokenName === ')' || $tokenName === ']') && !empty($bucketStack)) {
|
2017-05-17 07:40:13 +02:00
|
|
|
// Store the bucket we're currently working on
|
|
|
|
$oldBucket = $bucket;
|
|
|
|
// Fetch the key for the bucket at the top of the stack
|
|
|
|
end($bucketStack);
|
|
|
|
$key = key($bucketStack);
|
|
|
|
reset($bucketStack);
|
|
|
|
// Re-instate the bucket from the top of the stack
|
|
|
|
$bucket = &$bucketStack[$key];
|
|
|
|
// Add our saved, "nested" bucket to the bucket we just popped off the stack
|
|
|
|
$bucket[$key] = $oldBucket;
|
|
|
|
// Remove the bucket we just popped off the stack
|
|
|
|
array_pop($bucketStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've got something to add to the bucket, add it
|
|
|
|
if ($result !== null || $forceResult) {
|
|
|
|
if ($currentKey) {
|
|
|
|
$bucket[$currentKey] = $result;
|
|
|
|
$currentKey = null;
|
|
|
|
} else {
|
|
|
|
$bucket[] = $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've just pushed an array, that becomes our new bucket
|
2020-04-20 19:58:09 +02:00
|
|
|
if ($result === []) {
|
2017-05-17 07:40:13 +02:00
|
|
|
// Fetch the key that the array was pushed to
|
|
|
|
end($bucket);
|
|
|
|
$key = key($bucket);
|
|
|
|
reset($bucket);
|
|
|
|
// Store reference to "old" bucket in the stack
|
|
|
|
$bucketStack[$key] = &$bucket;
|
|
|
|
// Set the active bucket to be our newly-pushed, empty array
|
|
|
|
$bucket = &$bucket[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = [$class, $args];
|
|
|
|
static::$_cache_parse[$classSpec] = $result;
|
|
|
|
return $result;
|
|
|
|
}
|
2020-02-14 03:44:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of classes with a particular extension applied
|
|
|
|
*
|
|
|
|
* This reflects all extensions added (or removed) both via the configuration API as well as dynamically
|
|
|
|
* using Extensible::add_extension() and Extensible::remove_extension().
|
|
|
|
*
|
|
|
|
* @param string $extensionClass Extension class name
|
|
|
|
* @param string $baseClassOrObject Class or object to find subclasses of with the extension applied
|
|
|
|
* @param bool $includeBaseClass Include the base class itself if it has the extension applied?
|
|
|
|
* @return string[] Class names with the extension applied
|
|
|
|
* @throws \ReflectionException
|
|
|
|
*/
|
|
|
|
public static function classesWithExtension(
|
|
|
|
string $extensionClass,
|
|
|
|
string $baseClassOrObject = DataObject::class,
|
|
|
|
bool $includeBaseClass = false
|
|
|
|
): array {
|
|
|
|
// get class names
|
|
|
|
$baseClass = self::class_name($baseClassOrObject);
|
|
|
|
|
|
|
|
// get a list of all subclasses for a given class
|
|
|
|
$classes = ClassInfo::subclassesFor($baseClass, $includeBaseClass);
|
|
|
|
|
|
|
|
// include the base class if required
|
|
|
|
if ($includeBaseClass) {
|
|
|
|
$classes = array_merge([strtolower($baseClass) => $baseClass], $classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only keep classes with the Extension applied
|
|
|
|
$classes = array_filter($classes, function ($class) use ($extensionClass) {
|
|
|
|
return Extensible::has_extension($class, $extensionClass);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|