ClusterOption.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. return null;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function filter(OptionsInterface $options, $value)
  47. {
  48. if (is_string($value)) {
  49. $value = $this->createByDescription($options, $value);
  50. }
  51. if (!$value instanceof ClusterInterface) {
  52. throw new InvalidArgumentException(
  53. "An instance of type 'Predis\Connection\Aggregate\ClusterInterface' was expected."
  54. );
  55. }
  56. return $value;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getDefault(OptionsInterface $options)
  62. {
  63. return new PredisCluster();
  64. }
  65. }