Annotation.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Gedmo\References\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for References
  9. * behavioral extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  13. * @author Jonathan H. Wage <jonwage@gmail.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Annotation implements AnnotationDriverInterface
  17. {
  18. /**
  19. * Annotation to mark field as reference to one
  20. */
  21. const REFERENCE_ONE = 'Gedmo\\Mapping\\Annotation\\ReferenceOne';
  22. /**
  23. * Annotation to mark field as reference to many
  24. */
  25. const REFERENCE_MANY = 'Gedmo\\Mapping\\Annotation\\ReferenceMany';
  26. /**
  27. * Annotation to mark field as reference to many
  28. */
  29. const REFERENCE_MANY_EMBED = 'Gedmo\\Mapping\\Annotation\\ReferenceManyEmbed';
  30. private $annotations = array(
  31. 'referenceOne' => self::REFERENCE_ONE,
  32. 'referenceMany' => self::REFERENCE_MANY,
  33. 'referenceManyEmbed' => self::REFERENCE_MANY_EMBED,
  34. );
  35. /**
  36. * Annotation reader instance
  37. *
  38. * @var object
  39. */
  40. private $reader;
  41. /**
  42. * original driver if it is available
  43. */
  44. protected $_originalDriver = null;
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function setAnnotationReader($reader)
  49. {
  50. $this->reader = $reader;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function readExtendedMetadata($meta, array &$config)
  56. {
  57. $class = $meta->getReflectionClass();
  58. foreach($this->annotations as $key => $annotation) {
  59. $config[$key] = array();
  60. foreach ($class->getProperties() as $property) {
  61. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  62. $meta->isInheritedField($property->name) ||
  63. isset($meta->associationMappings[$property->name]['inherited'])
  64. ) {
  65. continue;
  66. }
  67. if ($reference = $this->reader->getPropertyAnnotation($property, $annotation)) {
  68. $config[$key][$property->getName()] = array(
  69. 'field' => $property->getName(),
  70. 'type' => $reference->type,
  71. 'class' => $reference->class,
  72. 'identifier' => $reference->identifier,
  73. 'mappedBy' => $reference->mappedBy,
  74. 'inversedBy' => $reference->inversedBy,
  75. );
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Passes in the mapping read by original driver
  82. *
  83. * @param $driver
  84. * @return void
  85. */
  86. public function setOriginalDriver($driver)
  87. {
  88. $this->_originalDriver = $driver;
  89. }
  90. }