ClassUtils.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Util;
  20. use Doctrine\Common\Persistence\Proxy;
  21. /**
  22. * Class and reflection related functionality for objects that
  23. * might or not be proxy objects at the moment.
  24. *
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. * @author Johannes Schmitt <schmittjoh@gmail.com>
  27. */
  28. class ClassUtils
  29. {
  30. /**
  31. * Gets the real class name of a class name that could be a proxy.
  32. *
  33. * @param string $class
  34. *
  35. * @return string
  36. */
  37. public static function getRealClass($class)
  38. {
  39. if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) {
  40. return $class;
  41. }
  42. return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
  43. }
  44. /**
  45. * Gets the real class name of an object (even if its a proxy).
  46. *
  47. * @param object $object
  48. *
  49. * @return string
  50. */
  51. public static function getClass($object)
  52. {
  53. return self::getRealClass(get_class($object));
  54. }
  55. /**
  56. * Gets the real parent class name of a class or object.
  57. *
  58. * @param string $className
  59. *
  60. * @return string
  61. */
  62. public static function getParentClass($className)
  63. {
  64. return get_parent_class( self::getRealClass( $className ) );
  65. }
  66. /**
  67. * Creates a new reflection class.
  68. *
  69. * @param string $class
  70. *
  71. * @return \ReflectionClass
  72. */
  73. public static function newReflectionClass($class)
  74. {
  75. return new \ReflectionClass( self::getRealClass( $class ) );
  76. }
  77. /**
  78. * Creates a new reflection object.
  79. *
  80. * @param object $object
  81. *
  82. * @return \ReflectionObject
  83. */
  84. public static function newReflectionObject($object)
  85. {
  86. return self::newReflectionClass( self::getClass( $object ) );
  87. }
  88. /**
  89. * Given a class name and a proxy namespace returns the proxy name.
  90. *
  91. * @param string $className
  92. * @param string $proxyNamespace
  93. *
  94. * @return string
  95. */
  96. public static function generateProxyClassName($className, $proxyNamespace)
  97. {
  98. return rtrim($proxyNamespace, '\\') . '\\'.Proxy::MARKER.'\\' . ltrim($className, '\\');
  99. }
  100. }