Annotation.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * original driver if it is available
  37. */
  38. protected $_originalDriver = null;
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function setAnnotationReader($reader)
  43. {
  44. $this->reader = $reader;
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function validateFullMetadata(ClassMetadata $meta, array $config)
  50. {
  51. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  52. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  53. }
  54. if (isset($config['versioned']) && !isset($config['loggable'])) {
  55. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  56. }
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  62. {
  63. $class = $meta->getReflectionClass();
  64. // class annotations
  65. if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) {
  66. $config['loggable'] = true;
  67. if ($annot->logEntryClass) {
  68. if (!class_exists($annot->logEntryClass)) {
  69. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  70. }
  71. $config['logEntryClass'] = $annot->logEntryClass;
  72. }
  73. }
  74. // property annotations
  75. foreach ($class->getProperties() as $property) {
  76. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  77. $meta->isInheritedField($property->name) ||
  78. isset($meta->associationMappings[$property->name]['inherited'])
  79. ) {
  80. continue;
  81. }
  82. // versioned property
  83. if ($versioned = $this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
  84. $field = $property->getName();
  85. if ($meta->isCollectionValuedAssociation($field)) {
  86. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  87. }
  88. // fields cannot be overrided and throws mapping exception
  89. $config['versioned'][] = $field;
  90. }
  91. }
  92. }
  93. /**
  94. * Passes in the mapping read by original driver
  95. *
  96. * @param $driver
  97. * @return void
  98. */
  99. public function setOriginalDriver($driver)
  100. {
  101. $this->_originalDriver = $driver;
  102. }
  103. }