Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Cache] make PhpMarshaller handle hard references #28188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\GetTrait;
Expand All @@ -35,7 +34,6 @@ class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInte
use GetTrait;

private $createCacheItem;
private $marshaller;

/**
* @param string $file The PHP file were values are cached
Expand Down Expand Up @@ -106,9 +104,6 @@ public function get(string $key, callable $callback, float $beta = null)
if ($value instanceof \Closure) {
return $value();
}
if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
return ($this->marshaller ?? $this->marshaller = new DefaultMarshaller())->unmarshall($value);
}
} catch (\Throwable $e) {
unset($this->keys[$key]);
goto get_from_pool;
Expand Down Expand Up @@ -144,13 +139,6 @@ public function getItem($key)
$value = null;
$isHit = false;
}
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
try {
$value = unserialize($value);
} catch (\Throwable $e) {
$value = null;
$isHit = false;
}
}

$f = $this->createCacheItem;
Expand Down Expand Up @@ -284,12 +272,6 @@ private function generateItems(array $keys): \Generator
} catch (\Throwable $e) {
yield $key => $f($key, null, false);
}
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
try {
yield $key => $f($key, $this->unserializeValue($value), true);
} catch (\Throwable $e) {
yield $key => $f($key, null, false);
}
} else {
yield $key => $f($key, $value, true);
}
Expand Down
159 changes: 31 additions & 128 deletions src/Symfony/Component/Cache/Marshaller/PhpMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
namespace Symfony\Component\Cache\Marshaller;

use Symfony\Component\Cache\Marshaller\PhpMarshaller\Configurator;
use Symfony\Component\Cache\Marshaller\PhpMarshaller\Marshaller;
use Symfony\Component\Cache\Marshaller\PhpMarshaller\Reference;
use Symfony\Component\Cache\Marshaller\PhpMarshaller\Registry;
use Symfony\Component\Cache\Marshaller\PhpMarshaller\Values;

/**
* @author Nicolas Grekas <[email protected]>
Expand All @@ -27,14 +29,31 @@
*/
class PhpMarshaller
{
public static function marshall($value, int &$objectsCount)
public static function marshall($value, bool &$isStaticValue = null): string
{
if (!\is_object($value) && !\is_array($value)) {
return $value;
$isStaticValue = true;

if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value)) {
return var_export($value, true);
}

$objectsPool = new \SplObjectStorage();
$value = array($value);
$objectsCount = self::doMarshall($value, $objectsPool);
$refsPool = array();
$objectsCount = 0;

try {
$value = Marshaller::marshall(array($value), $objectsPool, $refsPool, $objectsCount, $isStaticValue)[0];
} finally {
$references = array();
foreach ($refsPool as $i => $v) {
$v[0] = $v[1];
$references[1 + $i] = $v[2];
}
}

if ($isStaticValue) {
return var_export($value, true);
}

$classes = array();
$values = array();
Expand All @@ -46,6 +65,7 @@ public static function marshall($value, int &$objectsCount)
}
}
ksort($wakeups);

$properties = array();
foreach ($values as $i => $vars) {
foreach ($vars as $class => $values) {
Expand All @@ -54,131 +74,14 @@ public static function marshall($value, int &$objectsCount)
}
}
}
if (!$classes) {
return $value[0];
}

return new Configurator(new Registry($classes), $properties, $value[0], $wakeups);
}

public static function optimize(string $exportedValue)
{
return preg_replace(sprintf("{%s::__set_state\(array\(\s++'0' => (\d+),\s++\)\)}", preg_quote(Reference::class)), Registry::class.'::$objects[$1]', $exportedValue);
}

