minor #38567 [Cache] Make Redis initializers static (Nyholm)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] Make Redis initializers static

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes .. or maybe?
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

I am on very thin ice now. I saw a comment on similar code here: https://github.com/symfony/symfony/pull/38563#discussion_r504600024

These anonymous functions in the cache component could also be made static to avoid being connected to the object using the Redis trait.

Feel free to correct me if this does not make much sense.

Commits
-------

ad8de57b91 [Cache] Make Redis initializers static
This commit is contained in:
Fabien Potencier 2020-10-14 16:09:22 +02:00
commit a590e90698

View File

@ -174,7 +174,7 @@ trait RedisTrait
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
$redis = new $class();
$initializer = function ($redis) use ($connect, $params, $dsn, $auth, $hosts) {
$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts) {
try {
@$redis->{$connect}($hosts[0]['host'] ?? $hosts[0]['path'], $hosts[0]['port'] ?? null, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval']);
@ -226,7 +226,7 @@ trait RedisTrait
$redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
}
} elseif (is_a($class, \RedisCluster::class, true)) {
$initializer = function () use ($class, $params, $dsn, $hosts) {
$initializer = static function () use ($class, $params, $dsn, $hosts) {
foreach ($hosts as $i => $host) {
$hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
}