Annotation.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Doctrine\Common\Persistence\Mapping\ClassMetadata,
  4. Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  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. * @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 annoted 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 (!class_exists($annot->logEntryClass)) {
  49. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  50. }
  51. $config['logEntryClass'] = $annot->logEntryClass;
  52. }
  53. }
  54. // property annotations
  55. foreach ($class->getProperties() as $property) {
  56. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  57. $meta->isInheritedField($property->name) ||
  58. isset($meta->associationMappings[$property->name]['inherited'])
  59. ) {
  60. continue;
  61. }
  62. // versioned property
  63. if ($versioned = $this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
  64. $field = $property->getName();
  65. if ($meta->isCollectionValuedAssociation($field)) {
  66. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  67. }
  68. // fields cannot be overrided and throws mapping exception
  69. $config['versioned'][] = $field;
  70. }
  71. }
  72. if (!$meta->isMappedSuperclass && $config) {
  73. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  74. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  75. }
  76. if (isset($config['versioned']) && !isset($config['loggable'])) {
  77. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  78. }
  79. }
  80. }
  81. }