RuntimeReflectionService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Doctrine\Common\Persistence\Mapping;
  3. use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
  4. use ReflectionClass;
  5. use ReflectionException;
  6. use ReflectionMethod;
  7. use ReflectionProperty;
  8. use function class_exists;
  9. use function class_parents;
  10. /**
  11. * PHP Runtime Reflection Service.
  12. */
  13. class RuntimeReflectionService implements ReflectionService
  14. {
  15. /**
  16. * {@inheritDoc}
  17. */
  18. public function getParentClasses($class)
  19. {
  20. if (! class_exists($class)) {
  21. throw MappingException::nonExistingClass($class);
  22. }
  23. return class_parents($class);
  24. }
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function getClassShortName($class)
  29. {
  30. $reflectionClass = new ReflectionClass($class);
  31. return $reflectionClass->getShortName();
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function getClassNamespace($class)
  37. {
  38. $reflectionClass = new ReflectionClass($class);
  39. return $reflectionClass->getNamespaceName();
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getClass($class)
  45. {
  46. return new ReflectionClass($class);
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function getAccessibleProperty($class, $property)
  52. {
  53. $reflectionProperty = new ReflectionProperty($class, $property);
  54. if ($reflectionProperty->isPublic()) {
  55. $reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
  56. }
  57. $reflectionProperty->setAccessible(true);
  58. return $reflectionProperty;
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function hasPublicMethod($class, $method)
  64. {
  65. try {
  66. $reflectionMethod = new ReflectionMethod($class, $method);
  67. } catch (ReflectionException $e) {
  68. return false;
  69. }
  70. return $reflectionMethod->isPublic();
  71. }
  72. }