ReplicationOption.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\MasterSlaveReplication;
  12. use Predis\Connection\Aggregate\SentinelReplication;
  13. use Predis\Connection\Aggregate\ReplicationInterface;
  14. /**
  15. * Configures an aggregate connection used for master/slave replication among
  16. * multiple Redis nodes.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class ReplicationOption implements OptionInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @todo There's more code than needed due to a bug in filter_var() as
  26. * discussed here https://bugs.php.net/bug.php?id=49510 and different
  27. * behaviours when encountering NULL values on PHP 5.3.
  28. */
  29. public function filter(OptionsInterface $options, $value)
  30. {
  31. if ($value instanceof ReplicationInterface) {
  32. return $value;
  33. }
  34. if (is_bool($value) || $value === null) {
  35. return $value ? $this->getDefault($options) : null;
  36. }
  37. if ($value === 'sentinel') {
  38. return function ($sentinels, $options) {
  39. return new SentinelReplication($options->service, $sentinels, $options->connections);
  40. };
  41. }
  42. if (
  43. !is_object($value) &&
  44. null !== $asbool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
  45. ) {
  46. return $asbool ? $this->getDefault($options) : null;
  47. }
  48. throw new \InvalidArgumentException(
  49. "An instance of type 'Predis\Connection\Aggregate\ReplicationInterface' was expected."
  50. );
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getDefault(OptionsInterface $options)
  56. {
  57. $replication = new MasterSlaveReplication();
  58. if ($options->autodiscovery) {
  59. $replication->setConnectionFactory($options->connections);
  60. $replication->setAutoDiscovery(true);
  61. }
  62. return $replication;
  63. }
  64. }