|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Marshaller; |
| 13 | + |
| 14 | +/** |
| 15 | + * A marshaller optimized for data structures generated by AbstractTagAwareAdapter. |
| 16 | + * |
| 17 | + * @author Nicolas Grekas <p@tchwork.com> |
| 18 | + */ |
| 19 | +class TagAwareMarshaller implements MarshallerInterface |
| 20 | +{ |
| 21 | + private $marshaller; |
| 22 | + |
| 23 | + public function __construct(MarshallerInterface $marshaller = null) |
| 24 | + { |
| 25 | + $this->marshaller = $marshaller ?? new DefaultMarshaller(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + public function marshall(array $values, ?array &$failed): array |
| 32 | + { |
| 33 | + $failed = $notSerialized = $serialized = []; |
| 34 | + |
| 35 | + foreach ($values as $id => $value) { |
| 36 | + if (\is_array($value) && \is_array($value['tags'] ?? null) && \array_key_exists('value', $value) && \count($value) === 2 + (\is_string($value['meta'] ?? null) && 8 === \strlen($value['meta']))) { |
| 37 | + // if the value is an array with keys "tags", "value" and "meta", use a compact serialization format |
| 38 | + // magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F allow detecting this format quickly in unmarshall() |
| 39 | + |
| 40 | + $v = $this->marshaller->marshall($value, $f); |
| 41 | + |
| 42 | + if ($f) { |
| 43 | + $f = []; |
| 44 | + } else { |
| 45 | + if ([] === $value['tags']) { |
| 46 | + $v['tags'] = ''; |
| 47 | + } |
| 48 | + |
| 49 | + $serialized[$id] = "\x9D".($value['meta'] ?? "\0\0\0\0\0\0\0\0").pack('N', \strlen($v['tags'])).$v['tags'].$v['value']; |
| 50 | + $serialized[$id][9] = "\x5F"; |
| 51 | + continue; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // other arbitratry values are serialized using the decorated marshaller below |
| 56 | + $notSerialized[$id] = $value; |
| 57 | + } |
| 58 | + |
| 59 | + return $notSerialized ? $serialized + $this->marshaller->marshall($notSerialized, $failed) : $serialized; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritdoc} |
| 64 | + */ |
| 65 | + public function unmarshall(string $value) |
| 66 | + { |
| 67 | + // detect the compact format used in marshall() using magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F |
| 68 | + if (13 >= \strlen($value) || "\x9D" !== $value[0] || "\0" !== $value[5] || "\x5F" !== $value[9]) { |
| 69 | + return $this->marshaller->unmarshall($value); |
| 70 | + } |
| 71 | + |
| 72 | + // data consists of value, tags and metadata which we need to unpack |
| 73 | + $meta = substr($value, 1, 12); |
| 74 | + $meta[8] = "\0"; |
| 75 | + $tagLen = unpack('Nlen', $meta, 8)['len']; |
| 76 | + $meta = substr($meta, 0, 8); |
| 77 | + |
| 78 | + return [ |
| 79 | + 'value' => $this->marshaller->unmarshall(substr($value, 13 + $tagLen)), |
| 80 | + 'tags' => $tagLen ? $this->marshaller->unmarshall(substr($value, 13, $tagLen)) : [], |
| 81 | + 'meta' => "\0\0\0\0\0\0\0\0" === $meta ? null : $meta, |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments