DefaultMarshaller.php 3.4 KB

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