Annotation.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is an annotation mapping driver for Loggable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for Loggable
  11. * extension.
  12. *
  13. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @package Gedmo.Loggable.Mapping.Driver
  16. * @subpackage Annotation
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Annotation implements Driver
  21. {
  22. /**
  23. * Annotation to define that this object is loggable
  24. */
  25. const ANNOTATION_LOGGABLE = 'Gedmo\Loggable\Mapping\Loggable';
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function validateFullMetadata(ClassMetadata $meta, array $config)
  30. {
  31. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  32. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  33. }
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  39. {
  40. require_once __DIR__ . '/../Annotations.php';
  41. $reader = new AnnotationReader();
  42. $reader->setAnnotationNamespaceAlias('Gedmo\Loggable\Mapping\\', 'gedmo');
  43. $class = $meta->getReflectionClass();
  44. // class annotations
  45. $classAnnotations = $reader->getClassAnnotations($class);
  46. if (isset($classAnnotations[self::ANNOTATION_LOGGABLE])) {
  47. $config['loggable'] = true;
  48. $annot = $classAnnotations[self::ANNOTATION_LOGGABLE];
  49. if ($annot->logEntryClass) {
  50. if (!class_exists($annot->logEntryClass)) {
  51. throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  52. }
  53. $config['logEntryClass'] = $annot->logEntryClass;
  54. }
  55. }
  56. }
  57. }