N°8910 - Upgrade Symfony packages

This commit is contained in:
Benjamin Dalsass
2026-02-19 09:18:56 +01:00
committed by GitHub
parent d5706fcbef
commit d2f67dcb3c
126 changed files with 1107 additions and 2067 deletions

View File

@@ -76,32 +76,23 @@ class Exporter
$class = $value::class;
$reflector = Registry::$reflectors[$class] ??= Registry::getClassReflector($class);
$properties = [];
$sleep = null;
$proto = Registry::$prototypes[$class];
if ($reflector->hasMethod('__serialize')) {
if (!$reflector->getMethod('__serialize')->isPublic()) {
throw new \Error(sprintf('Call to %s method "%s::__serialize()".', $reflector->getMethod('__serialize')->isProtected() ? 'protected' : 'private', $class));
throw new \Error(\sprintf('Call to %s method "%s::__serialize()".', $reflector->getMethod('__serialize')->isProtected() ? 'protected' : 'private', $class));
}
if (!\is_array($serializeProperties = $value->__serialize())) {
if (!\is_array($arrayValue = $value->__serialize())) {
throw new \TypeError($class.'::__serialize() must return an array');
}
if ($reflector->hasMethod('__unserialize')) {
$properties = $serializeProperties;
} else {
foreach ($serializeProperties as $n => $v) {
$c = $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
$properties[$c][$n] = $v;
}
$properties = $arrayValue;
goto prepare_value;
}
goto prepare_value;
}
$sleep = null;
$proto = Registry::$prototypes[$class];
if (($value instanceof \ArrayIterator || $value instanceof \ArrayObject) && null !== $proto) {
} elseif (($value instanceof \ArrayIterator || $value instanceof \ArrayObject) && null !== $proto) {
// ArrayIterator and ArrayObject need special care because their "flags"
// option changes the behavior of the (array) casting operator.
[$arrayValue, $properties] = self::getArrayObjectProperties($value, $proto);
@@ -117,10 +108,7 @@ class Exporter
}
$properties = ['SplObjectStorage' => ["\0" => $properties]];
$arrayValue = (array) $value;
} elseif ($value instanceof \Serializable
|| $value instanceof \__PHP_Incomplete_Class
|| \PHP_VERSION_ID < 80200 && $value instanceof \DatePeriod
) {
} elseif ($value instanceof \Serializable || $value instanceof \__PHP_Incomplete_Class || \PHP_VERSION_ID < 80200 && $value instanceof \DatePeriod) {
++$objectsCount;
$objectsPool[$value] = [$id = \count($objectsPool), serialize($value), [], 0];
$value = new Reference($id);
@@ -144,15 +132,15 @@ class Exporter
$i = 0;
$n = (string) $name;
if ('' === $n || "\0" !== $n[0]) {
$c = $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
$parent = $reflector;
do {
$p = $parent->hasProperty($n) ? $parent->getProperty($n) : null;
} while (!$p && $parent = $parent->getParentClass());
$c = $p && (!$p->isPublic() || (\PHP_VERSION_ID >= 80400 ? $p->isProtectedSet() || $p->isPrivateSet() : $p->isReadOnly())) ? $p->class : 'stdClass';
} elseif ('*' === $n[1]) {
$n = substr($n, 3);
$c = $reflector->getProperty($n)->class;
if ('Error' === $c) {
$c = 'TypeError';
} elseif ('Exception' === $c) {
$c = 'ErrorException';
}
} else {
$i = strpos($n, "\0", 2);
$c = substr($n, 1, $i - 1);
@@ -165,13 +153,19 @@ class Exporter
}
unset($sleep[$name], $sleep[$n]);
}
if (!\array_key_exists($name, $proto) || $proto[$name] !== $v || "\x00Error\x00trace" === $name || "\x00Exception\x00trace" === $name) {
if ("\x00Error\x00trace" === $name || "\x00Exception\x00trace" === $name) {
$properties[$c][$n] = $v;
} elseif (!\array_key_exists($name, $proto) || $proto[$name] !== $v) {
$properties[match ($c) {
'Error' => 'TypeError',
'Exception' => 'ErrorException',
default => $c,
}][$n] = $v;
}
}
if ($sleep) {
foreach ($sleep as $n => $v) {
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE);
trigger_error(\sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE);
}
}
if (method_exists($class, '__unserialize')) {
@@ -224,10 +218,10 @@ class Exporter
$subIndent = $indent.' ';
if (\is_string($value)) {
$code = sprintf("'%s'", addcslashes($value, "'\\"));
$code = \sprintf("'%s'", addcslashes($value, "'\\"));
$code = preg_replace_callback("/((?:[\\0\\r\\n]|\u{202A}|\u{202B}|\u{202D}|\u{202E}|\u{2066}|\u{2067}|\u{2068}|\u{202C}|\u{2069})++)(.)/", function ($m) use ($subIndent) {
$m[1] = sprintf('\'."%s".\'', str_replace(
$m[1] = \sprintf('\'."%s".\'', str_replace(
["\0", "\r", "\n", "\u{202A}", "\u{202B}", "\u{202D}", "\u{202E}", "\u{2066}", "\u{2067}", "\u{2068}", "\u{202C}", "\u{2069}", '\n\\'],
['\0', '\r', '\n', '\u{202A}', '\u{202B}', '\u{202D}', '\u{202E}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{202C}', '\u{2069}', '\n"'."\n".$subIndent.'."\\'],
$m[1]
@@ -285,7 +279,7 @@ class Exporter
return self::exportHydrator($value, $indent, $subIndent);
}
throw new \UnexpectedValueException(sprintf('Cannot export value of type "%s".', get_debug_type($value)));
throw new \UnexpectedValueException(\sprintf('Cannot export value of type "%s".', get_debug_type($value)));
}
private static function exportRegistry(Registry $value, string $indent, string $subIndent): string

View File

@@ -20,6 +20,9 @@ use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
*/
class Hydrator
{
public const PROPERTY_HAS_HOOKS = 1;
public const PROPERTY_NOT_BY_REF = 2;
public static array $hydrators = [];
public static array $simpleHydrators = [];
public static array $propertyScopes = [];
@@ -70,11 +73,11 @@ class Hydrator
return $baseHydrator;
case 'ErrorException':
return $baseHydrator->bindTo(null, new class() extends \ErrorException {
return $baseHydrator->bindTo(null, new class extends \ErrorException {
});
case 'TypeError':
return $baseHydrator->bindTo(null, new class() extends \Error {
return $baseHydrator->bindTo(null, new class extends \Error {
});
case 'SplObjectStorage':
@@ -83,7 +86,7 @@ class Hydrator
if ("\0" === $name) {
foreach ($values as $i => $v) {
for ($j = 0; $j < \count($v); ++$j) {
$objects[$i]->attach($v[$j], $v[++$j]);
$objects[$i][$v[$j]] = $v[++$j];
}
}
continue;
@@ -156,13 +159,16 @@ class Hydrator
public static function getSimpleHydrator($class)
{
$baseHydrator = self::$simpleHydrators['stdClass'] ??= (function ($properties, $object) {
$readonly = (array) $this;
$notByRef = (array) $this;
foreach ($properties as $name => &$value) {
$object->$name = $value;
if (!($readonly[$name] ?? false)) {
if (!$noRef = $notByRef[$name] ?? false) {
$object->$name = $value;
$object->$name = &$value;
} elseif (true !== $noRef) {
$noRef($object, $value);
} else {
$object->$name = $value;
}
}
})->bindTo(new \stdClass());
@@ -172,11 +178,11 @@ class Hydrator
return $baseHydrator;
case 'ErrorException':
return $baseHydrator->bindTo(new \stdClass(), new class() extends \ErrorException {
return $baseHydrator->bindTo(new \stdClass(), new class extends \ErrorException {
});
case 'TypeError':
return $baseHydrator->bindTo(new \stdClass(), new class() extends \Error {
return $baseHydrator->bindTo(new \stdClass(), new class extends \Error {
});
case 'SplObjectStorage':
@@ -188,7 +194,7 @@ class Hydrator
continue;
}
for ($i = 0; $i < \count($value); ++$i) {
$object->attach($value[$i], $value[++$i]);
$object[$value[$i]] = $value[++$i];
}
}
};
@@ -217,14 +223,19 @@ class Hydrator
}
if (!$classReflector->isInternal()) {
$readonly = new \stdClass();
foreach ($classReflector->getProperties(\ReflectionProperty::IS_READONLY) as $propertyReflector) {
if ($class === $propertyReflector->class) {
$readonly->{$propertyReflector->name} = true;
$notByRef = new \stdClass();
foreach ($classReflector->getProperties() as $propertyReflector) {
if ($propertyReflector->isStatic()) {
continue;
}
if (\PHP_VERSION_ID >= 80400 && !$propertyReflector->isAbstract() && $propertyReflector->getHooks()) {
$notByRef->{$propertyReflector->name} = $propertyReflector->setRawValue(...);
} elseif ($propertyReflector->isReadOnly()) {
$notByRef->{$propertyReflector->name} = true;
}
}
return $baseHydrator->bindTo($readonly, $class);
return $baseHydrator->bindTo($notByRef, $class);
}
if ($classReflector->name !== $class) {
@@ -269,12 +280,23 @@ class Hydrator
continue;
}
$name = $property->name;
$access = ($flags << 2) | ($flags & \ReflectionProperty::IS_READONLY ? self::PROPERTY_NOT_BY_REF : 0);
if (\PHP_VERSION_ID >= 80400 && !$property->isAbstract() && $h = $property->getHooks()) {
$access |= self::PROPERTY_HAS_HOOKS | (isset($h['get']) && !$h['get']->returnsReference() ? self::PROPERTY_NOT_BY_REF : 0);
}
if (\ReflectionProperty::IS_PRIVATE & $flags) {
$propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, $flags & \ReflectionProperty::IS_READONLY ? $class : null, $property];
$propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, null, $access, $property];
continue;
}
$propertyScopes[$name] = [$class, $name, $flags & \ReflectionProperty::IS_READONLY ? $property->class : null, $property];
$propertyScopes[$name] = [$class, $name, null, $access, $property];
if ($flags & (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY)) {
$propertyScopes[$name][2] = $property->class;
}
if (\ReflectionProperty::IS_PROTECTED & $flags) {
$propertyScopes["\0*\0$name"] = $propertyScopes[$name];
@@ -285,12 +307,20 @@ class Hydrator
$class = $r->name;
foreach ($r->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
if (!$property->isStatic()) {
$name = $property->name;
$readonlyScope = $property->isReadOnly() ? $class : null;
$propertyScopes["\0$class\0$name"] = [$class, $name, $readonlyScope, $property];
$propertyScopes[$name] ??= [$class, $name, $readonlyScope, $property];
$flags = $property->getModifiers();
if (\ReflectionProperty::IS_STATIC & $flags) {
continue;
}
$name = $property->name;
$access = ($flags << 2) | ($flags & \ReflectionProperty::IS_READONLY ? self::PROPERTY_NOT_BY_REF : 0);
if (\PHP_VERSION_ID >= 80400 && $h = $property->getHooks()) {
$access |= self::PROPERTY_HAS_HOOKS | (isset($h['get']) && !$h['get']->returnsReference() ? self::PROPERTY_NOT_BY_REF : 0);
}
$propertyScopes["\0$class\0$name"] = [$class, $name, null, $access, $property];
$propertyScopes[$name] ??= $propertyScopes["\0$class\0$name"];
}
}

View File

@@ -50,6 +50,7 @@ class LazyObjectRegistry
public static function getClassResetters($class)
{
$classProperties = [];
$hookedProperties = [];
if ((self::$classReflectors[$class] ??= new \ReflectionClass($class))->isInternal()) {
$propertyScopes = [];
@@ -57,11 +58,17 @@ class LazyObjectRegistry
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
}
foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) {
foreach ($propertyScopes as $key => [$scope, $name, $writeScope, $access]) {
$propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
if ($k === $key && "\0$class\0lazyObjectState" !== $k) {
$classProperties[$readonlyScope ?? $scope][$name] = $key;
if ($k !== $key || "\0$class\0lazyObjectState" === $k) {
continue;
}
if ($access & Hydrator::PROPERTY_HAS_HOOKS) {
$hookedProperties[$k] = true;
} else {
$classProperties[$writeScope ?? $scope][$name] = $key;
}
}
@@ -76,9 +83,13 @@ class LazyObjectRegistry
}, null, $scope);
}
$resetters[] = static function ($instance, $skippedProperties, $onlyProperties = null) {
$resetters[] = static function ($instance, $skippedProperties, $onlyProperties = null) use ($hookedProperties) {
foreach ((array) $instance as $name => $value) {
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties) && (null === $onlyProperties || \array_key_exists($name, $onlyProperties))) {
if ("\0" !== ($name[0] ?? '')
&& !\array_key_exists($name, $skippedProperties)
&& (null === $onlyProperties || \array_key_exists($name, $onlyProperties))
&& !isset($hookedProperties[$name])
) {
unset($instance->$name);
}
}
@@ -90,8 +101,8 @@ class LazyObjectRegistry
public static function getClassAccessors($class)
{
return \Closure::bind(static fn () => [
'get' => static function &($instance, $name, $readonly) {
if (!$readonly) {
'get' => static function &($instance, $name, $notByRef) {
if (!$notByRef) {
return $instance->$name;
}
$value = $instance->$name;
@@ -127,9 +138,9 @@ class LazyObjectRegistry
return $methods;
}
public static function getScope($propertyScopes, $class, $property, $readonlyScope = null)
public static function getScopeForRead($propertyScopes, $class, $property)
{
if (null === $readonlyScope && !isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
if (!isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
return null;
}
$frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
@@ -137,7 +148,27 @@ class LazyObjectRegistry
if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
$scope = $frame['object']->class;
}
if (null === $readonlyScope && '*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
if ('*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
return null;
}
return $scope;
}
public static function getScopeForWrite($propertyScopes, $class, $property, $flags)
{
if (!($flags & (\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_READONLY | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET | \ReflectionProperty::IS_PROTECTED_SET : 0)))) {
return null;
}
$frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
$scope = $frame['object']->class;
}
if ($flags & (\ReflectionProperty::IS_PRIVATE | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY))) {
return $scope;
}
if ($flags & (\ReflectionProperty::IS_PROTECTED | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PROTECTED_SET : 0)) && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
return null;
}

View File

@@ -45,7 +45,7 @@ class LazyObjectState
$this->status = \is_array($initializer) ? self::STATUS_UNINITIALIZED_PARTIAL : self::STATUS_UNINITIALIZED_FULL;
}
public function initialize($instance, $propertyName, $propertyScope)
public function initialize($instance, $propertyName, $writeScope)
{
if (self::STATUS_INITIALIZED_FULL === $this->status) {
return self::STATUS_INITIALIZED_FULL;
@@ -53,13 +53,13 @@ class LazyObjectState
if (\is_array($this->initializer)) {
$class = $instance::class;
$propertyScope ??= $class;
$writeScope ??= $class;
$propertyScopes = Hydrator::$propertyScopes[$class];
$propertyScopes[$k = "\0$propertyScope\0$propertyName"] ?? $propertyScopes[$k = "\0*\0$propertyName"] ?? $k = $propertyName;
$propertyScopes[$k = "\0$writeScope\0$propertyName"] ?? $propertyScopes[$k = "\0*\0$propertyName"] ?? $k = $propertyName;
if ($initializer = $this->initializer[$k] ?? null) {
$value = $initializer(...[$instance, $propertyName, $propertyScope, LazyObjectRegistry::$defaultProperties[$class][$k] ?? null]);
$accessor = LazyObjectRegistry::$classAccessors[$propertyScope] ??= LazyObjectRegistry::getClassAccessors($propertyScope);
$value = $initializer(...[$instance, $propertyName, $writeScope, LazyObjectRegistry::$defaultProperties[$class][$k] ?? null]);
$accessor = LazyObjectRegistry::$classAccessors[$writeScope] ??= LazyObjectRegistry::getClassAccessors($writeScope);
$accessor['set']($instance, $propertyName, $value);
return $this->status = self::STATUS_INITIALIZED_PARTIAL;
@@ -67,12 +67,12 @@ class LazyObjectState
if ($initializer = $this->initializer["\0"] ?? null) {
if (!\is_array($values = $initializer($instance, LazyObjectRegistry::$defaultProperties[$class]))) {
throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
throw new \TypeError(\sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
}
$properties = (array) $instance;
foreach ($values as $key => $value) {
if (!\array_key_exists($key, $properties) && [$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
if (!\array_key_exists($key, $properties) && [$scope, $name, $writeScope] = $propertyScopes[$key] ?? null) {
$scope = $writeScope ?? $scope;
$accessor = LazyObjectRegistry::$classAccessors[$scope] ??= LazyObjectRegistry::getClassAccessors($scope);
$accessor['set']($instance, $name, $value);
@@ -116,10 +116,10 @@ class LazyObjectState
$properties = (array) $instance;
$onlyProperties = \is_array($this->initializer) ? $this->initializer : null;
foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) {
foreach ($propertyScopes as $key => [$scope, $name, , $access]) {
$propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
if ($k === $key && (null !== $readonlyScope || !\array_key_exists($k, $properties))) {
if ($k === $key && ($access & Hydrator::PROPERTY_HAS_HOOKS || ($access >> 2) & \ReflectionProperty::IS_READONLY || !\array_key_exists($k, $properties))) {
$skippedProperties[$k] = true;
}
}

View File

@@ -11,12 +11,15 @@
namespace Symfony\Component\VarExporter\Internal;
use Symfony\Component\Serializer\Attribute\Ignore;
if (\PHP_VERSION_ID >= 80300) {
/**
* @internal
*/
trait LazyObjectTrait
{
#[Ignore]
private readonly LazyObjectState $lazyObjectState;
}
} else {
@@ -25,6 +28,7 @@ if (\PHP_VERSION_ID >= 80300) {
*/
trait LazyObjectTrait
{
#[Ignore]
private LazyObjectState $lazyObjectState;
}
}