diff --git a/src/Symfony/Component/VarExporter/Internal/LazyObjectTrait.php b/src/Symfony/Component/VarExporter/Internal/LazyObjectTrait.php index cccdf6cffdb2..4a6f232af85a 100644 --- a/src/Symfony/Component/VarExporter/Internal/LazyObjectTrait.php +++ b/src/Symfony/Component/VarExporter/Internal/LazyObjectTrait.php @@ -11,12 +11,15 @@ namespace Symfony\Component\VarExporter\Internal; +use Symfony\Component\Serializer\Attribute\Ignore; + if (\PHP_VERSION_ID >= 80300) { /** * @internal */ trait LazyObjectTrait { + #[Ignore] private readonly LazyObjectState $lazyObjectState; } } else { @@ -25,6 +28,7 @@ trait LazyObjectTrait */ trait LazyObjectTrait { + #[Ignore] private LazyObjectState $lazyObjectState; } } diff --git a/src/Symfony/Component/VarExporter/LazyGhostTrait.php b/src/Symfony/Component/VarExporter/LazyGhostTrait.php index ab8a8e87b47f..5191b59e705f 100644 --- a/src/Symfony/Component/VarExporter/LazyGhostTrait.php +++ b/src/Symfony/Component/VarExporter/LazyGhostTrait.php @@ -11,6 +11,7 @@ namespace Symfony\Component\VarExporter; +use Symfony\Component\Serializer\Attribute\Ignore; use Symfony\Component\VarExporter\Internal\Hydrator; use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry; use Symfony\Component\VarExporter\Internal\LazyObjectState; @@ -61,6 +62,7 @@ public static function createLazyGhost(\Closure|array $initializer, ?array $skip * * @param $partial Whether partially initialized objects should be considered as initialized */ + #[Ignore] public function isLazyObjectInitialized(bool $partial = false): bool { if (!$state = $this->lazyObjectState ?? null) { @@ -389,6 +391,7 @@ public function __destruct() } } + #[Ignore] private function setLazyObjectAsInitialized(bool $initialized): void { $state = $this->lazyObjectState ?? null; diff --git a/src/Symfony/Component/VarExporter/LazyProxyTrait.php b/src/Symfony/Component/VarExporter/LazyProxyTrait.php index 4dd435bcda63..2033670522ab 100644 --- a/src/Symfony/Component/VarExporter/LazyProxyTrait.php +++ b/src/Symfony/Component/VarExporter/LazyProxyTrait.php @@ -11,6 +11,7 @@ namespace Symfony\Component\VarExporter; +use Symfony\Component\Serializer\Attribute\Ignore; use Symfony\Component\VarExporter\Hydrator as PublicHydrator; use Symfony\Component\VarExporter\Internal\Hydrator; use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry; @@ -50,6 +51,7 @@ public static function createLazyProxy(\Closure $initializer, ?object $instance * * @param $partial Whether partially initialized objects should be considered as initialized */ + #[Ignore] public function isLazyObjectInitialized(bool $partial = false): bool { return !isset($this->lazyObjectState) || isset($this->lazyObjectState->realInstance) || Registry::$noInitializerState === $this->lazyObjectState->initializer; diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/SimpleObject.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/SimpleObject.php new file mode 100644 index 000000000000..9187f652fde4 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/SimpleObject.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarExporter\Tests\Fixtures; + +class SimpleObject +{ + public function getMethod(): string + { + return 'method'; + } + + public string $property = 'property'; +} diff --git a/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php b/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php index be554b953287..68e76a7dac1f 100644 --- a/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php +++ b/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php @@ -12,6 +12,9 @@ namespace Symfony\Component\VarExporter\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; +use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\VarExporter\Internal\LazyObjectState; use Symfony\Component\VarExporter\ProxyHelper; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass; @@ -22,6 +25,7 @@ use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass; +use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject; class LazyGhostTraitTest extends TestCase { @@ -461,6 +465,19 @@ public function testAccessingUninializedPropertyWithLazyGhost() $object->property; } + public function testNormalization() + { + $object = $this->createLazyGhost(SimpleObject::class, function ($instance) {}); + + $loader = new AttributeLoader(); + $metadataFactory = new ClassMetadataFactory($loader); + $serializer = new ObjectNormalizer($metadataFactory); + + $output = $serializer->normalize($object); + + $this->assertSame(['property' => 'property', 'method' => 'method'], $output); + } + /** * @template T * diff --git a/src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php b/src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php index 887527528fd7..c4234d085b6d 100644 --- a/src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php +++ b/src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php @@ -12,6 +12,9 @@ namespace Symfony\Component\VarExporter\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; +use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\VarExporter\Exception\LogicException; use Symfony\Component\VarExporter\LazyProxyTrait; use Symfony\Component\VarExporter\ProxyHelper; @@ -22,6 +25,7 @@ use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestOverwritePropClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestUnserializeClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass; +use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject; class LazyProxyTraitTest extends TestCase { @@ -281,6 +285,19 @@ public function __construct() $this->assertSame(['foo' => 123], (array) $obj->getDep()); } + public function testNormalization() + { + $object = $this->createLazyProxy(SimpleObject::class, fn () => new SimpleObject()); + + $loader = new AttributeLoader(); + $metadataFactory = new ClassMetadataFactory($loader); + $serializer = new ObjectNormalizer($metadataFactory); + + $output = $serializer->normalize($object); + + $this->assertSame(['property' => 'property', 'method' => 'method'], $output); + } + /** * @template T *