Annotation.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Loggable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Loggable
  11. * extension.
  12. *
  13. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @package Gedmo.Loggable.Mapping.Driver
  16. * @subpackage Annotation
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Annotation implements Driver
  21. {
  22. /**
  23. * Annotation to define that this object is loggable
  24. */
  25. const LOGGABLE = 'Gedmo\\Mapping\\Annotation\\Loggable';
  26. /**
  27. * Annotation to define that this property is versioned
  28. */
  29. const VERSIONED = 'Gedmo\\Mapping\\Annotation\\Versioned';
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function validateFullMetadata(ClassMetadata $meta, array $config)
  34. {
  35. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  36. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  37. }
  38. if (isset($config['versioned']) && !isset($config['loggable'])) {
  39. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  46. {
  47. $reader = new AnnotationReader();
  48. $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
  49. $reader->setAutoloadAnnotations(true);
  50. $class = $meta->getReflectionClass();
  51. // class annotations
  52. $classAnnotations = $reader->getClassAnnotations($class);
  53. if (isset($classAnnotations[self::LOGGABLE])) {
  54. $config['loggable'] = true;
  55. $annot = $classAnnotations[self::LOGGABLE];
  56. if ($annot->logEntryClass) {
  57. if (!class_exists($annot->logEntryClass)) {
  58. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  59. }
  60. $config['logEntryClass'] = $annot->logEntryClass;
  61. }
  62. }
  63. // property annotations
  64. foreach ($class->getProperties() as $property) {
  65. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  66. $meta->isInheritedField($property->name) ||
  67. isset($meta->associationMappings[$property->name]['inherited'])
  68. ) {
  69. continue;
  70. }
  71. // versioned property
  72. if ($versioned = $reader->getPropertyAnnotation($property, self::VERSIONED)) {
  73. $field = $property->getName();
  74. if ($meta->isCollectionValuedAssociation($field)) {
  75. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  76. }
  77. // fields cannot be overrided and throws mapping exception
  78. $config['versioned'][] = $field;
  79. }
  80. }
  81. }
  82. }