RuntimeReflectionService.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Persistence\Mapping;
  20. use ReflectionClass;
  21. use ReflectionProperty;
  22. use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
  23. use Doctrine\Common\Persistence\Mapping\MappingException;
  24. /**
  25. * PHP Runtime Reflection Service.
  26. *
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class RuntimeReflectionService implements ReflectionService
  30. {
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function getParentClasses($class)
  35. {
  36. if ( ! class_exists($class)) {
  37. throw MappingException::nonExistingClass($class);
  38. }
  39. return class_parents($class);
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getClassShortName($class)
  45. {
  46. $reflectionClass = new ReflectionClass($class);
  47. return $reflectionClass->getShortName();
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function getClassNamespace($class)
  53. {
  54. $reflectionClass = new ReflectionClass($class);
  55. return $reflectionClass->getNamespaceName();
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getClass($class)
  61. {
  62. return new ReflectionClass($class);
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getAccessibleProperty($class, $property)
  68. {
  69. $reflectionProperty = new ReflectionProperty($class, $property);
  70. if ($reflectionProperty->isPublic()) {
  71. $reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
  72. }
  73. $reflectionProperty->setAccessible(true);
  74. return $reflectionProperty;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function hasPublicMethod($class, $method)
  80. {
  81. return method_exists($class, $method) && is_callable(array($class, $method));
  82. }
  83. }