|
| 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 | +use Symfony\Component\Cache\Marshaller\PhpMarshaller\Configurator; |
| 15 | +use Symfony\Component\Cache\Marshaller\PhpMarshaller\Reference; |
| 16 | +use Symfony\Component\Cache\Marshaller\PhpMarshaller\Registry; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Nicolas Grekas <p@tchwork.com> |
| 20 | + * |
| 21 | + * PhpMarshaller allows serializing PHP data structures using var_export() |
| 22 | + * while preserving all the semantics associated to serialize(). |
| 23 | + * |
| 24 | + * By leveraging OPcache, the generated PHP code is faster than doing the same with unserialize(). |
| 25 | + * |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +class PhpMarshaller |
| 29 | +{ |
| 30 | + public static function marshall($value, int &$objectsCount) |
| 31 | + { |
| 32 | + if (!\is_object($value) && !\is_array($value)) { |
| 33 | + return $value; |
| 34 | + } |
| 35 | + $objectsPool = new \SplObjectStorage(); |
| 36 | + $value = array($value); |
| 37 | + $objectsCount = self::doMarshall($value, $objectsPool); |
| 38 | + |
| 39 | + $classes = array(); |
| 40 | + $values = array(); |
| 41 | + $wakeups = array(); |
| 42 | + foreach ($objectsPool as $i => $v) { |
| 43 | + list(, $classes[], $values[], $wakeup) = $objectsPool[$v]; |
| 44 | + if ($wakeup) { |
| 45 | + $wakeups[$wakeup] = $i; |
| 46 | + } |
| 47 | + } |
| 48 | + ksort($wakeups); |
| 49 | + $properties = array(); |
| 50 | + foreach ($values as $i => $vars) { |
| 51 | + foreach ($vars as $class => $values) { |
| 52 | + foreach ($values as $name => $v) { |
| 53 | + $properties[$class][$name][$i] = $v; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + if (!$classes) { |
| 58 | + return $value[0]; |
| 59 | + } |
| 60 | + |
| 61 | + return new Configurator(new Registry($classes), $properties, $value[0], $wakeups); |
| 62 | + } |
| 63 | + |
| 64 | + public static function optimize(string $exportedValue) |
| 65 | + { |
| 66 | + return preg_replace(sprintf("{%s::__set_state\(array\(\s++'0' => (\d+),\s++\)\)}", preg_quote(Reference::class)), Registry::class.'::$objects[$1]', $exportedValue); |
| 67 | + } |
| 68 | + |
| 69 | + private static function doMarshall(array &$array, \SplObjectStorage $objectsPool): int |
| 70 | + { |
| 71 | + $objectsCount = 0; |
| 72 | + |
| 73 | + foreach ($array as &$value) { |
| 74 | + if (\is_array($value) && $value) { |
| 75 | + $objectsCount += self::doMarshall($value, $objectsPool); |
| 76 | + } |
| 77 | + if (!\is_object($value)) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (isset($objectsPool[$value])) { |
| 81 | + ++$objectsCount; |
| 82 | + $value = new Reference($objectsPool[$value][0]); |
| 83 | + continue; |
| 84 | + } |
| 85 | + $class = \get_class($value); |
| 86 | + $properties = array(); |
| 87 | + $sleep = null; |
| 88 | + $arrayValue = (array) $value; |
| 89 | + $proto = (Registry::$reflectors[$class] ?? Registry::getClassReflector($class))->newInstanceWithoutConstructor(); |
| 90 | + |
| 91 | + if ($value instanceof \ArrayIterator || $value instanceof \ArrayObject) { |
| 92 | + // ArrayIterator and ArrayObject need special care because their "flags" |
| 93 | + // option changes the behavior of the (array) casting operator. |
| 94 | + $reflector = $value instanceof \ArrayIterator ? 'ArrayIterator' : 'ArrayObject'; |
| 95 | + $reflector = Registry::$reflectors[$reflector] ?? Registry::getClassReflector($reflector); |
| 96 | + |
| 97 | + $properties = array( |
| 98 | + $arrayValue, |
| 99 | + $reflector->getMethod('getFlags')->invoke($value), |
| 100 | + $value instanceof \ArrayObject ? $reflector->getMethod('getIteratorClass')->invoke($value) : 'ArrayIterator', |
| 101 | + ); |
| 102 | + |
| 103 | + $reflector = $reflector->getMethod('setFlags'); |
| 104 | + $reflector->invoke($proto, \ArrayObject::STD_PROP_LIST); |
| 105 | + |
| 106 | + if ($properties[1] & \ArrayObject::STD_PROP_LIST) { |
| 107 | + $reflector->invoke($value, 0); |
| 108 | + $properties[0] = (array) $value; |
| 109 | + } else { |
| 110 | + $reflector->invoke($value, \ArrayObject::STD_PROP_LIST); |
| 111 | + $arrayValue = (array) $value; |
| 112 | + } |
| 113 | + $reflector->invoke($value, $properties[1]); |
| 114 | + |
| 115 | + if (array(array(), 0, 'ArrayIterator') === $properties) { |
| 116 | + $properties = array(); |
| 117 | + } else { |
| 118 | + if ('ArrayIterator' === $properties[2]) { |
| 119 | + unset($properties[2]); |
| 120 | + } |
| 121 | + $properties = array($reflector->class => array("\0" => $properties)); |
| 122 | + } |
| 123 | + } elseif ($value instanceof \SplObjectStorage) { |
| 124 | + foreach (clone $value as $v) { |
| 125 | + $properties[] = $v; |
| 126 | + $properties[] = $value[$v]; |
| 127 | + } |
| 128 | + $properties = array('SplObjectStorage' => array("\0" => $properties)); |
| 129 | + } elseif ($value instanceof \Serializable) { |
| 130 | + ++$objectsCount; |
| 131 | + $objectsPool[$value] = array($id = \count($objectsPool), serialize($value), array(), 0); |
| 132 | + $value = new Reference($id); |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + if (\method_exists($class, '__sleep')) { |
| 137 | + if (!\is_array($sleep = $value->__sleep())) { |
| 138 | + trigger_error('serialize(): __sleep should return an array only containing the names of instance-variables to serialize', E_USER_NOTICE); |
| 139 | + $value = null; |
| 140 | + continue; |
| 141 | + } |
| 142 | + $sleep = array_flip($sleep); |
| 143 | + } |
| 144 | + |
| 145 | + $proto = (array) $proto; |
| 146 | + |
| 147 | + foreach ($arrayValue as $name => $v) { |
| 148 | + $k = (string) $name; |
| 149 | + if ('' === $k || "\0" !== $k[0]) { |
| 150 | + $c = $class; |
| 151 | + } elseif ('*' === $k[1]) { |
| 152 | + $c = $class; |
| 153 | + $k = substr($k, 3); |
| 154 | + } else { |
| 155 | + $i = strpos($k, "\0", 2); |
| 156 | + $c = substr($k, 1, $i - 1); |
| 157 | + $k = substr($k, 1 + $i); |
| 158 | + } |
| 159 | + if (null === $sleep) { |
| 160 | + $properties[$c][$k] = $v; |
| 161 | + } elseif (isset($sleep[$k]) && $c === $class) { |
| 162 | + $properties[$c][$k] = $v; |
| 163 | + unset($sleep[$k]); |
| 164 | + } |
| 165 | + if (\array_key_exists($name, $proto) && $proto[$name] === $v) { |
| 166 | + unset($properties[$c][$k]); |
| 167 | + } |
| 168 | + } |
| 169 | + if ($sleep) { |
| 170 | + foreach ($sleep as $k => $v) { |
| 171 | + trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $k), E_USER_NOTICE); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + $objectsPool[$value] = array($id = \count($objectsPool)); |
| 176 | + $objectsCount += 1 + self::doMarshall($properties, $objectsPool); |
| 177 | + $objectsPool[$value] = array($id, $class, $properties, \method_exists($class, '__wakeup') ? $objectsCount : 0); |
| 178 | + |
| 179 | + $value = new Reference($id); |
| 180 | + } |
| 181 | + |
| 182 | + return $objectsCount; |
| 183 | + } |
| 184 | +} |
0 commit comments