vendor/gedmo/doctrine-extensions/src/Loggable/Mapping/Driver/Annotation.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Doctrine Behavioral Extensions package.
  4.  * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Gedmo\Loggable\Mapping\Driver;
  9. use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as ClassMetadataODM;
  10. use Doctrine\Persistence\Mapping\ClassMetadata;
  11. use Gedmo\Exception\InvalidMappingException;
  12. use Gedmo\Mapping\Annotation\Loggable;
  13. use Gedmo\Mapping\Annotation\Versioned;
  14. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  15. /**
  16.  * This is an annotation mapping driver for Loggable
  17.  * behavioral extension. Used for extraction of extended
  18.  * metadata from Annotations specifically for Loggable
  19.  * extension.
  20.  *
  21.  * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  22.  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  23.  *
  24.  * @internal
  25.  */
  26. class Annotation extends AbstractAnnotationDriver
  27. {
  28.     /**
  29.      * Annotation to define that this object is loggable
  30.      */
  31.     public const LOGGABLE Loggable::class;
  32.     /**
  33.      * Annotation to define that this property is versioned
  34.      */
  35.     public const VERSIONED Versioned::class;
  36.     public function validateFullMetadata(ClassMetadata $meta, array $config)
  37.     {
  38.         if ($config && $meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
  39.             throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
  40.         }
  41.         if (isset($config['versioned']) && !isset($config['loggable'])) {
  42.             throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->getName()}");
  43.         }
  44.     }
  45.     public function readExtendedMetadata($meta, array &$config)
  46.     {
  47.         $class $this->getMetaReflectionClass($meta);
  48.         // class annotations
  49.         if ($annot $this->reader->getClassAnnotation($classself::LOGGABLE)) {
  50.             $config['loggable'] = true;
  51.             if ($annot->logEntryClass) {
  52.                 if (!$cl $this->getRelatedClassName($meta$annot->logEntryClass)) {
  53.                     throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
  54.                 }
  55.                 $config['logEntryClass'] = $cl;
  56.             }
  57.         }
  58.         // property annotations
  59.         foreach ($class->getProperties() as $property) {
  60.             $field $property->getName();
  61.             if ($meta->isMappedSuperclass && !$property->isPrivate()) {
  62.                 continue;
  63.             }
  64.             // versioned property
  65.             if ($this->reader->getPropertyAnnotation($propertyself::VERSIONED)) {
  66.                 if (!$this->isMappingValid($meta$field)) {
  67.                     throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->getName()}");
  68.                 }
  69.                 if (isset($meta->embeddedClasses[$field])) {
  70.                     $this->inspectEmbeddedForVersioned($field$config$meta);
  71.                     continue;
  72.                 }
  73.                 // fields cannot be overrided and throws mapping exception
  74.                 if (!in_array($field$config['versioned'] ?? [], true)) {
  75.                     $config['versioned'][] = $field;
  76.                 }
  77.             }
  78.         }
  79.         if (!$meta->isMappedSuperclass && $config) {
  80.             if ($meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
  81.                 throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
  82.             }
  83.             if ($this->isClassAnnotationInValid($meta$config)) {
  84.                 throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->getName()}");
  85.             }
  86.         }
  87.         return $config;
  88.     }
  89.     /**
  90.      * @param string $field
  91.      *
  92.      * @return bool
  93.      */
  94.     protected function isMappingValid(ClassMetadata $meta$field)
  95.     {
  96.         return false == $meta->isCollectionValuedAssociation($field);
  97.     }
  98.     /**
  99.      * @param array<string, mixed> $config
  100.      *
  101.      * @return bool
  102.      */
  103.     protected function isClassAnnotationInValid(ClassMetadata $meta, array &$config)
  104.     {
  105.         return isset($config['versioned']) && !isset($config['loggable']) && (!isset($meta->isEmbeddedClass) || !$meta->isEmbeddedClass);
  106.     }
  107.     /**
  108.      * Searches properties of embedded object for versioned fields
  109.      *
  110.      * @param array<string, mixed> $config
  111.      */
  112.     private function inspectEmbeddedForVersioned(string $field, array &$config\Doctrine\ORM\Mapping\ClassMetadata $meta): void
  113.     {
  114.         $class = new \ReflectionClass($meta->embeddedClasses[$field]['class']);
  115.         // property annotations
  116.         foreach ($class->getProperties() as $property) {
  117.             // versioned property
  118.             if ($this->reader->getPropertyAnnotation($propertyself::VERSIONED)) {
  119.                 $embeddedField $field.'.'.$property->getName();
  120.                 $config['versioned'][] = $embeddedField;
  121.                 if (isset($meta->embeddedClasses[$embeddedField])) {
  122.                     $this->inspectEmbeddedForVersioned($embeddedField$config$meta);
  123.                 }
  124.             }
  125.         }
  126.     }
  127. }