1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Tool;
trait Reflection {
/**
* Cache with keys by full interface class names and with values with
* only static and also only public method names by the interface name.
* @var array
*/
protected static $cacheInterfStaticMths = [];
/**
* Key/value store for parsed reflection attributes constructor arguments.
* @var array
*/
protected static $cacheAttrsArgs = [];
/**
* Prefered PHP classes and properties anontation.
* `FALSE` by default, older PHP Docs tags anotations are default
* because of maximum compatibility.
* @var bool
*/
protected static $attributesAnotation = FALSE;
/**
* @inheritDocs
* @param bool $attributesAnotation
* @return bool
*/
public static function SetAttributesAnotations ($attributesAnotation = TRUE) {
return self::$attributesAnotation = $attributesAnotation;
}
/**
* @inheritDocs
* @return bool
*/
public static function GetAttributesAnotations () {
return self::$attributesAnotation;
}
/**
* @inheritDocs
* @param string $testClassName Full test class name.
* @param string $interfaceName Full interface class name.
* @param bool $checkStaticMethods Check implementation of all static methods by interface static methods.
* @param bool $throwException If `TRUE`, throw an exception if something is not implemented or if `FALSE` return `FALSE` only.
* @throws \InvalidArgumentException
* @return bool
*/
public static function CheckClassInterface ($testClassName, $interfaceName, $checkStaticMethods = FALSE, $throwException = TRUE) {
$result = FALSE;
$errorMsg = '';
// check given test class for all implemented instance methods by given interface
$interfaceName = trim($interfaceName, '\\');
$testClassType = new \ReflectionClass($testClassName);
if (in_array($interfaceName, $testClassType->getInterfaceNames(), TRUE)) {
$result = TRUE;
} else {
$errorMsg = "Class `{$testClassName}` doesn't implement interface `{$interfaceName}`.";
}
if ($result && $checkStaticMethods) {
// check given test class for all implemented static methods by given interface
$allStaticsImplemented = TRUE;
$interfaceMethods = static::checkClassInterfaceGetPublicStaticMethods($interfaceName);
foreach ($interfaceMethods as $methodName) {
if (!$testClassType->hasMethod($methodName)) {
$allStaticsImplemented = FALSE;
$errorMsg = "Class `{$testClassName}` doesn't implement static method `{$methodName}` from interface `{$interfaceName}`.";
break;
}
// arguments compatibility in presented static method are automatically checked by PHP
}
if (!$allStaticsImplemented)
$result = FALSE;
}
// return result or thrown an exception
if ($result) return TRUE;
if (!$throwException) return FALSE;
throw new \InvalidArgumentException("[".get_class()."] " . $errorMsg);
}
/**
* @inheritDocs
* @param string $testClassName Full test class name.
* @param string $traitName Full trait class name.
* @param bool $checkParentClasses If `TRUE`, trait implementation will be checked on all parent classes until success. Default is `FALSE`.
* @param bool $throwException If `TRUE`, throw an exception if trait is not implemented or if `FALSE` return `FALSE` only.
* @throws \InvalidArgumentException
* @return boolean
*/
public static function CheckClassTrait ($testClassName, $traitName, $checkParentClasses = FALSE, $throwException = TRUE) {
$result = FALSE;
$errorMsg = '';
// check given test class for all implemented instance methods by given interface
$testClassType = new \ReflectionClass($testClassName);
if (in_array($traitName, $testClassType->getTraitNames(), TRUE)) {
$result = TRUE;
} else if ($checkParentClasses) {
$currentClassType = $testClassType;
while (TRUE) {
$parentClass = $currentClassType->getParentClass();
if ($parentClass === FALSE) break;
$parentClassType = new \ReflectionClass($parentClass->getName());
if (in_array($traitName, $parentClassType->getTraitNames(), TRUE)) {
$result = TRUE;
break;
} else {
$currentClassType = $parentClassType;
}
}
}
if (!$result)
$errorMsg = "Class `{$testClassName}` doesn't implement trait `{$traitName}`.";
// return result or thrown an exception
if ($result) return TRUE;
if (!$throwException) return FALSE;
throw new \InvalidArgumentException("[".get_class()."] " . $errorMsg);
}
/**
* @inheritDocs
* @param string|object $classFullNameOrInstance Class instance or full class name.
* @param \string[] $attrsClassesOrDocsTags Array with attribute(s) full class names
* or array with PhpDocs tag(s) name(s).
* @param bool|NULL $preferAttributes Prefered way to get meta data. `TRUE` means try
* to get PHP8+ attribute(s) only, `FALSE` means
* try to get PhpDocs tag(s) only and `NULL` (default)
* means try to get PHP8+ attribute(s) first and if
* there is nothing, try to get PhpDocs tag(s).
* @return array Keys are attributes full class names (or PhpDocs tags names) and values
* are attributes constructor arguments (or PhpDocs tags arguments).
*/
public static function GetClassAttrsArgs ($classFullNameOrInstance, $attrsClassesOrDocsTags, $preferAttributes = NULL) {
$result = [];
$attrsOnly = $preferAttributes === TRUE;
$docsTagsOnly = $preferAttributes === FALSE;
$reflectionObject = new \ReflectionClass($classFullNameOrInstance);
foreach ($attrsClassesOrDocsTags as $attrClassOrDocsTag)
$result[$attrClassOrDocsTag] = static::getAttrArgsOrPhpDocTagArgs(
implode('|', ['cls', $classFullNameOrInstance, $attrClassOrDocsTag]),
$reflectionObject, $attrClassOrDocsTag, $attrsOnly, $docsTagsOnly
);
return $result;
}
/**
* @inheritDocs
* @param string|object $classFullNameOrInstance Class instance or full class name.
* @param string $methodName Class method name.
* @param \string[] $attrsClassesOrDocsTags Array with attribute(s) full class names
* or array with PhpDocs tag(s) name(s).
* @param bool|NULL $preferAttributes Prefered way to get meta data. `TRUE` means try
* to get PHP8+ attribute(s) only, `FALSE` means
* try to get PhpDocs tag(s) only and `NULL` (default)
* means try to get PHP8+ attribute(s) first and if
* there is nothing, try to get PhpDocs tag(s).
* @return array Keys are attributes full class names (or PhpDocs tags names) and values
* are attributes constructor arguments (or PhpDocs tags arguments).
*/
public static function GetMethodAttrsArgs ($classFullNameOrInstance, $methodName, $attrsClassesOrDocsTags, $preferAttributes = NULL) {
$result = [];
$attrsOnly = $preferAttributes === TRUE;
$docsTagsOnly = $preferAttributes === FALSE;
$reflectionObject = new \ReflectionMethod($classFullNameOrInstance, $methodName);
$classMethodFullName = $classFullNameOrInstance . '::' . $methodName;
foreach ($attrsClassesOrDocsTags as $attrClassOrDocsTag)
$result[$attrClassOrDocsTag] = static::getAttrArgsOrPhpDocTagArgs(
implode('|', ['mthd', $classMethodFullName, $attrClassOrDocsTag]),
$reflectionObject, $attrClassOrDocsTag, $attrsOnly, $docsTagsOnly
);
return $result;
}
/**
* @inheritDocs
* @param string|object $classFullNameOrInstance Class instance or full class name.
* @param string $propertyName Class property name.
* @param \string[] $attrsClassesOrDocsTags Array with attribute(s) full class names
* or array with PhpDocs tag(s) name(s).
* @param bool|NULL $preferAttributes Prefered way to get meta data. `TRUE` means try
* to get PHP8+ attribute(s) only, `FALSE` means
* try to get PhpDocs tag(s) only and `NULL` (default)
* means try to get PHP8+ attribute(s) first and if
* there is nothing, try to get PhpDocs tag(s).
* @return array Keys are attributes full class names (or PhpDocs tags names) and values
* are attributes constructor arguments (or PhpDocs tags arguments).
*/
public static function GetPropertyAttrsArgs ($classFullNameOrInstance, $propertyName, $attrsClassesOrDocsTags, $preferAttributes = NULL) {
$result = [];
$attrsOnly = $preferAttributes === TRUE;
$docsTagsOnly = $preferAttributes === FALSE;
$reflectionObject = new \ReflectionProperty($classFullNameOrInstance, $propertyName);
$classPropFullName = $classFullNameOrInstance . '::' . $propertyName;
foreach ($attrsClassesOrDocsTags as $attrClassOrDocsTag)
$result[$attrClassOrDocsTag] = static::getAttrArgsOrPhpDocTagArgs(
implode('|', ['prop', $classPropFullName, $attrClassOrDocsTag]),
$reflectionObject, $attrClassOrDocsTag, $attrsOnly, $docsTagsOnly
);
return $result;
}
/**
* Get (cached) reflection object attribute(s) constructor arguments or
* get reflection object PhpDocs tags and it's arguments for older PHP versions.
* Set result into local memory cache. You can optionally set prefered way
* to get desired meta data by last two arguments.
* @param string $cacheKey Result cache key.
* @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflectionObject Reflection object to get attributes/tags from.
* @param string $attrClassOrDocsTag Attributes class full names (or PhpDocs tags).
* @param bool $attrsOnly `TRUE` to get PHP8+ attributes only, do not fall back to PhpDocs tags.
* @param bool $docsTagsOnly `TRUE` to get PhpDocs tags only, do not try PHP8+ attributes.
* @return array Keys are attributes full class names (or PhpDocs tags names) and values
* are attributes constructor arguments (or PhpDocs tags arguments).
*/
protected static function getAttrArgsOrPhpDocTagArgs ($cacheKey, $reflectionObject, $attrClassOrDocsTag, $attrsOnly, $docsTagsOnly) {
if (array_key_exists($cacheKey, self::$cacheAttrsArgs)) {
$result = self::$cacheAttrsArgs[$cacheKey];
} else {
if ($attrsOnly) {
$result = static::GetAttrCtorArgs(
$reflectionObject, $attrClassOrDocsTag
);
} else if ($docsTagsOnly) {
$result = static::GetPhpDocsTagArgs(
$reflectionObject, $attrClassOrDocsTag
);
} else {
$result = static::GetAttrCtorArgs(
$reflectionObject, $attrClassOrDocsTag
);
if ($result === NULL)
$result = static::GetPhpDocsTagArgs(
$reflectionObject, $attrClassOrDocsTag
);
}
self::$cacheAttrsArgs[$cacheKey] = $result;
}
return $result;
}
/**
* @inheritDocs
* @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflectionObject
* @param string $attributeClassFullName
* @return array|NULL
*/
public static function GetAttrCtorArgs ($reflectionObject, $attributeClassFullName) {
$result = NULL;
$traversing = $reflectionObject instanceof \ReflectionClass;
while (TRUE) {
$attrs = $reflectionObject->getAttributes($attributeClassFullName);
if (count($attrs) > 0) {
$result = $attrs[0]->getArguments();
break;
}
if ($traversing) {
$reflectionObject = $reflectionObject->getParentClass();
if ($reflectionObject === FALSE) break;
} else {
break;
}
}
return $result;
}
/**
* @inheritDocs
* @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflectionObject
* @param string $phpDocsTagName
* @return array|NULL
*/
public static function GetPhpDocsTagArgs ($reflectionObject, $phpDocsTagName) {
$result = NULL;
$traversing = $reflectionObject instanceof \ReflectionClass;
while (TRUE) {
$docComment = $reflectionObject->getDocComment();
$tagPos = mb_strpos($docComment, $phpDocsTagName);
if ($tagPos !== FALSE) {
$result = [];
preg_match("#{$phpDocsTagName}\s+([^\r\n\*@]+)#", $docComment, $matches, 0, $tagPos);
if ($matches && count($matches) > 1) {
$rawResult = explode(',', $matches[1]);
foreach ($rawResult as $rawItem) {
$rawItem = trim($rawItem);
if ($rawItem !== '')
$result[] = $rawItem;
}
}
break;
}
if ($traversing) {
$reflectionObject = $reflectionObject->getParentClass();
if ($reflectionObject === FALSE) break;
} else {
break;
}
}
return $result;
}
/**
* Complete array with only static and also only public method names by given interface name.
* Return completed array and cache it in static local array.
* @param string $interfaceName
* @return array
*/
protected static function & checkClassInterfaceGetPublicStaticMethods ($interfaceName) {
if (!isset(static::$cacheInterfStaticMths[$interfaceName]))
static::$cacheInterfStaticMths[$interfaceName] = array_map(
function (\ReflectionMethod $method) {
return $method->name;
},
(new \ReflectionClass($interfaceName))->getMethods(
\ReflectionMethod::IS_STATIC
)
);
return static::$cacheInterfStaticMths[$interfaceName];
}
}