| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Monospice\LaravelRedisSentinel\Connectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Monospice\LaravelRedisSentinel\Connections\PredisConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Predis\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Initializes Predis Client instances for Redis Sentinel connections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @category Package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package  Monospice\LaravelRedisSentinel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author   Cy Rossignol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @license  See LICENSE file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @link     http://github.com/monospice/laravel-redis-sentinel-drivers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class PredisConnector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Configuration options specific to Sentinel connection operation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * We cannot pass these options as an array to the Predis client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Instead, we'll set them on the connection directly using methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * provided by the SentinelReplication class of the Predis package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $sentinelConnectionOptionKeys = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'sentinel_timeout', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'retry_wait', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'retry_limit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'update_sentinels', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Create a new Redis Sentinel connection from the provided configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $server  The client configuration for the connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param array $options The global client options shared by all Sentinel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * connections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return PredisConnection The Sentinel connection containing a configured | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Predis Client | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |     public function connect(array $server, array $options = [ ]) | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |         // Merge the global options shared by all Sentinel connections with | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         // connection-specific options | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         $clientOpts = array_merge($options, Arr::pull($server, 'options', [ ])); | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         // Automatically set "replication" to "sentinel". This is the Sentinel | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         // driver, after all. | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         $clientOpts['replication'] = 'sentinel'; | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         // Extract the array of Sentinel connection options from the rest of | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         // the client options | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         $sentinelKeys = array_flip($this->sentinelConnectionOptionKeys); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         $sentinelOpts = array_intersect_key($clientOpts, $sentinelKeys); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         // Filter the Sentinel connection options elements from the client | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         // options array | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         $clientOpts = array_diff_key($clientOpts, $sentinelKeys); | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         return new PredisConnection( | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             new Client($server, $clientOpts), | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             $sentinelOpts | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |