Annotation.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Doctrine\ORM\Mapping\ClassMetadata;
  4. use 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. * @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 extends AbstractAnnotationDriver
  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. * {@inheritDoc}
  31. */
  32. public function validateFullMetadata(ClassMetadata $meta, array $config)
  33. {
  34. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  35. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  36. }
  37. if (isset($config['versioned']) && !isset($config['loggable'])) {
  38. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  39. }
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function readExtendedMetadata($meta, array &$config)
  45. {
  46. $class = $this->getMetaReflectionClass($meta);
  47. // class annotations
  48. if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) {
  49. $config['loggable'] = true;
  50. if ($annot->logEntryClass) {
  51. if (!class_exists($annot->logEntryClass)) {
  52. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  53. }
  54. $config['logEntryClass'] = $annot->logEntryClass;
  55. }
  56. }
  57. // property annotations
  58. foreach ($class->getProperties() as $property) {
  59. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  60. $meta->isInheritedField($property->name) ||
  61. isset($meta->associationMappings[$property->name]['inherited'])
  62. ) {
  63. continue;
  64. }
  65. // versioned property
  66. if ($versioned = $this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
  67. $field = $property->getName();
  68. if ($meta->isCollectionValuedAssociation($field)) {
  69. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  70. }
  71. // fields cannot be overrided and throws mapping exception
  72. $config['versioned'][] = $field;
  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 (isset($config['versioned']) && !isset($config['loggable'])) {
  80. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  81. }
  82. }
  83. }
  84. }