1<?php 2 3/* 4 * This file is part of the Predis package. 5 * 6 * (c) Daniele Alessandri <suppakilla@gmail.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Predis\Configuration; 13 14use Predis\Connection\Aggregate\ClusterInterface; 15use Predis\Connection\Aggregate\PredisCluster; 16use Predis\Connection\Aggregate\RedisCluster; 17 18/** 19 * Configures an aggregate connection used for clustering 20 * multiple Redis nodes using various implementations with 21 * different algorithms or strategies. 22 * 23 * @author Daniele Alessandri <suppakilla@gmail.com> 24 */ 25class ClusterOption implements OptionInterface 26{ 27 /** 28 * Creates a new cluster connection from on a known descriptive name. 29 * 30 * @param OptionsInterface $options Instance of the client options. 31 * @param string $id Descriptive identifier of the cluster type (`predis`, `redis-cluster`) 32 * 33 * @return ClusterInterface|null 34 */ 35 protected function createByDescription(OptionsInterface $options, $id) 36 { 37 switch ($id) { 38 case 'predis': 39 case 'predis-cluster': 40 return new PredisCluster(); 41 42 case 'redis': 43 case 'redis-cluster': 44 return new RedisCluster($options->connections); 45 46 default: 47 return; 48 } 49 } 50 51 /** 52 * {@inheritdoc} 53 */ 54 public function filter(OptionsInterface $options, $value) 55 { 56 if (is_string($value)) { 57 $value = $this->createByDescription($options, $value); 58 } 59 60 if (!$value instanceof ClusterInterface) { 61 throw new \InvalidArgumentException( 62 "An instance of type 'Predis\Connection\Aggregate\ClusterInterface' was expected." 63 ); 64 } 65 66 return $value; 67 } 68 69 /** 70 * {@inheritdoc} 71 */ 72 public function getDefault(OptionsInterface $options) 73 { 74 return new PredisCluster(); 75 } 76} 77