private static function doMarshall(array &$array, \SplObjectStorage $objectsPool): int
{
$objectsCount = 0;

foreach ($array as &$value) {
if (\is_array($value) && $value) {
$objectsCount += self::doMarshall($value, $objectsPool);
}
if (!\is_object($value)) {
continue;
}
if (isset($objectsPool[$value])) {
++$objectsCount;
$value = new Reference($objectsPool[$value][0]);
continue;
}
$class = \get_class($value);
$properties = array();
$sleep = null;
$arrayValue = (array) $value;
$proto = (Registry::$reflectors[$class] ?? Registry::getClassReflector($class))->newInstanceWithoutConstructor();

if ($value instanceof \ArrayIterator || $value instanceof \ArrayObject) {
// ArrayIterator and ArrayObject need special care because their "flags"
// option changes the behavior of the (array) casting operator.
$reflector = $value instanceof \ArrayIterator ? 'ArrayIterator' : 'ArrayObject';
$reflector = Registry::$reflectors[$reflector] ?? Registry::getClassReflector($reflector);
$value = new Configurator($classes ? new Registry($classes) : null, $references ? new Values($references) : null, $properties, $value, $wakeups);
$value = var_export($value, true);

$properties = array(
$arrayValue,
$reflector->getMethod('getFlags')->invoke($value),
$value instanceof \ArrayObject ? $reflector->getMethod('getIteratorClass')->invoke($value) : 'ArrayIterator',
);

$reflector = $reflector->getMethod('setFlags');
$reflector->invoke($proto, \ArrayObject::STD_PROP_LIST);

if ($properties[1] & \ArrayObject::STD_PROP_LIST) {
$reflector->invoke($value, 0);
$properties[0] = (array) $value;
} else {
$reflector->invoke($value, \ArrayObject::STD_PROP_LIST);
$arrayValue = (array) $value;
}
$reflector->invoke($value, $properties[1]);

if (array(array(), 0, 'ArrayIterator') === $properties) {
$properties = array();
} else {
if ('ArrayIterator' === $properties[2]) {
unset($properties[2]);
}
$properties = array($reflector->class => array("\0" => $properties));
}
} elseif ($value instanceof \SplObjectStorage) {
foreach (clone $value as $v) {
$properties[] = $v;
$properties[] = $value[$v];
}
$properties = array('SplObjectStorage' => array("\0" => $properties));
} elseif ($value instanceof \Serializable) {
++$objectsCount;
$objectsPool[$value] = array($id = \count($objectsPool), serialize($value), array(), 0);
$value = new Reference($id);
continue;
}

if (\method_exists($class, '__sleep')) {
if (!\is_array($sleep = $value->__sleep())) {
trigger_error('serialize(): __sleep should return an array only containing the names of instance-variables to serialize', E_USER_NOTICE);
$value = null;
continue;
}
$sleep = array_flip($sleep);
}

$proto = (array) $proto;

foreach ($arrayValue as $name => $v) {
$k = (string) $name;
if ('' === $k || "\0" !== $k[0]) {
$c = $class;
} elseif ('*' === $k[1]) {
$c = $class;
$k = substr($k, 3);
} else {
$i = strpos($k, "\0", 2);
$c = substr($k, 1, $i - 1);
$k = substr($k, 1 + $i);
}
if (null === $sleep) {
$properties[$c][$k] = $v;
} elseif (isset($sleep[$k]) && $c === $class) {
$properties[$c][$k] = $v;
unset($sleep[$k]);
}
if (\array_key_exists($name, $proto) && $proto[$name] === $v) {
unset($properties[$c][$k]);
}
}
if ($sleep) {
foreach ($sleep as $k => $v) {
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $k), E_USER_NOTICE);
}
}

$objectsPool[$value] = array($id = \count($objectsPool));
$objectsCount += 1 + self::doMarshall($properties, $objectsPool);
$objectsPool[$value] = array($id, $class, $properties, \method_exists($class, '__wakeup') ? $objectsCount : 0);

$value = new Reference($id);
}
$regexp = sprintf("{%s::__set_state\(array\(\s++'id' => %%s(\d+),\s++\)\)}", preg_quote(Reference::class));
$value = preg_replace(sprintf($regexp, ''), Registry::class.'::$objects[$1]', $value);
$value = preg_replace(sprintf($regexp, '-'), '&'.Registry::class.'::$references[$1]', $value);

return $objectsCount;
return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ class Configurator
{
public static $configurators = array();

public function __construct(Registry $registry, array $properties, $value, array $wakeups)
public function __construct(?Registry $registry, ?Values $values, array $properties, $value, array $wakeups)
{
$this->{0} = $registry;
$this->{1} = $properties;
$this->{2} = $value;
$this->{3} = $wakeups;
$this->{1} = $values;
$this->{2} = $properties;
$this->{3} = $value;
$this->{4} = $wakeups;
}

public static function __set_state($state)
{
$objects = Registry::$objects;
Registry::$objects = \array_pop(Registry::$stack);
list(, $properties, $value, $wakeups) = $state;
list(Registry::$objects, Registry::$references) = \array_pop(Registry::$stack);
list(, , $properties, $value, $wakeups) = $state;

foreach ($properties as $class => $vars) {
(self::$configurators[$class] ?? self::getConfigurator($class))($vars, $objects);
Expand Down
Loading