| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Monospice\LaravelRedisSentinel\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Normalizes host definitions in the package's Redis Sentinel connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * configuration by splitting host definitions that contain multiple hosts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * into individual host definitions to support environment-based configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * The package allows developers to specify multiple hosts in a single Redis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Sentinel "*_HOST" environment variable to provide a way for developers to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * configure multiple hosts for a connection through the environment without | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * the need to modify the package's default configuration. An environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * variable can contain a comma-seperated string of hosts that the package will | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * split automatically using this class: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *     REDIS_HOST=sentinel1.example.com,sentinel2.example.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Before parsing the connection configuration, the connection config would | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * contain the following value if using the environment variable above: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *     'connection' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *             'host' => 'sentinel1.example.com,sentinel2.example.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  *             'port' => 26379, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  *         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  *     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * This class will convert the connection configuration to: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *     'connection' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  *         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  *             'host' => 'sentinel1.example.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *             'port' => 26379, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  *         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  *         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  *             'host' => 'sentinel2.example.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  *             'port' => 26379, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  *         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  *     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * @category Package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * @package  Monospice\LaravelRedisSentinel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * @author   Cy Rossignol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * @license  See LICENSE file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * @link     https://github.com/monospice/laravel-redis-sentinel-drivers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | class HostNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Create single host entries for any host definitions that specify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * multiple hosts in the provided set of connection configurations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param array $connections The set of connection configs containing host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * definitions to normalize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return array The normalized Redis Sentinel connection configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public static function normalizeConnections(array $connections) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($connections as $name => $connection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if ($name === 'options' || $name === 'clusters') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $connections[$name] = static::normalizeConnection($connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $connections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Create single host entries for any host definitions that specify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * multiple hosts in the provided connection configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array $connection The connection config which contains the host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * definitions for a single Redis Sentinel connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return array The normalized connection configuration values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public static function normalizeConnection(array $connection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $normal = [ ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if (array_key_exists('options', $connection)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $normal['options'] = $connection['options']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             unset($connection['options']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         foreach ($connection as $host) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $normal = array_merge($normal, static::normalizeHost($host)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $normal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Parse the provided host definition into multiple host definitions if it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * specifies more than one host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param array|string $host The host definition from a Redis Sentinel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return array One or more host definitions parsed from the provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * host definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public static function normalizeHost($host) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if (is_array($host)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return static::normalizeHostArray($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (is_string($host)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return static::normalizeHostString($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return [ $host ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Parse a host definition in the form of an array into multiple host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * definitions if it specifies more than one host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param array $hostArray The host definition from a Redis Sentinel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @return array One or more host definitions parsed from the provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * host definition | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     protected static function normalizeHostArray(array $hostArray) | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         if (! array_key_exists('host', $hostArray)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |             return [ $hostArray ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         $port = Arr::get($hostArray, 'port', 26379); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         return static::normalizeHostString($hostArray['host'], $port); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * Parse a host definition in the form of a string into multiple host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * definitions it it specifies more than one host. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param string $hostString The host definition from a Redis Sentinel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param int    $port       The port number to use for the resulting host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * definitions if the parsed host definition doesn't contain port numbers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @return array One or more host definitions parsed from the provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * host definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     protected static function normalizeHostString($hostString, $port = 26379) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $hosts = [ ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         foreach (explode(',', $hostString) as $host) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $host = trim($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             if (Str::contains($host, ':')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 $hosts[] = $host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 $hosts[] = [ 'host' => $host, 'port' => $port ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         return $hosts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 172 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |  |