ConnectionFactoryOption.php 1.3 KB

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