ClientReplication.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Option;
  11. use Predis\Connection\MasterSlaveReplication;
  12. use Predis\Connection\ReplicationConnectionInterface;
  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 AbstractOption
  19. {
  20. /**
  21. * Checks if the specified value is a valid instance of ReplicationConnectionInterface.
  22. *
  23. * @param ReplicationConnectionInterface $connection Instance of a replication connection.
  24. * @return ReplicationConnectionInterface
  25. */
  26. protected function checkInstance($connection)
  27. {
  28. if (!$connection instanceof ReplicationConnectionInterface) {
  29. throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
  30. }
  31. return $connection;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function filter(ClientOptionsInterface $options, $value)
  37. {
  38. if (is_callable($value)) {
  39. $connection = call_user_func($value, $options);
  40. if (!$connection instanceof ReplicationConnectionInterface) {
  41. throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface 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 ReplicationConnectionInterface) {
  50. throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface 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(ClientOptionsInterface $options)
  62. {
  63. return new MasterSlaveReplication();
  64. }
  65. }