ConnectionFactoryOption.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 InvalidArgumentException;
  12. use Predis\Connection;
  13. /**
  14. * Configures a connection factory used by the client to create new connection
  15. * instances for single Redis nodes.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class ConnectionFactoryOption implements OptionInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function filter(OptionsInterface $options, $value)
  25. {
  26. if ($value instanceof Connection\FactoryInterface) {
  27. return $value;
  28. } else if (is_array($value)) {
  29. $factory = $this->getDefault($options);
  30. foreach ($value as $scheme => $initializer) {
  31. $factory->define($scheme, $initializer);
  32. }
  33. return $factory;
  34. } else {
  35. throw new InvalidArgumentException('Invalid value for the connections option');
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getDefault(OptionsInterface $options)
  42. {
  43. return new Connection\Factory();
  44. }
  45. }