ClusterOption.php 1.9 KB

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