Annotation.php 3.3 KB

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