diff options
Diffstat (limited to 'vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php')
| -rw-r--r-- | vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php b/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php new file mode 100644 index 0000000..399e057 --- /dev/null +++ b/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\Persistence\Mapping; | ||
| 6 | |||
| 7 | use Doctrine\Persistence\Reflection\RuntimeReflectionProperty; | ||
| 8 | use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty; | ||
| 9 | use ReflectionClass; | ||
| 10 | use ReflectionException; | ||
| 11 | use ReflectionMethod; | ||
| 12 | |||
| 13 | use function array_key_exists; | ||
| 14 | use function assert; | ||
| 15 | use function class_exists; | ||
| 16 | use function class_parents; | ||
| 17 | use function phpversion; | ||
| 18 | use function version_compare; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * PHP Runtime Reflection Service. | ||
| 22 | */ | ||
| 23 | class RuntimeReflectionService implements ReflectionService | ||
| 24 | { | ||
| 25 | /** @var bool */ | ||
| 26 | private $supportsTypedPropertiesWorkaround; | ||
| 27 | |||
| 28 | public function __construct() | ||
| 29 | { | ||
| 30 | $this->supportsTypedPropertiesWorkaround = version_compare(phpversion(), '7.4.0') >= 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | /** | ||
| 34 | * {@inheritDoc} | ||
| 35 | */ | ||
| 36 | public function getParentClasses(string $class) | ||
| 37 | { | ||
| 38 | if (! class_exists($class)) { | ||
| 39 | throw MappingException::nonExistingClass($class); | ||
| 40 | } | ||
| 41 | |||
| 42 | $parents = class_parents($class); | ||
| 43 | |||
| 44 | assert($parents !== false); | ||
| 45 | |||
| 46 | return $parents; | ||
| 47 | } | ||
| 48 | |||
| 49 | /** | ||
| 50 | * {@inheritDoc} | ||
| 51 | */ | ||
| 52 | public function getClassShortName(string $class) | ||
| 53 | { | ||
| 54 | $reflectionClass = new ReflectionClass($class); | ||
| 55 | |||
| 56 | return $reflectionClass->getShortName(); | ||
| 57 | } | ||
| 58 | |||
| 59 | /** | ||
| 60 | * {@inheritDoc} | ||
| 61 | */ | ||
| 62 | public function getClassNamespace(string $class) | ||
| 63 | { | ||
| 64 | $reflectionClass = new ReflectionClass($class); | ||
| 65 | |||
| 66 | return $reflectionClass->getNamespaceName(); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * @psalm-param class-string<T> $class | ||
| 71 | * | ||
| 72 | * @return ReflectionClass | ||
| 73 | * @psalm-return ReflectionClass<T> | ||
| 74 | * | ||
| 75 | * @template T of object | ||
| 76 | */ | ||
| 77 | public function getClass(string $class) | ||
| 78 | { | ||
| 79 | return new ReflectionClass($class); | ||
| 80 | } | ||
| 81 | |||
| 82 | /** | ||
| 83 | * {@inheritDoc} | ||
| 84 | */ | ||
| 85 | public function getAccessibleProperty(string $class, string $property) | ||
| 86 | { | ||
| 87 | $reflectionProperty = new RuntimeReflectionProperty($class, $property); | ||
| 88 | |||
| 89 | if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) { | ||
| 90 | $reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property); | ||
| 91 | } | ||
| 92 | |||
| 93 | $reflectionProperty->setAccessible(true); | ||
| 94 | |||
| 95 | return $reflectionProperty; | ||
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * {@inheritDoc} | ||
| 100 | */ | ||
| 101 | public function hasPublicMethod(string $class, string $method) | ||
| 102 | { | ||
| 103 | try { | ||
| 104 | $reflectionMethod = new ReflectionMethod($class, $method); | ||
| 105 | } catch (ReflectionException $e) { | ||
| 106 | return false; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $reflectionMethod->isPublic(); | ||
| 110 | } | ||
| 111 | } | ||
