|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Camelot\DoctrineInheritanceMapping\Annotation; |
| 6 | + |
| 7 | +use Doctrine\Common\Annotations\Reader; |
| 8 | +use Doctrine\Common\Collections\ArrayCollection; |
| 9 | +use Doctrine\Common\Collections\Collection; |
| 10 | +use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
| 11 | +use Doctrine\ORM\Configuration; |
| 12 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 13 | +use ReflectionClass; |
| 14 | +use RuntimeException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Based on work by Jasper Kuperus <github@jasperkuperus.nl> |
| 18 | + */ |
| 19 | +final class DiscriminatorMapLoader |
| 20 | +{ |
| 21 | + /** @var Reader */ |
| 22 | + private $reader; |
| 23 | + /** @var MappingDriver */ |
| 24 | + private $mappingDriver; |
| 25 | + /** @var array */ |
| 26 | + private $cachedMap; |
| 27 | + |
| 28 | + public function __construct(Reader $reader, Configuration $config) |
| 29 | + { |
| 30 | + $this->reader = $reader; |
| 31 | + $this->mappingDriver = $config->getMetadataDriverImpl(); |
| 32 | + $this->cachedMap = []; |
| 33 | + } |
| 34 | + |
| 35 | + public function loadClassMetadata(ClassMetadata $classMetadata): void |
| 36 | + { |
| 37 | + $map = new ArrayCollection(); |
| 38 | + $class = $classMetadata->name; |
| 39 | + |
| 40 | + if (array_key_exists($class, $this->cachedMap)) { |
| 41 | + $this->overrideMetadata($classMetadata, $class); |
| 42 | + |
| 43 | + return; |
| 44 | + } |
| 45 | + $this->extractEntry($map, $class); |
| 46 | + if (!$map->containsKey($class)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + $this->checkFamily($map, $class); |
| 50 | + $this->createEntries($map); |
| 51 | + $this->overrideMetadata($classMetadata, $class); |
| 52 | + } |
| 53 | + |
| 54 | + private function createEntries(Collection $map) |
| 55 | + { |
| 56 | + $discriminatorMap = array_flip($map->toArray()); |
| 57 | + foreach ($map as $className => $discriminatorItem) { |
| 58 | + $this->cachedMap[$className]['map'] = $discriminatorMap; |
| 59 | + $this->cachedMap[$className]['discr'] = $map->get($className); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set the discriminator map, discr value & subclasses |
| 65 | + */ |
| 66 | + private function overrideMetadata(ClassMetadata $classMetadata, $class) |
| 67 | + { |
| 68 | + $classMetadata->discriminatorMap = $this->cachedMap[$class]['map']; |
| 69 | + $classMetadata->discriminatorValue = $this->cachedMap[$class]['discr']; |
| 70 | + if (isset($this->cachedMap[$class]['isParent']) && true === $this->cachedMap[$class]['isParent']) { |
| 71 | + $subclasses = $this->cachedMap[$class]['map']; |
| 72 | + unset($subclasses[$this->cachedMap[$class]['discr']]); |
| 73 | + $classMetadata->subClasses = array_values($subclasses); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Build the whole map. |
| 79 | + * |
| 80 | + * @throws \ReflectionException |
| 81 | + */ |
| 82 | + private function checkFamily(Collection $map, string $class): void |
| 83 | + { |
| 84 | + $rc = new ReflectionClass($class); |
| 85 | + $parent = $rc->getParentClass() ? $rc->getParentClass()->name : null; |
| 86 | + if ($parent !== null) { |
| 87 | + $this->checkFamily($map, $parent); |
| 88 | + } |
| 89 | + $this->cachedMap[$class]['isParent'] = true; |
| 90 | + $this->checkChildren($map, $class); |
| 91 | + } |
| 92 | + |
| 93 | + private function checkChildren(Collection $map, string $class): void |
| 94 | + { |
| 95 | + foreach ($this->mappingDriver->getAllClassNames() as $name) { |
| 96 | + $childRc = new ReflectionClass($name); |
| 97 | + $classParent = $childRc->getParentClass() ? $childRc->getParentClass()->name : null; |
| 98 | + if (!$classParent) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + if ($map->containsKey($name) || $classParent !== $class) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + $this->extractEntry($map, $name); |
| 105 | + $this->checkChildren($map, $name); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private function extractEntry(Collection $map, string $class): void |
| 110 | + { |
| 111 | + $rc = new ReflectionClass($class); |
| 112 | + foreach ($this->reader->getClassAnnotations($rc) as $annotation) { |
| 113 | + if (!$annotation instanceof DiscriminatorMapItem) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + $value = $annotation->getValue(); |
| 117 | + if ($map->containsKey($value)) { |
| 118 | + throw new RuntimeException(sprintf("Found duplicate discriminator map entry '%s' in %s", $value, $class)); |
| 119 | + } |
| 120 | + $map->set($class, $value); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments