Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ProxyGeneratorInterface): Remove ProxySpecification. #7

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* refactor(`MainTransformer`): Move to implementation namespace.
* refactor: Move proxy creation code to `ProxyFactory`.
* feat(`SubMapperInterface`): Add `createProxy()`.
* refactor(`ProxyGeneratorInterface`): Remove `ProxySpecification`.

## 0.10.0

Expand Down
9 changes: 4 additions & 5 deletions src/Proxy/Implementation/DoctrineProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\Persistence\ManagerRegistry;
use Rekalogika\Mapper\Proxy\Exception\ProxyNotSupportedException;
use Rekalogika\Mapper\Proxy\ProxyGeneratorInterface;
use Rekalogika\Mapper\Proxy\ProxySpecification;

/**
* Prevent proxy creation for Doctrine entities.
Expand All @@ -31,14 +30,14 @@ public function __construct(
) {
}

public function generateProxy(string $class): ProxySpecification
public function generateProxyCode(string $realClass, string $proxyClass): string
{
$manager = $this->managerRegistry->getManagerForClass($class);
$manager = $this->managerRegistry->getManagerForClass($realClass);

if ($manager) {
throw new ProxyNotSupportedException($class, reason: 'Doctrine entities do not support proxying.');
throw new ProxyNotSupportedException($realClass, reason: 'Doctrine entities do not support proxying.');
}

return $this->decorated->generateProxy($class);
return $this->decorated->generateProxyCode($realClass, $proxyClass);
}
}
5 changes: 3 additions & 2 deletions src/Proxy/Implementation/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public function createProxy(
$targetProxyClass = ProxyNamer::generateProxyClassName($class);

if (!class_exists($targetProxyClass, false)) {
$proxySpecification = $this->proxyGenerator->generateProxy($class);
$this->proxyRegistry->registerProxy($proxySpecification);
$sourceCode = $this->proxyGenerator
->generateProxyCode($class, $targetProxyClass);
$this->proxyRegistry->registerProxy($targetProxyClass, $sourceCode);

if (!class_exists($targetProxyClass)) {
throw new LogicException(
Expand Down
24 changes: 10 additions & 14 deletions src/Proxy/Implementation/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

use Rekalogika\Mapper\Proxy\Exception\ProxyNotSupportedException;
use Rekalogika\Mapper\Proxy\ProxyGeneratorInterface;
use Rekalogika\Mapper\Proxy\ProxyNamer;
use Rekalogika\Mapper\Proxy\ProxySpecification;
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\ProxyHelper;

Expand All @@ -25,28 +23,26 @@
*/
final readonly class ProxyGenerator implements ProxyGeneratorInterface
{
public function generateProxy(string $class): ProxySpecification
{
/** @var class-string */
$proxyClass = ProxyNamer::generateProxyClassName($class);

public function generateProxyCode(
string $realClass,
string $proxyClass
): string {
try {
$proxyCode = $this->generateProxyCode($class);
$proxyCode = $this->generateProxySourceCode($realClass, $proxyClass);
} catch (LogicException $e) {
throw new ProxyNotSupportedException($class, previous: $e);
throw new ProxyNotSupportedException($realClass, previous: $e);
}

return new ProxySpecification($proxyClass, $proxyCode);
return $proxyCode;
}

/**
* @param class-string $class
* @param class-string $realClass
* @return string
*/
private function generateProxyCode(string $class): string
private function generateProxySourceCode(string $realClass, string $proxyClass): string
{
$proxyClass = ProxyNamer::generateProxyClassName($class);
$targetReflection = new \ReflectionClass($class);
$targetReflection = new \ReflectionClass($realClass);

// get proxy class name & namespace
$shortName = preg_replace('/.*\\\\/', '', $proxyClass);
Expand Down
12 changes: 4 additions & 8 deletions src/Proxy/Implementation/ProxyRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Rekalogika\Mapper\Proxy\ProxyAutoloaderInterface;
use Rekalogika\Mapper\Proxy\ProxyRegistryInterface;
use Rekalogika\Mapper\Proxy\ProxySpecification;

/**
* @internal
Expand All @@ -34,18 +33,15 @@ public function __construct(
}
}

public function registerProxy(
ProxySpecification $proxySpecification,
): void {
$proxyClass = $proxySpecification->getClass();

public function registerProxy(string $class, string $sourceCode): void
{
$proxyFile = sprintf(
'%s/%s',
$this->proxyDirectory,
self::getProxyFileName($proxyClass)
self::getProxyFileName($class)
);

file_put_contents($proxyFile, $proxySpecification->getCode());
file_put_contents($proxyFile, $sourceCode);
}

public function registerAutoloader(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Proxy/ProxyGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
interface ProxyGeneratorInterface
{
/**
* @param class-string $class
* @param class-string $realClass
* @throws ProxyNotSupportedException
*/
public function generateProxy(string $class): ProxySpecification;
public function generateProxyCode(string $realClass, string $proxyClass): string;
}
1 change: 0 additions & 1 deletion src/Proxy/ProxyNamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ProxyNamer
{
/**
* @param class-string $class
* @return string
*/
public static function generateProxyClassName(string $class): string
{
Expand Down
4 changes: 1 addition & 3 deletions src/Proxy/ProxyRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
*/
interface ProxyRegistryInterface
{
public function registerProxy(
ProxySpecification $proxySpecification,
): void;
public function registerProxy(string $class, string $sourceCode): void;
}
39 changes: 0 additions & 39 deletions src/Proxy/ProxySpecification.php

This file was deleted.

Loading