ClientCluster.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Options;
  11. use Predis\Network\IConnectionCluster;
  12. use Predis\Network\PredisCluster;
  13. class ClientCluster extends Option
  14. {
  15. protected function checkInstance($cluster)
  16. {
  17. if (!$cluster instanceof IConnectionCluster) {
  18. throw new \InvalidArgumentException(
  19. 'Instance of Predis\Network\IConnectionCluster expected'
  20. );
  21. }
  22. return $cluster;
  23. }
  24. public function validate($value)
  25. {
  26. if (is_callable($value)) {
  27. return $this->checkInstance(call_user_func($value));
  28. }
  29. $initializer = $this->getInitializer($value);
  30. return $this->checkInstance($initializer());
  31. }
  32. protected function getInitializer($fqnOrType)
  33. {
  34. switch ($fqnOrType) {
  35. case 'predis':
  36. return function() { return new PredisCluster(); };
  37. default:
  38. return function() use($fqnOrType) {
  39. return new $fqnOrType();
  40. };
  41. }
  42. }
  43. public function getDefault()
  44. {
  45. return new PredisCluster();
  46. }
  47. }