Annotation.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  4. use Gedmo\Exception\InvalidMappingException;
  5. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  6. /**
  7. * This is an annotation mapping driver for Loggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations specifically for Loggable
  10. * extension.
  11. *
  12. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Annotation extends AbstractAnnotationDriver
  17. {
  18. /**
  19. * Annotation to define that this object is loggable
  20. */
  21. const LOGGABLE = 'Gedmo\\Mapping\\Annotation\\Loggable';
  22. /**
  23. * Annotation to define that this property is versioned
  24. */
  25. const VERSIONED = 'Gedmo\\Mapping\\Annotation\\Versioned';
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function validateFullMetadata(ClassMetadata $meta, array $config)
  30. {
  31. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  32. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  33. }
  34. if (isset($config['versioned']) && !isset($config['loggable'])) {
  35. throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  36. }
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function readExtendedMetadata($meta, array &$config)
  42. {
  43. $class = $this->getMetaReflectionClass($meta);
  44. // class annotations
  45. if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) {
  46. $config['loggable'] = true;
  47. if ($annot->logEntryClass) {
  48. if (!$cl = $this->getRelatedClassName($meta, $annot->logEntryClass)) {
  49. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  50. }
  51. $config['logEntryClass'] = $cl;
  52. }
  53. }
  54. // property annotations
  55. foreach ($class->getProperties() as $property) {
  56. $field = $property->getName();
  57. if ($meta->isMappedSuperclass && !$property->isPrivate()) {
  58. continue;
  59. }
  60. // versioned property
  61. if ($this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
  62. if (!$this->isMappingValid($meta, $field)) {
  63. throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
  64. }
  65. if (isset($meta->embeddedClasses[$field])) {
  66. $this->inspectEmbeddedForVersioned($field, $config, $meta);
  67. continue;
  68. }
  69. // fields cannot be overrided and throws mapping exception
  70. if (!(isset($config['versioned']) && in_array($field, $config['versioned']))) {
  71. $config['versioned'][] = $field;
  72. }
  73. }
  74. }
  75. if (!$meta->isMappedSuperclass && $config) {
  76. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  77. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  78. }
  79. if ($this->isClassAnnotationInValid($meta, $config)) {
  80. throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  81. }
  82. }
  83. }
  84. /**
  85. * @param ClassMetadata $meta
  86. * @param string $field
  87. *
  88. * @return bool
  89. */
  90. protected function isMappingValid(ClassMetadata $meta, $field)
  91. {
  92. return $meta->isCollectionValuedAssociation($field) == false;
  93. }
  94. /**
  95. * @param ClassMetadata $meta
  96. * @param array $config
  97. *
  98. * @return bool
  99. */
  100. protected function isClassAnnotationInValid(ClassMetadata $meta, array &$config)
  101. {
  102. return isset($config['versioned']) && !isset($config['loggable']) && (!isset($meta->isEmbeddedClass) || !$meta->isEmbeddedClass);
  103. }
  104. /**
  105. * Searches properties of embedded object for versioned fields
  106. *
  107. * @param string $field
  108. * @param array $config
  109. * @param \Doctrine\ORM\Mapping\ClassMetadata $meta
  110. */
  111. private function inspectEmbeddedForVersioned($field, array &$config, \Doctrine\ORM\Mapping\ClassMetadata $meta)
  112. {
  113. $сlass = new \ReflectionClass($meta->embeddedClasses[$field]['class']);
  114. // property annotations
  115. foreach ($сlass->getProperties() as $property) {
  116. // versioned property
  117. if ($this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
  118. $embeddedField = $field . '.' . $property->getName();
  119. $config['versioned'][] = $embeddedField;
  120. if (isset($meta->embeddedClasses[$embeddedField])) {
  121. $this->inspectEmbeddedForVersioned($embeddedField, $config, $meta);
  122. }
  123. }
  124. }
  125. }
  126. }