Skip to content

Commit

Permalink
Merge branch '7.0' into 7.1
Browse files Browse the repository at this point in the history
* 7.0:
  synchronize Redis proxy traits for different versions
  [Serializer] Fixed BackedEnumNormalizer priority for translatable enum
  [Config] Fix `YamlReferenceDumper` handling of array examples
  [Yaml] prefix examples with #
  • Loading branch information
xabbuh committed Mar 27, 2024
2 parents e70dcb1 + 07d63d3 commit fc97603
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,6 @@
])

->set('serializer.normalizer.backed_enum', BackedEnumNormalizer::class)
->tag('serializer.normalizer', ['priority' => -915])
->tag('serializer.normalizer', ['priority' => -880])
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum TranslatableBackedEnum: string implements TranslatableInterface
{
case Get = 'GET';

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return match ($this) {
self::Get => 'custom_get_string',
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\TranslatableBackedEnum;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
Expand Down Expand Up @@ -67,6 +69,15 @@ public static function provideNormalizersAndEncodersWithDefaultContextOption():
['serializer.encoder.csv.alias'],
];
}

public function testSerializeTranslatableBackedEnum()
{
static::bootKernel(['test_case' => 'Serializer']);

$serializer = static::getContainer()->get('serializer.alias');

$this->assertEquals('GET', $serializer->serialize(TranslatableBackedEnum::Get, 'yaml'));
}
}

class Foo
Expand Down
32 changes: 15 additions & 17 deletions src/Symfony/Component/Cache/Tests/Traits/RedisProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,31 @@ public function testRelayProxy()
*/
public function testRedis6Proxy($class, $stub)
{
$stub = file_get_contents("https://raw.githubusercontent.com/phpredis/phpredis/develop/{$stub}.stub.php");
if (version_compare(phpversion('redis'), '6.0.2', '>')) {
$stub = file_get_contents("https://raw.githubusercontent.com/phpredis/phpredis/develop/{$stub}.stub.php");
} else {
$stub = file_get_contents("https://raw.githubusercontent.com/phpredis/phpredis/6.0.2/{$stub}.stub.php");
}

$stub = preg_replace('/^class /m', 'return; \0', $stub);
$stub = preg_replace('/^return; class ([a-zA-Z]++)/m', 'interface \1StubInterface', $stub, 1);
$stub = preg_replace('/^ public const .*/m', '', $stub);
eval(substr($stub, 5));
$r = new \ReflectionClass($class.'StubInterface');

$proxy = file_get_contents(\dirname(__DIR__, 2)."/Traits/{$class}6Proxy.php");
$proxy = substr($proxy, 0, 4 + strpos($proxy, '[];'));
$this->assertEquals(self::dumpMethods(new \ReflectionClass($class.'StubInterface')), self::dumpMethods(new \ReflectionClass(sprintf('Symfony\Component\Cache\Traits\%s6Proxy', $class))));
}

private static function dumpMethods(\ReflectionClass $class): string
{
$methods = [];

foreach ($r->getMethods() as $method) {
foreach ($class->getMethods() as $method) {
if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name)) {
continue;
}

$return = $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return ';
$signature = ProxyHelper::exportSignature($method, false, $args);

if ('Redis' === $class && 'mget' === $method->name) {
$signature = str_replace(': \Redis|array|false', ': \Redis|array', $signature);
}

if ('RedisCluster' === $class && 'publish' === $method->name) {
$signature = str_replace(': \RedisCluster|bool|int', ': \RedisCluster|bool', $signature);
}

$methods[] = "\n ".str_replace('timeout = 0.0', 'timeout = 0', $signature)."\n".<<<EOPHP
{
{$return}(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
Expand All @@ -119,9 +118,8 @@ public function testRedis6Proxy($class, $stub)
EOPHP;
}

uksort($methods, 'strnatcmp');
$proxy .= implode('', $methods)."}\n";
usort($methods, 'strnatcmp');

$this->assertStringEqualsFile(\dirname(__DIR__, 2)."/Traits/{$class}6Proxy.php", $proxy);
return implode("\n", $methods);
}
}
16 changes: 1 addition & 15 deletions src/Symfony/Component/Cache/Traits/Redis6Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Redis6Proxy extends \Redis implements ResetInterface, LazyObjectInterface
use LazyProxyTrait {
resetLazyObject as reset;
}
use Redis6ProxyTrait;

private const LAZY_OBJECT_PROPERTY_SCOPES = [];

Expand Down Expand Up @@ -96,11 +97,6 @@ public function bgrewriteaof(): \Redis|bool
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->bgrewriteaof(...\func_get_args());
}

public function waitaof($numlocal, $numreplicas, $timeout): \Redis|array|false
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->waitaof(...\func_get_args());
}

public function bitcount($key, $start = 0, $end = -1, $bybit = false): \Redis|false|int
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->bitcount(...\func_get_args());
Expand Down Expand Up @@ -231,11 +227,6 @@ public function discard(): \Redis|bool
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->discard(...\func_get_args());
}

public function dump($key): \Redis|string
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->dump(...\func_get_args());
}

public function echo($str): \Redis|false|string
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->echo(...\func_get_args());
Expand Down Expand Up @@ -656,11 +647,6 @@ public function ltrim($key, $start, $end): \Redis|bool
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->ltrim(...\func_get_args());
}

public function mget($keys): \Redis|array
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->mget(...\func_get_args());
}

public function migrate($host, $port, $key, $dstdb, $timeout, $copy = false, $replace = false, #[\SensitiveParameter] $credentials = null): \Redis|bool
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->migrate(...\func_get_args());
Expand Down
51 changes: 51 additions & 0 deletions src/Symfony/Component/Cache/Traits/Redis6ProxyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Traits;

if (version_compare(phpversion('redis'), '6.0.2', '>')) {
/**
* @internal
*/
trait Redis6ProxyTrait
{
public function dump($key): \Redis|false|string
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->dump(...\func_get_args());
}

public function mget($keys): \Redis|array|false
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->mget(...\func_get_args());
}

public function waitaof($numlocal, $numreplicas, $timeout): \Redis|array|false
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->waitaof(...\func_get_args());
}
}
} else {
/**
* @internal
*/
trait Redis6ProxyTrait
{
public function dump($key): \Redis|string
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->dump(...\func_get_args());
}

public function mget($keys): \Redis|array
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->mget(...\func_get_args());
}
}
}
11 changes: 1 addition & 10 deletions src/Symfony/Component/Cache/Traits/RedisCluster6Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class RedisCluster6Proxy extends \RedisCluster implements ResetInterface, LazyOb
use LazyProxyTrait {
resetLazyObject as reset;
}
use RedisCluster6ProxyTrait;

private const LAZY_OBJECT_PROPERTY_SCOPES = [];

Expand Down Expand Up @@ -96,11 +97,6 @@ public function bgrewriteaof($key_or_address): \RedisCluster|bool
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->bgrewriteaof(...\func_get_args());
}

public function waitaof($key_or_address, $numlocal, $numreplicas, $timeout): \RedisCluster|array|false
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->waitaof(...\func_get_args());
}

public function bgsave($key_or_address): \RedisCluster|bool
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->bgsave(...\func_get_args());
Expand Down Expand Up @@ -661,11 +657,6 @@ public function pttl($key): \RedisCluster|false|int
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->pttl(...\func_get_args());
}

public function publish($channel, $message): \RedisCluster|bool
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->publish(...\func_get_args());
}

public function pubsub($key_or_address, ...$values): mixed
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->pubsub(...\func_get_args());
Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Component/Cache/Traits/RedisCluster6ProxyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Traits;

if (version_compare(phpversion('redis'), '6.0.2', '>')) {
/**
* @internal
*/
trait RedisCluster6ProxyTrait
{
public function publish($channel, $message): \RedisCluster|bool|int
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->publish(...\func_get_args());
}

public function waitaof($key_or_address, $numlocal, $numreplicas, $timeout): \RedisCluster|array|false
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->waitaof(...\func_get_args());
}
}
} else {
/**
* @internal
*/
trait RedisCluster6ProxyTrait
{
public function publish($channel, $message): \RedisCluster|bool
{
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->publish(...\func_get_args());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Config\Definition\NodeInterface;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\VariableNode;
use Symfony\Component\Yaml\Inline;

/**
Expand Down Expand Up @@ -90,19 +89,12 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul
$children = $this->getPrototypeChildren($node);
}

if (!$children) {
if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
$default = '';
} elseif (!\is_array($example)) {
$default = '[]';
}
if (!$children && !($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue()))) {
$default = '[]';
}
} elseif ($node instanceof EnumNode) {
$comments[] = 'One of '.$node->getPermissibleValues('; ');
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
} elseif (VariableNode::class === $node::class && \is_array($example)) {
// If there is an array example, we are sure we dont need to print a default value
$default = '';
} else {
$default = '~';

Expand Down Expand Up @@ -170,7 +162,7 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul

$this->writeLine('# '.$message.':', $depth * 4 + 4);

$this->writeArray(array_map(Inline::dump(...), $example), $depth + 1);
$this->writeArray(array_map(Inline::dump(...), $example), $depth + 1, true);
}

if ($children) {
Expand All @@ -191,7 +183,7 @@ private function writeLine(string $text, int $indent = 0): void
$this->reference .= sprintf($format, $text)."\n";
}

private function writeArray(array $array, int $depth): void
private function writeArray(array $array, int $depth, bool $asComment = false): void
{
$isIndexed = array_is_list($array);

Expand All @@ -201,15 +193,18 @@ private function writeArray(array $array, int $depth): void
} else {
$val = $value;
}
$prefix = $asComment ? '# ' : '';

$prefix = $asComment ? '# ' : '';

if ($isIndexed) {
$this->writeLine('- '.$val, $depth * 4);
$this->writeLine($prefix.'- '.$val, $depth * 4);
} else {
$this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
$this->writeLine(sprintf('%s%-20s %s', $prefix, $key.':', $val), $depth * 4);
}

if (\is_array($value)) {
$this->writeArray($value, $depth + 1);
$this->writeArray($value, $depth + 1, $asComment);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ enum=""
</pipou>
<array-with-array-example-and-no-default-value />
</config>
EOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ enum: ~ # One of "this"; "that"; Symfony\Component\Config\Tests\
# which should be indented
child3: ~ # Example: 'example setting'
scalar_prototyped: []
variable:
variable: ~
# Examples:
- foo
- bar
# - foo
# - bar
parameters:
# Prototype: Parameter name
Expand All @@ -142,6 +142,11 @@ enum: ~ # One of "this"; "that"; Symfony\Component\Config\Tests\
# Prototype
name: []
array_with_array_example_and_no_default_value: []
# Examples:
# - foo
# - bar
custom_node: true
EOL;
Expand Down

0 comments on commit fc97603

Please sign in to comment.