ClientReplication.php 2.1 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\Options;
  11. use Predis\Network\IConnectionReplication;
  12. use Predis\Network\MasterSlaveReplication;
  13. /**
  14. * Option class that returns a replication connection be used by a client.
  15. *
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. class ClientReplication extends Option
  19. {
  20. /**
  21. * Checks if the specified value is a valid instance of IConnectionReplication.
  22. *
  23. * @param IConnectionReplication $cluster Instance of a connection cluster.
  24. * @return IConnectionReplication
  25. */
  26. protected function checkInstance($connection)
  27. {
  28. if (!$connection instanceof IConnectionReplication) {
  29. throw new \InvalidArgumentException('Instance of Predis\Network\IConnectionReplication expected');
  30. }
  31. return $connection;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function filter(IClientOptions $options, $value)
  37. {
  38. if (is_callable($value)) {
  39. $connection = call_user_func($value, $options);
  40. if (!$connection instanceof IConnectionReplication) {
  41. throw new \InvalidArgumentException('Instance of Predis\Network\IConnectionReplication expected');
  42. }
  43. return $connection;
  44. }
  45. if (is_string($value)) {
  46. if (!class_exists($value)) {
  47. throw new \InvalidArgumentException("Class $value does not exist");
  48. }
  49. if (!($connection = new $value()) instanceof IConnectionReplication) {
  50. throw new \InvalidArgumentException('Instance of Predis\Network\IConnectionReplication expected');
  51. }
  52. return $connection;
  53. }
  54. if ($value == true) {
  55. return $this->getDefault($options);
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getDefault(IClientOptions $options)
  62. {
  63. return new MasterSlaveReplication();
  64. }
  65. }