ClassUtils.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 LGPL. 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. * Get the real class name of a class name that could be a proxy.
  32. *
  33. * @param string
  34. * @return string
  35. */
  36. public static function getRealClass($class)
  37. {
  38. if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) {
  39. return $class;
  40. }
  41. return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
  42. }
  43. /**
  44. * Get the real class name of an object (even if its a proxy)
  45. *
  46. * @param object
  47. * @return string
  48. */
  49. public static function getClass($object)
  50. {
  51. return self::getRealClass(get_class($object));
  52. }
  53. /**
  54. * Get the real parent class name of a class or object
  55. *
  56. * @param string
  57. * @return string
  58. */
  59. public static function getParentClass($className)
  60. {
  61. return get_parent_class( self::getRealClass( $className ) );
  62. }
  63. /**
  64. * Create a new reflection class
  65. *
  66. * @param string
  67. * @return ReflectionClass
  68. */
  69. public static function newReflectionClass($class)
  70. {
  71. return new \ReflectionClass( self::getRealClass( $class ) );
  72. }
  73. /**
  74. * Create a new reflection object
  75. *
  76. * @param object
  77. * @return ReflectionObject
  78. */
  79. public static function newReflectionObject($object)
  80. {
  81. return self::newReflectionClass( self::getClass( $object ) );
  82. }
  83. /**
  84. * Given a class name and a proxy namespace return the proxy name.
  85. *
  86. * @param string $className
  87. * @param string $proxyNamespace
  88. * @return string
  89. */
  90. public static function generateProxyClassName($className, $proxyNamespace)
  91. {
  92. return rtrim($proxyNamespace, '\\') . '\\'.Proxy::MARKER.'\\' . ltrim($className, '\\');
  93. }
  94. }