From 12a05f8d1ca070f491b25a82ba1b8e9448da9f21 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 17 Feb 2021 18:02:42 +0100 Subject: [PATCH] Introduce proxy class name resolvers (#145) --- .../Mapping/AbstractClassMetadataFactory.php | 32 ++++++++++++++++--- .../Mapping/ProxyClassNameResolver.php | 12 +++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 lib/Doctrine/Persistence/Mapping/ProxyClassNameResolver.php diff --git a/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php b/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php index 4a2a6ad3..20b254a9 100644 --- a/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php +++ b/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php @@ -58,6 +58,9 @@ abstract class AbstractClassMetadataFactory implements ClassMetadataFactory /** @var ReflectionService|null */ private $reflectionService = null; + /** @var ProxyClassNameResolver|null */ + private $proxyClassNameResolver = null; + /** * Sets the cache driver used by the factory to cache ClassMetadata instances. * @@ -140,6 +143,11 @@ public function getAllMetadata() return $metadata; } + public function setProxyClassNameResolver(ProxyClassNameResolver $resolver): void + { + $this->proxyClassNameResolver = $resolver; + } + /** * Lazy initialization of this stuff, especially the metadata driver, * since these are not needed at all when a metadata cache is active. @@ -467,12 +475,26 @@ protected function getCacheKey(string $realClassName): string */ private function getRealClass(string $class): string { - $pos = strrpos($class, '\\' . Proxy::MARKER . '\\'); - - if ($pos === false) { - return $class; + if ($this->proxyClassNameResolver === null) { + $this->createDefaultProxyClassNameResolver(); } - return substr($class, $pos + Proxy::MARKER_LENGTH + 2); + return $this->proxyClassNameResolver->resolveClassName($class); + } + + private function createDefaultProxyClassNameResolver(): void + { + $this->proxyClassNameResolver = new class implements ProxyClassNameResolver { + public function resolveClassName(string $className): string + { + $pos = strrpos($className, '\\' . Proxy::MARKER . '\\'); + + if ($pos === false) { + return $className; + } + + return substr($className, $pos + Proxy::MARKER_LENGTH + 2); + } + }; } } diff --git a/lib/Doctrine/Persistence/Mapping/ProxyClassNameResolver.php b/lib/Doctrine/Persistence/Mapping/ProxyClassNameResolver.php new file mode 100644 index 00000000..8df7a536 --- /dev/null +++ b/lib/Doctrine/Persistence/Mapping/ProxyClassNameResolver.php @@ -0,0 +1,12 @@ +