|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Core\Persistence\Doctrine; |
| 10 | + |
| 11 | +use Doctrine\DBAL\Connection; |
| 12 | +use Doctrine\ORM\Cache; |
| 13 | +use Doctrine\ORM\Configuration; |
| 14 | +use Doctrine\ORM\EntityManagerInterface; |
| 15 | +use Doctrine\ORM\EntityRepository; |
| 16 | +use Doctrine\ORM\Internal\Hydration\AbstractHydrator; |
| 17 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 18 | +use Doctrine\ORM\Mapping\ClassMetadataFactory; |
| 19 | +use Doctrine\ORM\NativeQuery; |
| 20 | +use Doctrine\ORM\Proxy\ProxyFactory; |
| 21 | +use Doctrine\ORM\Query; |
| 22 | +use Doctrine\ORM\Query\Expr; |
| 23 | +use Doctrine\ORM\Query\FilterCollection; |
| 24 | +use Doctrine\ORM\Query\ResultSetMapping; |
| 25 | +use Doctrine\ORM\QueryBuilder; |
| 26 | +use Doctrine\ORM\UnitOfWork; |
| 27 | +use Ibexa\Bundle\Core\Entity\EntityManagerFactory; |
| 28 | +use Ibexa\Contracts\Core\MVC\EventSubscriber\ConfigScopeChangeSubscriber; |
| 29 | +use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent; |
| 30 | +use Symfony\Contracts\Service\ResetInterface; |
| 31 | + |
| 32 | +/** |
| 33 | + * @internal |
| 34 | + */ |
| 35 | +final class SiteAccessAwareEntityManager implements EntityManagerInterface, ConfigScopeChangeSubscriber, ResetInterface |
| 36 | +{ |
| 37 | + private EntityManagerFactory $entityManagerFactory; |
| 38 | + |
| 39 | + private ?EntityManagerInterface $resolvedEntityManager = null; |
| 40 | + |
| 41 | + public function __construct(EntityManagerFactory $entityManagerFactory) |
| 42 | + { |
| 43 | + $this->entityManagerFactory = $entityManagerFactory; |
| 44 | + } |
| 45 | + |
| 46 | + public function onConfigScopeChange(ScopeChangeEvent $event): void |
| 47 | + { |
| 48 | + $this->resolvedEntityManager = null; |
| 49 | + } |
| 50 | + |
| 51 | + public function reset(): void |
| 52 | + { |
| 53 | + $this->resolvedEntityManager = null; |
| 54 | + } |
| 55 | + |
| 56 | + private function getWrapped(): EntityManagerInterface |
| 57 | + { |
| 58 | + return $this->resolvedEntityManager ??= $this->entityManagerFactory->getEntityManager(); |
| 59 | + } |
| 60 | + |
| 61 | + public function getConnection(): Connection |
| 62 | + { |
| 63 | + return $this->getWrapped()->getConnection(); |
| 64 | + } |
| 65 | + |
| 66 | + public function getExpressionBuilder(): Expr |
| 67 | + { |
| 68 | + return $this->getWrapped()->getExpressionBuilder(); |
| 69 | + } |
| 70 | + |
| 71 | + public function beginTransaction(): void |
| 72 | + { |
| 73 | + $this->getWrapped()->beginTransaction(); |
| 74 | + } |
| 75 | + |
| 76 | + public function transactional($func) |
| 77 | + { |
| 78 | + return $this->getWrapped()->transactional($func); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return mixed |
| 83 | + */ |
| 84 | + public function wrapInTransaction(callable $func) |
| 85 | + { |
| 86 | + return $this->getWrapped()->wrapInTransaction($func); |
| 87 | + } |
| 88 | + |
| 89 | + public function commit(): void |
| 90 | + { |
| 91 | + $this->getWrapped()->commit(); |
| 92 | + } |
| 93 | + |
| 94 | + public function rollback(): void |
| 95 | + { |
| 96 | + $this->getWrapped()->rollback(); |
| 97 | + } |
| 98 | + |
| 99 | + public function createQuery($dql = ''): Query |
| 100 | + { |
| 101 | + return $this->getWrapped()->createQuery($dql); |
| 102 | + } |
| 103 | + |
| 104 | + public function createNamedQuery($name): Query |
| 105 | + { |
| 106 | + return $this->getWrapped()->createNamedQuery($name); |
| 107 | + } |
| 108 | + |
| 109 | + public function createNativeQuery($sql, ResultSetMapping $rsm): NativeQuery |
| 110 | + { |
| 111 | + return $this->getWrapped()->createNativeQuery($sql, $rsm); |
| 112 | + } |
| 113 | + |
| 114 | + public function createNamedNativeQuery($name): NativeQuery |
| 115 | + { |
| 116 | + return $this->getWrapped()->createNamedNativeQuery($name); |
| 117 | + } |
| 118 | + |
| 119 | + public function createQueryBuilder(): QueryBuilder |
| 120 | + { |
| 121 | + return $this->getWrapped()->createQueryBuilder(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @template T of object |
| 126 | + * |
| 127 | + * @param class-string<T> $entityName |
| 128 | + * |
| 129 | + * @return T|null |
| 130 | + */ |
| 131 | + public function getReference($entityName, $id): ?object |
| 132 | + { |
| 133 | + return $this->getWrapped()->getReference($entityName, $id); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @template T of object |
| 138 | + * |
| 139 | + * @param class-string<T> $entityName |
| 140 | + * |
| 141 | + * @return T|null |
| 142 | + */ |
| 143 | + public function getPartialReference($entityName, $identifier): ?object |
| 144 | + { |
| 145 | + return $this->getWrapped()->getPartialReference($entityName, $identifier); |
| 146 | + } |
| 147 | + |
| 148 | + public function close(): void |
| 149 | + { |
| 150 | + $this->getWrapped()->close(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @template T of object |
| 155 | + * |
| 156 | + * @param T $entity |
| 157 | + * @param bool $deep |
| 158 | + * |
| 159 | + * @return T |
| 160 | + */ |
| 161 | + public function copy($entity, $deep = false): object |
| 162 | + { |
| 163 | + /** @var T */ |
| 164 | + return $this->getWrapped()->copy($entity, $deep); |
| 165 | + } |
| 166 | + |
| 167 | + public function lock($entity, $lockMode, $lockVersion = null): void |
| 168 | + { |
| 169 | + $this->getWrapped()->lock($entity, $lockMode, $lockVersion); |
| 170 | + } |
| 171 | + |
| 172 | + public function getEventManager(): \Doctrine\Common\EventManager |
| 173 | + { |
| 174 | + return $this->getWrapped()->getEventManager(); |
| 175 | + } |
| 176 | + |
| 177 | + public function getConfiguration(): Configuration |
| 178 | + { |
| 179 | + return $this->getWrapped()->getConfiguration(); |
| 180 | + } |
| 181 | + |
| 182 | + public function isOpen(): bool |
| 183 | + { |
| 184 | + return $this->getWrapped()->isOpen(); |
| 185 | + } |
| 186 | + |
| 187 | + public function getUnitOfWork(): UnitOfWork |
| 188 | + { |
| 189 | + return $this->getWrapped()->getUnitOfWork(); |
| 190 | + } |
| 191 | + |
| 192 | + public function getHydrator($hydrationMode): AbstractHydrator |
| 193 | + { |
| 194 | + return $this->getWrapped()->getHydrator($hydrationMode); |
| 195 | + } |
| 196 | + |
| 197 | + public function newHydrator($hydrationMode): AbstractHydrator |
| 198 | + { |
| 199 | + return $this->getWrapped()->newHydrator($hydrationMode); |
| 200 | + } |
| 201 | + |
| 202 | + public function getProxyFactory(): ProxyFactory |
| 203 | + { |
| 204 | + return $this->getWrapped()->getProxyFactory(); |
| 205 | + } |
| 206 | + |
| 207 | + public function getFilters(): FilterCollection |
| 208 | + { |
| 209 | + return $this->getWrapped()->getFilters(); |
| 210 | + } |
| 211 | + |
| 212 | + public function isFiltersStateClean(): bool |
| 213 | + { |
| 214 | + return $this->getWrapped()->isFiltersStateClean(); |
| 215 | + } |
| 216 | + |
| 217 | + public function hasFilters(): bool |
| 218 | + { |
| 219 | + return $this->getWrapped()->hasFilters(); |
| 220 | + } |
| 221 | + |
| 222 | + public function getCache(): ?Cache |
| 223 | + { |
| 224 | + return $this->getWrapped()->getCache(); |
| 225 | + } |
| 226 | + |
| 227 | + public function find($className, $id): ?object |
| 228 | + { |
| 229 | + return $this->getWrapped()->find($className, $id); |
| 230 | + } |
| 231 | + |
| 232 | + public function persist(object $object): void |
| 233 | + { |
| 234 | + $this->getWrapped()->persist($object); |
| 235 | + } |
| 236 | + |
| 237 | + public function remove(object $object): void |
| 238 | + { |
| 239 | + $this->getWrapped()->remove($object); |
| 240 | + } |
| 241 | + |
| 242 | + public function clear(): void |
| 243 | + { |
| 244 | + $this->getWrapped()->clear(); |
| 245 | + } |
| 246 | + |
| 247 | + public function detach(object $object): void |
| 248 | + { |
| 249 | + $this->getWrapped()->detach($object); |
| 250 | + } |
| 251 | + |
| 252 | + public function refresh(object $object, ?int $lockMode = null): void |
| 253 | + { |
| 254 | + $this->getWrapped()->refresh($object, $lockMode); |
| 255 | + } |
| 256 | + |
| 257 | + public function flush(): void |
| 258 | + { |
| 259 | + $this->getWrapped()->flush(); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * @template T of object |
| 264 | + * |
| 265 | + * @param class-string<T> $className |
| 266 | + * |
| 267 | + * @return EntityRepository<T> |
| 268 | + */ |
| 269 | + public function getRepository($className): EntityRepository |
| 270 | + { |
| 271 | + return $this->getWrapped()->getRepository($className); |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * @template T of object |
| 276 | + * |
| 277 | + * @param class-string<T> $className |
| 278 | + * |
| 279 | + * @return ClassMetadata<T> |
| 280 | + */ |
| 281 | + public function getClassMetadata($className): ClassMetadata |
| 282 | + { |
| 283 | + return $this->getWrapped()->getClassMetadata($className); |
| 284 | + } |
| 285 | + |
| 286 | + public function getMetadataFactory(): ClassMetadataFactory |
| 287 | + { |
| 288 | + return $this->getWrapped()->getMetadataFactory(); |
| 289 | + } |
| 290 | + |
| 291 | + public function initializeObject(object $obj): void |
| 292 | + { |
| 293 | + $this->getWrapped()->initializeObject($obj); |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * @param mixed $value |
| 298 | + */ |
| 299 | + public function isUninitializedObject($value): bool |
| 300 | + { |
| 301 | + return $this->getWrapped()->isUninitializedObject($value); |
| 302 | + } |
| 303 | + |
| 304 | + public function contains(object $object): bool |
| 305 | + { |
| 306 | + return $this->getWrapped()->contains($object); |
| 307 | + } |
| 308 | +} |
0 commit comments