vendor/symfony/cache/Marshaller/DefaultMarshaller.php line 26

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