Skip to content

Commit 6105a36

Browse files
authored
fix: make proxy work with last symfony/var-exporter version (#664)
1 parent e8623a3 commit 6105a36

File tree

3 files changed

+2
-48
lines changed

3 files changed

+2
-48
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/deprecation-contracts": "^2.2|^3.0",
2323
"symfony/framework-bundle": "^6.4|^7.0",
2424
"symfony/property-access": "^6.4|^7.0",
25+
"symfony/var-exporter": "^6.4.9|~7.0.9|^7.1.2",
2526
"zenstruck/assert": "^1.4"
2627
},
2728
"require-dev": {

src/Persistence/ProxyGenerator.php

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -90,60 +90,17 @@ private static function generateClassFor(object $object): string
9090
$proxyCode,
9191
[
9292
'implements \Symfony\Component\VarExporter\LazyObjectInterface' => \sprintf('implements \%s, \Symfony\Component\VarExporter\LazyObjectInterface', Proxy::class),
93-
'use \Symfony\Component\VarExporter\LazyProxyTrait;' => \sprintf("use \\%s;\n use \\%s;", IsProxy::class, LazyProxyTrait::class),
93+
'use \Symfony\Component\VarExporter\LazyProxyTrait' => \sprintf("use \\%s;\n use \\%s", IsProxy::class, LazyProxyTrait::class),
9494
'if (isset($this->lazyObjectState)) {' => "\$this->_autoRefresh();\n\n if (isset(\$this->lazyObjectReal)) {",
9595
'\func_get_args()' => '$this->unproxyArgs(\func_get_args())',
9696
],
9797
);
9898

99-
$proxyCode = self::handleContravarianceInUnserializeMethod($reflectionClass, $proxyCode);
100-
10199
eval($proxyCode); // @phpstan-ignore-line
102100

103101
return $proxyClass;
104102
}
105103

106-
/**
107-
* @template T of object
108-
*
109-
* @param \ReflectionClass<T> $class
110-
*
111-
* Monkey patch for https://github.com/symfony/symfony/pull/57460, until the PR is released.
112-
*/
113-
private static function handleContravarianceInUnserializeMethod(\ReflectionClass $class, string $proxyCode): string
114-
{
115-
if (
116-
!str_contains($proxyCode, '__doUnserialize')
117-
&& $class->hasMethod('__unserialize')
118-
&& null !== ($unserializeParameter = $class->getMethod('__unserialize')->getParameters()[0] ?? null)
119-
&& null === $unserializeParameter->getType()
120-
) {
121-
$proxyCode = str_replace(
122-
'use \Symfony\Component\VarExporter\LazyProxyTrait;',
123-
<<<EOPHP
124-
use \Symfony\Component\VarExporter\LazyProxyTrait {
125-
__unserialize as private _doUnserialize;
126-
}
127-
EOPHP,
128-
$proxyCode
129-
);
130-
131-
$unserializeMethod = <<<EOPHP
132-
133-
public function __unserialize(\$data): void
134-
{
135-
\$this->_doUnserialize(\$data);
136-
}
137-
138-
EOPHP;
139-
140-
$lastCurlyBraceOffset = strrpos($proxyCode, '}') ?: throw new \LogicException('Last curly brace offset not found.');
141-
$proxyCode = substr_replace($proxyCode, $unserializeMethod."}", $lastCurlyBraceOffset, 1);
142-
}
143-
144-
return $proxyCode;
145-
}
146-
147104
/**
148105
* @param class-string $class
149106
*/

tests/Unit/Persistence/ProxyGeneratorTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ public function it_can_generate_proxy_for_class_with_unserialize_magic_method(ob
2929
{
3030
$proxyfiedObj = ProxyGenerator::wrap($obj);
3131
self::assertEquals(\unserialize(\serialize($proxyfiedObj))->_real(), $proxyfiedObj->_real());
32-
33-
// if this assertion fails, https://github.com/symfony/symfony/pull/57460 have been released
34-
// so, the monkey patch around contravariance problem could be removed
35-
self::assertFalse((new \ReflectionClass($proxyfiedObj))->hasMethod('__doUnserialize'));
3632
}
3733

3834
public static function classWithUnserializeMagicMethodProvider(): iterable

0 commit comments

Comments
 (0)