ClusterOption.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\Aggregate\ClusterInterface;
  13. use Predis\Connection\Aggregate\PredisCluster;
  14. use Predis\Connection\Aggregate\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 ClusterInterface
  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 ClusterInterface) {
  51. throw new InvalidArgumentException(
  52. "An instance of type 'Predis\Connection\Aggregate\ClusterInterface' was expected."
  53. );
  54. }
  55. return $value;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getDefault(OptionsInterface $options)
  61. {
  62. return new PredisCluster();
  63. }
  64. }