|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 4 | +use Doctrine\Common\Cache\ArrayCache; |
| 5 | +use Doctrine\ODM\MongoDB; |
| 6 | +use Doctrine\ORM; |
| 7 | +use Doctrine\Persistence\Mapping\ClassMetadata; |
| 8 | +use Doctrine\Persistence\Mapping\ClassMetadataFactory; |
| 9 | +use Doctrine\Persistence\ObjectManager; |
| 10 | +use Doctrine\Persistence\ObjectRepository; |
| 11 | + |
| 12 | +$config = new MongoDB\Configuration(); |
| 13 | +$config->setProxyDir(__DIR__); |
| 14 | +$config->setProxyNamespace('PHPstan\Doctrine\OdmProxies'); |
| 15 | +$config->setMetadataCacheImpl(new ArrayCache()); |
| 16 | +$config->setHydratorDir(__DIR__); |
| 17 | +$config->setHydratorNamespace('PHPstan\Doctrine\OdmHydrators'); |
| 18 | + |
| 19 | +$config->setMetadataDriverImpl( |
| 20 | + new MongoDB\Mapping\Driver\AnnotationDriver( |
| 21 | + new AnnotationReader(), |
| 22 | + [__DIR__ . '/ObjectMetadataResolverTest.php'] |
| 23 | + ) |
| 24 | +); |
| 25 | + |
| 26 | +$documentManager = MongoDB\DocumentManager::create( |
| 27 | + null, |
| 28 | + $config |
| 29 | +); |
| 30 | + |
| 31 | +$config = new ORM\Configuration(); |
| 32 | +$config->setProxyDir(__DIR__); |
| 33 | +$config->setProxyNamespace('PHPstan\Doctrine\OrmProxies'); |
| 34 | +$config->setMetadataCacheImpl(new ArrayCache()); |
| 35 | + |
| 36 | +$config->setMetadataDriverImpl( |
| 37 | + new ORM\Mapping\Driver\AnnotationDriver( |
| 38 | + new AnnotationReader(), |
| 39 | + [__DIR__ . '/ObjectMetadataResolverTest.php'] |
| 40 | + ) |
| 41 | +); |
| 42 | + |
| 43 | +$entityManager = ORM\EntityManager::create( |
| 44 | + [ |
| 45 | + 'driver' => 'pdo_sqlite', |
| 46 | + 'memory' => true, |
| 47 | + ], |
| 48 | + $config |
| 49 | +); |
| 50 | + |
| 51 | +$metadataFactory = new class($documentManager, $entityManager) implements ClassMetadataFactory |
| 52 | +{ |
| 53 | + |
| 54 | + /** @var MongoDB\DocumentManager */ |
| 55 | + private $documentManager; |
| 56 | + |
| 57 | + /** @var ORM\EntityManager */ |
| 58 | + private $entityManager; |
| 59 | + |
| 60 | + public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager) |
| 61 | + { |
| 62 | + $this->documentManager = $documentManager; |
| 63 | + $this->entityManager = $entityManager; |
| 64 | + } |
| 65 | + |
| 66 | + public function getAllMetadata(): array |
| 67 | + { |
| 68 | + return array_merge( |
| 69 | + $this->documentManager->getMetadataFactory()->getAllMetadata(), |
| 70 | + $this->entityManager->getMetadataFactory()->getAllMetadata() |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param string $className |
| 76 | + */ |
| 77 | + public function getMetadataFor($className): void |
| 78 | + { |
| 79 | + throw new \Exception(__FILE__); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param string $className |
| 84 | + */ |
| 85 | + public function isTransient($className): void |
| 86 | + { |
| 87 | + throw new \Exception(__FILE__); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $className |
| 92 | + */ |
| 93 | + public function hasMetadataFor($className): void |
| 94 | + { |
| 95 | + throw new \Exception(__FILE__); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param string $className |
| 100 | + * @param ClassMetadata $class |
| 101 | + */ |
| 102 | + public function setMetadataFor($className, $class): void |
| 103 | + { |
| 104 | + throw new \Exception(__FILE__); |
| 105 | + } |
| 106 | + |
| 107 | +}; |
| 108 | + |
| 109 | +return new class($documentManager, $entityManager, $metadataFactory) implements ObjectManager |
| 110 | +{ |
| 111 | + |
| 112 | + /** @var MongoDB\DocumentManager */ |
| 113 | + private $documentManager; |
| 114 | + |
| 115 | + /** @var ORM\EntityManager */ |
| 116 | + private $entityManager; |
| 117 | + |
| 118 | + /** @var ClassMetadataFactory */ |
| 119 | + private $classMetadataFactory; |
| 120 | + |
| 121 | + public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager, ClassMetadataFactory $classMetadataFactory) |
| 122 | + { |
| 123 | + $this->documentManager = $documentManager; |
| 124 | + $this->entityManager = $entityManager; |
| 125 | + $this->classMetadataFactory = $classMetadataFactory; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param string $className |
| 130 | + */ |
| 131 | + public function getRepository($className): ObjectRepository |
| 132 | + { |
| 133 | + if (strpos($className, 'Entity') !== false) { |
| 134 | + return $this->entityManager->getRepository($className); |
| 135 | + } |
| 136 | + |
| 137 | + return $this->documentManager->getRepository($className); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param string $className |
| 142 | + */ |
| 143 | + public function getClassMetadata($className): ClassMetadata |
| 144 | + { |
| 145 | + if (strpos($className, 'Entity') !== false) { |
| 146 | + return $this->entityManager->getClassMetadata($className); |
| 147 | + } |
| 148 | + |
| 149 | + return $this->documentManager->getClassMetadata($className); |
| 150 | + } |
| 151 | + |
| 152 | + public function getMetadataFactory(): ClassMetadataFactory |
| 153 | + { |
| 154 | + return $this->classMetadataFactory; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param string $className |
| 159 | + * @param mixed $id |
| 160 | + */ |
| 161 | + public function find($className, $id): void |
| 162 | + { |
| 163 | + throw new \Exception(__FILE__); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param object $object |
| 168 | + */ |
| 169 | + public function persist($object): void |
| 170 | + { |
| 171 | + throw new \Exception(__FILE__); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @param object $object |
| 176 | + */ |
| 177 | + public function remove($object): void |
| 178 | + { |
| 179 | + throw new \Exception(__FILE__); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @param object $object |
| 184 | + */ |
| 185 | + public function merge($object): void |
| 186 | + { |
| 187 | + throw new \Exception(__FILE__); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @param string|null $objectName |
| 192 | + */ |
| 193 | + public function clear($objectName = null): void |
| 194 | + { |
| 195 | + throw new \Exception(__FILE__); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @param object $object |
| 200 | + */ |
| 201 | + public function detach($object): void |
| 202 | + { |
| 203 | + throw new \Exception(__FILE__); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @param object $object |
| 208 | + */ |
| 209 | + public function refresh($object): void |
| 210 | + { |
| 211 | + throw new \Exception(__FILE__); |
| 212 | + } |
| 213 | + |
| 214 | + public function flush(): void |
| 215 | + { |
| 216 | + throw new \Exception(__FILE__); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * @param object $object |
| 221 | + */ |
| 222 | + public function initializeObject($object): void |
| 223 | + { |
| 224 | + throw new \Exception(__FILE__); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * @param object $object |
| 229 | + */ |
| 230 | + public function contains($object): bool |
| 231 | + { |
| 232 | + throw new \Exception(__FILE__); |
| 233 | + } |
| 234 | + |
| 235 | +}; |
0 commit comments