AbstractAnnotationDriver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Gedmo\Mapping\Driver;
  3. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  4. /**
  5. * This is an abstract class to implement common functionality
  6. * for extension annotation mapping drivers.
  7. *
  8. * @author Derek J. Lambert <dlambert@dereklambert.com>
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. abstract class AbstractAnnotationDriver implements AnnotationDriverInterface
  12. {
  13. /**
  14. * Annotation reader instance
  15. *
  16. * @var object
  17. */
  18. protected $reader;
  19. /**
  20. * Original driver if it is available
  21. */
  22. protected $_originalDriver = null;
  23. /**
  24. * List of types which are valid for extension
  25. *
  26. * @var array
  27. */
  28. protected $validTypes = array();
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function setAnnotationReader($reader)
  33. {
  34. $this->reader = $reader;
  35. }
  36. /**
  37. * Passes in the mapping read by original driver
  38. *
  39. * @param object $driver
  40. */
  41. public function setOriginalDriver($driver)
  42. {
  43. $this->_originalDriver = $driver;
  44. }
  45. /**
  46. * @param object $meta
  47. *
  48. * @return \ReflectionClass
  49. */
  50. public function getMetaReflectionClass($meta)
  51. {
  52. $class = $meta->getReflectionClass();
  53. if (!$class) {
  54. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  55. // this happens when running annotation driver in combination with
  56. // static reflection services. This is not the nicest fix
  57. $class = new \ReflectionClass($meta->name);
  58. }
  59. return $class;
  60. }
  61. /**
  62. * Checks if $field type is valid
  63. *
  64. * @param object $meta
  65. * @param string $field
  66. *
  67. * @return boolean
  68. */
  69. protected function isValidField($meta, $field)
  70. {
  71. $mapping = $meta->getFieldMapping($field);
  72. return $mapping && in_array($mapping['type'], $this->validTypes);
  73. }
  74. /**
  75. * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $meta
  76. * @param array $config
  77. */
  78. public function validateFullMetadata(ClassMetadata $meta, array $config)
  79. {
  80. }
  81. /**
  82. * Try to find out related class name out of mapping
  83. *
  84. * @param ClassMetadata $metadata - the mapped class metadata
  85. * @param $name - the related object class name
  86. * @return string - related class name or empty string if does not exist
  87. */
  88. protected function getRelatedClassName($metadata, $name)
  89. {
  90. if (class_exists($name) || interface_exists($name)) {
  91. return $name;
  92. }
  93. $refl = $metadata->getReflectionClass();
  94. $ns = $refl->getNamespaceName();
  95. $className = $ns . '\\' . $name;
  96. return class_exists($className) ? $className : '';
  97. }
  98. }