File size: 4,285 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Helper;

use Mautic\CoreBundle\Predis\Command\Unlink;
use Mautic\CoreBundle\Predis\Replication\MasterOnlyStrategy;
use Mautic\CoreBundle\Predis\Replication\StrategyConfig;
use Predis\Client;
use Predis\Cluster\ClusterStrategy;
use Predis\Connection\Aggregate\PredisCluster;
use Predis\Connection\Aggregate\RedisCluster;
use Predis\Connection\Aggregate\SentinelReplication;
use Predis\Profile\RedisProfile;

/**
 * Helper functions for simpler operations with arrays.
 */
class PRedisConnectionHelper
{
    /**
     * Transform the redis url config key into an array if needed
     * If possible also resolve the configured redis hostname to multiple IP addresses.
     *
     * @param mixed $configuredUrls a string or an array of redis endpoints to connect to
     */
    public static function getRedisEndpoints($configuredUrls): iterable
    {
        if (is_iterable($configuredUrls)) {
            // assume arrays are already in the correct format
            return $configuredUrls;
        } else {
            $parsed = parse_url($configuredUrls);
            if (!$parsed) {
                return [$configuredUrls];
            }

            // resolve hostnames ahead of time to support dns records with multiple ip addresses
            // we need to provide each one to predis separately or it will just use a single one
            $resolvedArray = gethostbynamel($parsed['host']);
            if (!$resolvedArray) {
                return [$configuredUrls];
            } else {
                // this will return an array of associative arrays which is supported by Predis
                return array_map(function ($i) use ($parsed) {
                    $parsed['host'] = $i;

                    return $parsed;
                }, $resolvedArray);
            }
        }
    }

    /**
     * Transform the redis mautic config to an options array consumable by PRedis.
     *
     * @param array $redisConfiguration mautic's redis configuration
     */
    public static function makeRedisOptions(array $redisConfiguration, string $prefix = ''): array
    {
        $redisOptions = [];

        if ($prefix) {
            $redisOptions['prefix'] = $prefix;
        }

        if (!empty($redisConfiguration['replication'])) {
            $redisOptions['replication'] = $redisConfiguration['replication'];
            $redisOptions['service']     = $redisConfiguration['service'] ?? 'mymaster';
        }

        if (!empty($redisConfiguration['password'])) {
            $redisOptions['parameters'] = ['password' => $redisConfiguration['password']];
        }

        foreach (['cluster', 'scheme', 'ssl'] as $key) {
            if (isset($redisConfiguration[$key])) {
                $redisOptions[$key] = $redisConfiguration[$key];
            }
        }

        return $redisOptions;
    }

    /**
     * @param mixed[] $endpoints
     * @param mixed[] $inputOptions
     */
    public static function createClient(array $endpoints, array $inputOptions): Client
    {
        $replication = $inputOptions['replication'] ?? null;

        if ('sentinel' === $replication) {
            $inputOptions['aggregate'] = fn () => fn ($sentinels, $options): SentinelReplication => new SentinelReplication(
                $options->service,
                $sentinels,
                $options->connections,
                new MasterOnlyStrategy(StrategyConfig::fromArray($inputOptions))
            );
        }

        $client  = new Client($endpoints, $inputOptions);
        $profile = $client->getProfile();
        \assert($profile instanceof RedisProfile);

        if (!$profile->getCommandClass(Unlink::ID)) {
            $profile->defineCommand(Unlink::ID, Unlink::class);
        }

        $connection = $client->getConnection();

        if ($connection instanceof RedisCluster || $connection instanceof PredisCluster) {
            $clusterStrategy = $connection->getClusterStrategy();

            if ($clusterStrategy instanceof ClusterStrategy && !in_array(Unlink::ID, $clusterStrategy->getSupportedCommands())) {
                $clusterStrategy->setCommandHandler(Unlink::ID, [$clusterStrategy, 'getKeyFromAllArguments']);
            }
        }

        return $client;
    }
}