|
| 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\ClassMetadataFactory; |
| 8 | + |
| 9 | +$config = new MongoDB\Configuration(); |
| 10 | +$config->setProxyDir(__DIR__); |
| 11 | +$config->setProxyNamespace('PHPstan\Doctrine\OdmProxies'); |
| 12 | +$config->setMetadataCacheImpl(new ArrayCache()); |
| 13 | +$config->setHydratorDir(__DIR__); |
| 14 | +$config->setHydratorNamespace('PHPstan\Doctrine\OdmHydrators'); |
| 15 | + |
| 16 | +$config->setMetadataDriverImpl( |
| 17 | + new MongoDB\Mapping\Driver\AnnotationDriver( |
| 18 | + new AnnotationReader(), |
| 19 | + [__DIR__ . '/ObjectMetadataResolverTest.php'] |
| 20 | + ) |
| 21 | +); |
| 22 | + |
| 23 | +$documentManager = MongoDB\DocumentManager::create( |
| 24 | + null, |
| 25 | + $config |
| 26 | +); |
| 27 | + |
| 28 | +$config = new ORM\Configuration(); |
| 29 | +$config->setProxyDir(__DIR__); |
| 30 | +$config->setProxyNamespace('PHPstan\Doctrine\OrmProxies'); |
| 31 | +$config->setMetadataCacheImpl(new ArrayCache()); |
| 32 | + |
| 33 | +$config->setMetadataDriverImpl( |
| 34 | + new ORM\Mapping\Driver\AnnotationDriver( |
| 35 | + new AnnotationReader(), |
| 36 | + [__DIR__ . '/ObjectMetadataResolverTest.php'] |
| 37 | + ) |
| 38 | +); |
| 39 | + |
| 40 | +$entityManager = ORM\EntityManager::create( |
| 41 | + [ |
| 42 | + 'driver' => 'pdo_sqlite', |
| 43 | + 'memory' => true, |
| 44 | + ], |
| 45 | + $config |
| 46 | +); |
| 47 | + |
| 48 | +$metadataFactory = new class($documentManager, $entityManager) implements ClassMetadataFactory |
| 49 | +{ |
| 50 | + /** @var MongoDB\DocumentManager */ |
| 51 | + private $documentManager; |
| 52 | + |
| 53 | + /** @var ORM\EntityManager */ |
| 54 | + private $entityManager; |
| 55 | + |
| 56 | + public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager) |
| 57 | + { |
| 58 | + $this->documentManager = $documentManager; |
| 59 | + $this->entityManager = $entityManager; |
| 60 | + } |
| 61 | + |
| 62 | + public function getAllMetadata() |
| 63 | + { |
| 64 | + return array_merge( |
| 65 | + $this->documentManager->getMetadataFactory()->getAllMetadata(), |
| 66 | + $this->entityManager->getMetadataFactory()->getAllMetadata() |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function getMetadataFor($className) |
| 71 | + { |
| 72 | + throw new \Exception(__FILE__); |
| 73 | + } |
| 74 | + |
| 75 | + public function isTransient($className) |
| 76 | + { |
| 77 | + throw new \Exception(__FILE__); |
| 78 | + } |
| 79 | + |
| 80 | + public function hasMetadataFor($className) |
| 81 | + { |
| 82 | + throw new \Exception(__FILE__); |
| 83 | + } |
| 84 | + |
| 85 | + public function setMetadataFor($className, $class) |
| 86 | + { |
| 87 | + throw new \Exception(__FILE__); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +return new class($documentManager, $entityManager, $metadataFactory) implements \Doctrine\Persistence\ObjectManager |
| 92 | +{ |
| 93 | + /** @var MongoDB\DocumentManager */ |
| 94 | + private $documentManager; |
| 95 | + |
| 96 | + /** @var ORM\EntityManager */ |
| 97 | + private $entityManager; |
| 98 | + |
| 99 | + /** @var ClassMetadataFactory */ |
| 100 | + private $classMetadataFactory; |
| 101 | + |
| 102 | + public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager, ClassMetadataFactory $classMetadataFactory) |
| 103 | + { |
| 104 | + $this->documentManager = $documentManager; |
| 105 | + $this->entityManager = $entityManager; |
| 106 | + $this->classMetadataFactory = $classMetadataFactory; |
| 107 | + } |
| 108 | + |
| 109 | + public function getRepository($className) |
| 110 | + { |
| 111 | + if (strpos($className, 'Entity') !== false) { |
| 112 | + return $this->entityManager->getRepository($className); |
| 113 | + } |
| 114 | + |
| 115 | + return $this->documentManager->getRepository($className); |
| 116 | + } |
| 117 | + |
| 118 | + public function getClassMetadata($className) |
| 119 | + { |
| 120 | + if (strpos($className, 'Entity') !== false) { |
| 121 | + return $this->entityManager->getClassMetadata($className); |
| 122 | + } |
| 123 | + |
| 124 | + return $this->documentManager->getClassMetadata($className); |
| 125 | + } |
| 126 | + |
| 127 | + public function getMetadataFactory() |
| 128 | + { |
| 129 | + return $this->classMetadataFactory; |
| 130 | + } |
| 131 | + |
| 132 | + public function find($className, $id) |
| 133 | + { |
| 134 | + throw new \Exception(__FILE__); |
| 135 | + } |
| 136 | + |
| 137 | + public function persist($object) |
| 138 | + { |
| 139 | + throw new \Exception(__FILE__); |
| 140 | + } |
| 141 | + |
| 142 | + public function remove($object) |
| 143 | + { |
| 144 | + throw new \Exception(__FILE__); |
| 145 | + } |
| 146 | + |
| 147 | + public function merge($object) |
| 148 | + { |
| 149 | + throw new \Exception(__FILE__); |
| 150 | + } |
| 151 | + |
| 152 | + public function clear($objectName = null) |
| 153 | + { |
| 154 | + throw new \Exception(__FILE__); |
| 155 | + } |
| 156 | + |
| 157 | + public function detach($object) |
| 158 | + { |
| 159 | + throw new \Exception(__FILE__); |
| 160 | + } |
| 161 | + |
| 162 | + public function refresh($object) |
| 163 | + { |
| 164 | + throw new \Exception(__FILE__); |
| 165 | + } |
| 166 | + |
| 167 | + public function flush() |
| 168 | + { |
| 169 | + throw new \Exception(__FILE__); |
| 170 | + } |
| 171 | + |
| 172 | + public function initializeObject($obj) |
| 173 | + { |
| 174 | + throw new \Exception(__FILE__); |
| 175 | + } |
| 176 | + |
| 177 | + public function contains($object) |
| 178 | + { |
| 179 | + throw new \Exception(__FILE__); |
| 180 | + } |
| 181 | +}; |
0 commit comments