Skip to content

Commit

Permalink
Introduce proxy class name resolvers (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus authored Feb 17, 2021
1 parent a18b272 commit 12a05f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
};
}
}
12 changes: 12 additions & 0 deletions lib/Doctrine/Persistence/Mapping/ProxyClassNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Doctrine\Persistence\Mapping;

interface ProxyClassNameResolver
{
/**
* @psalm-param class-string $className
* @psalm-return class-string
*/
public function resolveClassName(string $className): string;
}

0 comments on commit 12a05f8

Please sign in to comment.