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 Predis\Connection\Aggregate\ClusterInterface;
  12. use Predis\Connection\Aggregate\PredisCluster;
  13. use Predis\Connection\Aggregate\RedisCluster;
  14. /**
  15. * Configures an aggregate connection used for clustering
  16. * multiple Redis nodes using various implementations with
  17. * different algorithms or strategies.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class ClusterOption implements OptionInterface
  22. {
  23. /**
  24. * Creates a new cluster connection from on a known descriptive name.
  25. *
  26. * @param OptionsInterface $options Instance of the client options.
  27. * @param string $id Descriptive identifier of the cluster type (`predis`, `redis-cluster`)
  28. *
  29. * @return ClusterInterface|null
  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. default:
  41. return;
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function filter(OptionsInterface $options, $value)
  48. {
  49. if (is_string($value)) {
  50. $value = $this->createByDescription($options, $value);
  51. }
  52. if (!$value instanceof ClusterInterface) {
  53. throw new \InvalidArgumentException(
  54. "An instance of type 'Predis\Connection\Aggregate\ClusterInterface' was expected."
  55. );
  56. }
  57. return $value;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getDefault(OptionsInterface $options)
  63. {
  64. return new PredisCluster();
  65. }
  66. }