|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Pechynho\Utility; |
| 5 | + |
| 6 | + |
| 7 | +use Doctrine\Common\Annotations\AnnotationException; |
| 8 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 9 | +use Doctrine\Common\Annotations\AnnotationRegistry; |
| 10 | +use RuntimeException; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Jan Pech <[email protected]> |
| 14 | + */ |
| 15 | +class Annotations |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @param string $class |
| 19 | + * @param string $property |
| 20 | + * @param string $annotation |
| 21 | + * @return object|null |
| 22 | + */ |
| 23 | + public static function getPropertyAnnotation(string $class, string $property, string $annotation) |
| 24 | + { |
| 25 | + ParamsChecker::notWhiteSpaceOrNullString('$class', $class, __METHOD__); |
| 26 | + ParamsChecker::notWhiteSpaceOrNullString('$property', $property, __METHOD__); |
| 27 | + ParamsChecker::notWhiteSpaceOrNullString('$annotation', $annotation, __METHOD__); |
| 28 | + $property = Reflections::getProperty($class, $property); |
| 29 | + $annotation = self::createAnnotationReader()->getPropertyAnnotation($property, $annotation); |
| 30 | + return $annotation; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param string $class |
| 35 | + * @param string $method |
| 36 | + * @param string $annotation |
| 37 | + * @return object|null |
| 38 | + */ |
| 39 | + public static function getMethodAnnotation(string $class, string $method, string $annotation) |
| 40 | + { |
| 41 | + ParamsChecker::notWhiteSpaceOrNullString('$class', $class, __METHOD__); |
| 42 | + ParamsChecker::notWhiteSpaceOrNullString('$method', $method, __METHOD__); |
| 43 | + ParamsChecker::notWhiteSpaceOrNullString('$annotation', $annotation, __METHOD__); |
| 44 | + $method = Reflections::getMethod($class, $method); |
| 45 | + $annotation = self::createAnnotationReader()->getMethodAnnotation($method, $annotation); |
| 46 | + return $annotation; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param string $class |
| 51 | + * @param string $annotation |
| 52 | + * @return object|null |
| 53 | + */ |
| 54 | + public static function getClassAnnotation(string $class, string $annotation) |
| 55 | + { |
| 56 | + ParamsChecker::notWhiteSpaceOrNullString('$class', $class, __METHOD__); |
| 57 | + ParamsChecker::notWhiteSpaceOrNullString('$annotation', $annotation, __METHOD__); |
| 58 | + $reflectionClass = Reflections::createReflectionClass($class); |
| 59 | + $annotationReader = self::createAnnotationReader(); |
| 60 | + while ($reflectionClass !== false) |
| 61 | + { |
| 62 | + $annotationInstance = $annotationReader->getClassAnnotation($reflectionClass, $annotation); |
| 63 | + if ($annotationInstance !== null) |
| 64 | + { |
| 65 | + return $annotationInstance; |
| 66 | + } |
| 67 | + $reflectionClass = $reflectionClass->getParentClass(); |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return AnnotationReader |
| 74 | + */ |
| 75 | + public static function createAnnotationReader() |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + if (method_exists(AnnotationRegistry::class, "registerLoader")) |
| 80 | + { |
| 81 | + AnnotationRegistry::registerLoader("class_exists"); |
| 82 | + } |
| 83 | + return new AnnotationReader(); |
| 84 | + } |
| 85 | + catch (AnnotationException $exception) |
| 86 | + { |
| 87 | + throw new RuntimeException(sprintf("Creating new instance of '%s' class was not successful.", AnnotationReader::class)); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments