8000 [Cache] synchronize Redis proxy traits for different versions by xabbuh · Pull Request #54410 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] synchronize Redis proxy traits for different versions #54410

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
Mar 27, 2024
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
32 changes: 15 additions & 17 deletions src/Symfony/Component/Cache/Tests/Traits/RedisProxiesTest.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,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 @@ -120,9 +119,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
4FF3
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());
}
}
}
0