DefaultMarshaller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 $useIgbinarySerialize = true;
  20. private $throwOnSerializationFailure;
  21. public function __construct(?bool $useIgbinarySerialize = null, bool $throwOnSerializationFailure = false)
  22. {
  23. if (null === $useIgbinarySerialize) {
  24. $useIgbinarySerialize = \extension_loaded('igbinary') && (\PHP_VERSION_ID < 70400 || version_compare('3.1.6', phpversion('igbinary'), '<='));
  25. } elseif ($useIgbinarySerialize && (!\extension_loaded('igbinary') || (\PHP_VERSION_ID >= 70400 && version_compare('3.1.6', phpversion('igbinary'), '>')))) {
  26. 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.');
  27. }
  28. $this->useIgbinarySerialize = $useIgbinarySerialize;
  29. $this->throwOnSerializationFailure = $throwOnSerializationFailure;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function marshall(array $values, ?array &$failed): array
  35. {
  36. $serialized = $failed = [];
  37. foreach ($values as $id => $value) {
  38. try {
  39. if ($this->useIgbinarySerialize) {
  40. $serialized[$id] = igbinary_serialize($value);
  41. } else {
  42. $serialized[$id] = serialize($value);
  43. }
  44. } catch (\Exception $e) {
  45. if ($this->throwOnSerializationFailure) {
  46. throw new \ValueError($e->getMessage(), 0, $e);
  47. }
  48. $failed[] = $id;
  49. }
  50. }
  51. return $serialized;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function unmarshall(string $value)
  57. {
  58. if ('b:0;' === $value) {
  59. return false;
  60. }
  61. if ('N;' === $value) {
  62. return null;
  63. }
  64. static $igbinaryNull;
  65. if ($value === ($igbinaryNull ?? $igbinaryNull = \extension_loaded('igbinary') ? igbinary_serialize(null) : false)) {
  66. return null;
  67. }
  68. $unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
  69. try {
  70. if (':' === ($value[1] ?? ':')) {
  71. if (false !== $value = unserialize($value)) {
  72. return $value;
  73. }
  74. } elseif (false === $igbinaryNull) {
  75. throw new \RuntimeException('Failed to unserialize values, did you forget to install the "igbinary" extension?');
  76. } elseif (null !== $value = igbinary_unserialize($value)) {
  77. return $value;
  78. }
  79. throw new \DomainException(error_get_last() ? error_get_last()['message'] : 'Failed to unserialize values.');
  80. } catch (\Error $e) {
  81. throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR, $e->getFile(), $e->getLine());
  82. } finally {
  83. ini_set('unserialize_callback_func', $unserializeCallbackHandler);
  84. }
  85. }
  86. /**
  87. * @internal
  88. */
  89. public static function handleUnserializeCallback(string $class)
  90. {
  91. throw new \DomainException('Class not found: '.$class);
  92. }
  93. }