Annotation.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is an annotation mapping driver for Loggable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from Annotations specificaly for Loggable
  9. * extension.
  10. *
  11. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Loggable.Mapping.Driver
  14. * @subpackage Annotation
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Annotation implements AnnotationDriverInterface
  19. {
  20. /**
  21. * Annotation to define that this object is loggable
  22. */
  23. const LOGGABLE = 'Gedmo\\Mapping\\Annotation\\Loggable';
  24. /**
  25. * Annotation to define that this property is versioned
  26. */
  27. const VERSIONED = 'Gedmo\\Mapping\\Annotation\\Versioned';
  28. /**
  29. * Annotation reader instance
  30. *
  31. * @var object
  32. */
  33. private $reader;
  34. /**
  35. * original driver if it is available
  36. */
  37. protected $_originalDriver = null;
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function setAnnotationReader($reader)
  42. {
  43. $this->reader = $reader;
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function validateFullMetadata($meta, array $config)
  49. {
  50. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  51. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  52. }
  53. if (isset($config['versioned']) && !isset($config['loggable'])) {
  54. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  55. }
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function readExtendedMetadata($meta, array &$config)
  61. {
  62. $class = $meta->getReflectionClass();
  63. if (!$class) {
  64. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  65. // this happens when running annotation driver in combination with
  66. // static reflection services. This is not the nicest fix
  67. $class = new \ReflectionClass($meta->name);
  68. }
  69. // class annotations
  70. if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) {
  71. $config['loggable'] = true;
  72. if ($annot->logEntryClass) {
  73. if (!class_exists($annot->logEntryClass)) {
  74. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  75. }
  76. $config['logEntryClass'] = $annot->logEntryClass;
  77. }
  78. }
  79. // property annotations
  80. foreach ($class->getProperties() as $property) {
  81. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  82. $meta->isInheritedField($property->name) ||
  83. isset($meta->associationMappings[$property->name]['inherited'])
  84. ) {
  85. continue;
  86. }
  87. // versioned property
  88. if ($versioned = $this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
  89. $field = $property->getName();
  90. if ($meta->isCollectionValuedAssociation($field)) {
  91. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  92. }
  93. // fields cannot be overrided and throws mapping exception
  94. $config['versioned'][] = $field;
  95. }
  96. }
  97. if (!$meta->isMappedSuperclass && $config) {
  98. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  99. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  100. }
  101. if (isset($config['versioned']) && !isset($config['loggable'])) {
  102. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  103. }
  104. }
  105. }
  106. /**
  107. * Passes in the mapping read by original driver
  108. *
  109. * @param $driver
  110. * @return void
  111. */
  112. public function setOriginalDriver($driver)
  113. {
  114. $this->_originalDriver = $driver;
  115. }
  116. }