ClassUtils.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Doctrine\Common\Util;
  3. use Doctrine\Persistence\Proxy;
  4. use ReflectionClass;
  5. use function get_class;
  6. use function get_parent_class;
  7. use function ltrim;
  8. use function rtrim;
  9. use function strrpos;
  10. use function substr;
  11. /**
  12. * Class and reflection related functionality for objects that
  13. * might or not be proxy objects at the moment.
  14. */
  15. class ClassUtils
  16. {
  17. /**
  18. * Gets the real class name of a class name that could be a proxy.
  19. *
  20. * @param string $className
  21. *
  22. * @return string
  23. *
  24. * @psalm-param class-string $className
  25. * @psalm-return class-string
  26. */
  27. public static function getRealClass($className)
  28. {
  29. $pos = strrpos($className, '\\' . Proxy::MARKER . '\\');
  30. if ($pos === false) {
  31. return $className;
  32. }
  33. return substr($className, $pos + Proxy::MARKER_LENGTH + 2);
  34. }
  35. /**
  36. * Gets the real class name of an object (even if its a proxy).
  37. *
  38. * @param object $object
  39. *
  40. * @return string
  41. *
  42. * @psalm-return class-string
  43. */
  44. public static function getClass($object)
  45. {
  46. return self::getRealClass(get_class($object));
  47. }
  48. /**
  49. * Gets the real parent class name of a class or object.
  50. *
  51. * @param string $className
  52. *
  53. * @return string
  54. *
  55. * @psalm-param class-string $className
  56. * @psalm-return class-string
  57. */
  58. public static function getParentClass($className)
  59. {
  60. return get_parent_class(self::getRealClass($className));
  61. }
  62. /**
  63. * Creates a new reflection class.
  64. *
  65. * @param string $className
  66. *
  67. * @return ReflectionClass
  68. *
  69. * @psalm-param class-string $className
  70. */
  71. public static function newReflectionClass($className)
  72. {
  73. return new ReflectionClass(self::getRealClass($className));
  74. }
  75. /**
  76. * Creates a new reflection object.
  77. *
  78. * @param object $object
  79. *
  80. * @return ReflectionClass
  81. */
  82. public static function newReflectionObject($object)
  83. {
  84. return self::newReflectionClass(self::getClass($object));
  85. }
  86. /**
  87. * Given a class name and a proxy namespace returns the proxy name.
  88. *
  89. * @param string $className
  90. * @param string $proxyNamespace
  91. *
  92. * @return string
  93. *
  94. * @psalm-param class-string $className
  95. * @psalm-return class-string
  96. */
  97. public static function generateProxyClassName($className, $proxyNamespace)
  98. {
  99. return rtrim($proxyNamespace, '\\') . '\\' . Proxy::MARKER . '\\' . ltrim($className, '\\');
  100. }
  101. }