Skip to content

Commit 37e6102

Browse files
committed
[VarExporter] generate __doUnserialize() method in ProxyHelper::generateLazyProxy()
1 parent e3b80f3 commit 37e6102

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/Symfony/Component/VarExporter/ProxyHelper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,17 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf
200200
return <<<EOPHP
201201
{$parent} implements \\{$interfaces}
202202
{
203-
use \Symfony\Component\VarExporter\LazyProxyTrait;
203+
use \Symfony\Component\VarExporter\LazyProxyTrait {
204+
__unserialize as __doUnserialize;
205+
}
204206
205207
private const LAZY_OBJECT_PROPERTY_SCOPES = {$propertyScopes};
206-
{$body}}
208+
{$body}
209+
public function __unserialize(\$data): void
210+
{
211+
\$this->__doUnserialize(\$data);
212+
}
213+
}
207214
208215
// Help opcache.preload discover always-needed symbols
209216
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);

src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ public function testNormalization()
298298
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
299299
}
300300

301+
/**
302+
* @dataProvider classWithUnserializeMagicMethodProvider
303+
*/
304+
public function testItCanProxifyClassWithUnserializeMagicMethod(object $obj)
305+
{
306+
$proxy = $this->createLazyProxy($obj::class, fn () => $obj);
307+
self::assertIsObject($proxy);
308+
}
309+
310+
public static function classWithUnserializeMagicMethodProvider(): iterable
311+
{
312+
yield 'not type hinted __unserialize method' => [new class() {
313+
public function __unserialize($array)
314+
{
315+
}
316+
}];
317+
318+
yield 'type hinted __unserialize method' => [new class() {
319+
public function __unserialize(array $array)
320+
{
321+
}
322+
}];
323+
}
324+
301325
/**
302326
* @template T
303327
*

src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public function testGenerateLazyProxy()
6666
$expected = <<<'EOPHP'
6767
extends \Symfony\Component\VarExporter\Tests\TestForProxyHelper implements \Symfony\Component\VarExporter\LazyObjectInterface
6868
{
69-
use \Symfony\Component\VarExporter\LazyProxyTrait;
69+
use \Symfony\Component\VarExporter\LazyProxyTrait {
70+
__unserialize as __doUnserialize;
71+
}
7072
7173
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
7274
@@ -96,6 +98,11 @@ protected function foo7()
9698
9799
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelper::foo7()".');
98100
}
101+
102+
public function __unserialize($data): void
103+
{
104+
$this->__doUnserialize($data);
105+
}
99106
}
100107
101108
// Help opcache.preload discover always-needed symbols
@@ -113,7 +120,9 @@ public function testGenerateLazyProxyForInterfaces()
113120
$expected = <<<'EOPHP'
114121
implements \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1, \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2, \Symfony\Component\VarExporter\LazyObjectInterface
115122
{
116-
use \Symfony\Component\VarExporter\LazyProxyTrait;
123+
use \Symfony\Component\VarExporter\LazyProxyTrait {
124+
__unserialize as __doUnserialize;
125+
}
117126
118127
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
119128
@@ -148,6 +157,11 @@ public static function foo3(): string
148157
{
149158
throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2::foo3()".');
150159
}
160+
161+
public function __unserialize($data): void
162+
{
163+
$this->__doUnserialize($data);
164+
}
151165
}
152166
153167
// Help opcache.preload discover always-needed symbols
@@ -172,7 +186,6 @@ public function foo(#[\SensitiveParameter] $a): int
172186
173187
return parent::foo(...\func_get_args());
174188
}
175-
}
176189

177190
EOPHP;
178191

@@ -182,6 +195,7 @@ public function foo(#[\SensitiveParameter, AnotherAttribute] $a): int
182195
{
183196
}
184197
});
198+
185199
$this->assertStringContainsString($expected, ProxyHelper::generateLazyProxy($class));
186200
}
187201

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy