Skip to content

Commit c68fce5

Browse files
[Cache] Throw ValueError in debug mode when serialization fails
1 parent 44042f6 commit c68fce5

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
->set('cache.default_marshaller', DefaultMarshaller::class)
218218
->args([
219219
null, // use igbinary_serialize() when available
220+
'%kernel.debug%',
220221
])
221222

222223
->set('cache.early_expiration_handler', EarlyExpirationHandler::class)

src/Symfony/Component/Cache/Marshaller/DefaultMarshaller.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121
class DefaultMarshaller implements MarshallerInterface
2222
{
2323
private $useIgbinarySerialize = true;
24+
private $debug;
2425

25-
public function __construct(bool $useIgbinarySerialize = null)
26+
public function __construct(bool $useIgbinarySerialize = null, bool $debug = false)
2627
{
2728
if (null === $useIgbinarySerialize) {
2829
$useIgbinarySerialize = \extension_loaded('igbinary') && (\PHP_VERSION_ID < 70400 || version_compare('3.1.6', phpversion('igbinary'), '<='));
2930
} elseif ($useIgbinarySerialize && (!\extension_loaded('igbinary') || (\PHP_VERSION_ID >= 70400 && version_compare('3.1.6', phpversion('igbinary'), '>')))) {
3031
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.');
3132
}
3233
$this->useIgbinarySerialize = $useIgbinarySerialize;
34+
$this->debug = $debug;
3335
}
3436

3537
/**
@@ -47,6 +49,9 @@ public function marshall(array $values, ?array &$failed): array
4749
$serialized[$id] = serialize($value);
4850
}
4951
} catch (\Exception $e) {
52+
if ($this->debug) {
53+
throw new \ValueError($e->getMessage(), 0, $e);
54+
}
5055
$failed[] = $id;
5156
}
5257
}

src/Symfony/Component/Cache/Tests/Marshaller/DefaultMarshallerTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,16 @@ public function testIgbinaryUnserializeInvalid()
109109
restore_error_handler();
110110
}
111111
}
112+
113+
public function testSerializeDebug()
114+
{
115+
$marshaller = new DefaultMarshaller(false, true);
116+
$values = [
117+
'a' => function () {},
118+
];
119+
120+
$this->expectException(\ValueError::class);
121+
$marshaller->marshall($values, $failed);
122+
}
123+
112124
}

0 commit comments

Comments
 (0)