From 0d3ede7725ae07441999d9b4abdc94f48c89c792 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 23 Sep 2021 14:57:45 +0200 Subject: [PATCH] [Cache] Throw ValueError in debug mode when serialization fails --- .../Bundle/FrameworkBundle/Resources/config/cache.php | 1 + .../Component/Cache/Marshaller/DefaultMarshaller.php | 7 ++++++- .../Cache/Tests/Marshaller/DefaultMarshallerTest.php | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php index a15885003c70a..87e2db3f40197 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php @@ -217,6 +217,7 @@ ->set('cache.default_marshaller', DefaultMarshaller::class) ->args([ null, // use igbinary_serialize() when available + '%kernel.debug%', ]) ->set('cache.early_expiration_handler', EarlyExpirationHandler::class) diff --git a/src/Symfony/Component/Cache/Marshaller/DefaultMarshaller.php b/src/Symfony/Component/Cache/Marshaller/DefaultMarshaller.php index 7a02a962376ef..3202dd69cdab7 100644 --- a/src/Symfony/Component/Cache/Marshaller/DefaultMarshaller.php +++ b/src/Symfony/Component/Cache/Marshaller/DefaultMarshaller.php @@ -21,8 +21,9 @@ class DefaultMarshaller implements MarshallerInterface { private $useIgbinarySerialize = true; + private $throwOnSerializationFailure; - public function __construct(bool $useIgbinarySerialize = null) + public function __construct(bool $useIgbinarySerialize = null, bool $throwOnSerializationFailure = false) { if (null === $useIgbinarySerialize) { $useIgbinarySerialize = \extension_loaded('igbinary') && (\PHP_VERSION_ID < 70400 || version_compare('3.1.6', phpversion('igbinary'), '<=')); @@ -30,6 +31,7 @@ public function __construct(bool $useIgbinarySerialize = null) throw new CacheException(\extension_loaded('igbinary') && \PHP_VERSION_ID >= 70400 ? 'Please upgrade the "igbinary" PHP extension to v3.1.6 or higher.' : 'The "igbinary" PHP extension is not loaded.'); } $this->useIgbinarySerialize = $useIgbinarySerialize; + $this->throwOnSerializationFailure = $throwOnSerializationFailure; } /** @@ -47,6 +49,9 @@ public function marshall(array $values, ?array &$failed): array $serialized[$id] = serialize($value); } } catch (\Exception $e) { + if ($this->throwOnSerializationFailure) { + throw new \ValueError($e->getMessage(), 0, $e); + } $failed[] = $id; } } diff --git a/src/Symfony/Component/Cache/Tests/Marshaller/DefaultMarshallerTest.php b/src/Symfony/Component/Cache/Tests/Marshaller/DefaultMarshallerTest.php index 51ecc941b89c2..0217087843a15 100644 --- a/src/Symfony/Component/Cache/Tests/Marshaller/DefaultMarshallerTest.php +++ b/src/Symfony/Component/Cache/Tests/Marshaller/DefaultMarshallerTest.php @@ -109,4 +109,15 @@ public function testIgbinaryUnserializeInvalid() restore_error_handler(); } } + + public function testSerializeDebug() + { + $marshaller = new DefaultMarshaller(false, true); + $values = [ + 'a' => function () {}, + ]; + + $this->expectException(\ValueError::class); + $marshaller->marshall($values, $failed); + } }