DefaultMarshaller.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Marshaller;
  11. use Symfony\Component\Cache\Exception\CacheException;
  12. /**
  13. * Serializes/unserializes values using igbinary_serialize() if available, serialize() otherwise.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class DefaultMarshaller implements MarshallerInterface
  18. {
  19. private bool $useIgbinarySerialize = false;
  20. private bool $throwOnSerializationFailure = false;
  21. public function __construct(?bool $useIgbinarySerialize = null, bool $throwOnSerializationFailure = false)
  22. {
  23. if ($useIgbinarySerialize && (!\extension_loaded('igbinary') || version_compare('3.1.6', phpversion('igbinary'), '>'))) {
  24. throw new CacheException(\extension_loaded('igbinary') ? 'Please upgrade the "igbinary" PHP extension to v3.1.6 or higher.' : 'The "igbinary" PHP extension is not loaded.');
  25. }
  26. $this->useIgbinarySerialize = true === $useIgbinarySerialize;
  27. $this->throwOnSerializationFailure = $throwOnSerializationFailure;
  28. }
  29. public function marshall(array $values, ?array &$failed): array
  30. {
  31. $serialized = $failed = [];
  32. foreach ($values as $id => $value) {
  33. try {
  34. if ($this->useIgbinarySerialize) {
  35. $serialized[$id] = igbinary_serialize($value);
  36. } else {
  37. $serialized[$id] = serialize($value);
  38. }
  39. } catch (\Exception $e) {
  40. if ($this->throwOnSerializationFailure) {
  41. throw new \ValueError($e->getMessage(), 0, $e);
  42. }
  43. $failed[] = $id;
  44. }
  45. }
  46. return $serialized;
  47. }
  48. public function unmarshall(string $value): mixed
  49. {
  50. if ('b:0;' === $value) {
  51. return false;
  52. }
  53. if ('N;' === $value) {
  54. return null;
  55. }
  56. static $igbinaryNull;
  57. if ($value === $igbinaryNull ??= \extension_loaded('igbinary') ? igbinary_serialize(null) : false) {
  58. return null;
  59. }
  60. $unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
  61. try {
  62. if (':' === ($value[1] ?? ':')) {
  63. if (false !== $value = unserialize($value)) {
  64. return $value;
  65. }
  66. } elseif (false === $igbinaryNull) {
  67. throw new \RuntimeException('Failed to unserialize values, did you forget to install the "igbinary" extension?');
  68. } elseif (null !== $value = igbinary_unserialize($value)) {
  69. return $value;
  70. }
  71. throw new \DomainException(error_get_last() ? error_get_last()['message'] : 'Failed to unserialize values.');
  72. } catch (\Error $e) {
  73. throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR, $e->getFile(), $e->getLine());
  74. } finally {
  75. ini_set('unserialize_callback_func', $unserializeCallbackHandler);
  76. }
  77. }
  78. /**
  79. * @internal
  80. */
  81. public static function handleUnserializeCallback(string $class): never
  82. {
  83. throw new \DomainException('Class not found: '.$class);
  84. }
  85. }