ClientCluster.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Predis\Options;
  3. use Predis\Network\IConnectionCluster;
  4. use Predis\Network\PredisCluster;
  5. class ClientCluster extends Option
  6. {
  7. protected function checkInstance($cluster)
  8. {
  9. if (!$cluster instanceof IConnectionCluster) {
  10. throw new \InvalidArgumentException(
  11. 'Instance of Predis\Network\IConnectionCluster expected'
  12. );
  13. }
  14. return $cluster;
  15. }
  16. public function validate($value)
  17. {
  18. if (is_callable($value)) {
  19. return $this->checkInstance(call_user_func($value));
  20. }
  21. $initializer = $this->getInitializer($value);
  22. return $this->checkInstance($initializer());
  23. }
  24. protected function getInitializer($fqnOrType)
  25. {
  26. switch ($fqnOrType) {
  27. case 'predis':
  28. return function() { return new PredisCluster(); };
  29. default:
  30. return function() use($fqnOrType) {
  31. return new $fqnOrType();
  32. };
  33. }
  34. }
  35. public function getDefault()
  36. {
  37. return new PredisCluster();
  38. }
  39. }