ClientConnectionFactory.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\IConnectionFactory;
  12. use Predis\ConnectionFactory;
  13. /**
  14. * Option class that returns a connection factory to be used by a client.
  15. *
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. class ClientConnectionFactory extends Option
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function filter(IClientOptions $options, $value)
  24. {
  25. if ($value instanceof IConnectionFactory) {
  26. return $value;
  27. }
  28. 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. }
  35. if (is_string($value) && class_exists($value)) {
  36. if (!($factory = new $value()) && !$factory instanceof IConnectionFactory) {
  37. throw new \InvalidArgumentException("Class $value must be an instance of Predis\IConnectionFactory");
  38. }
  39. return $factory;
  40. }
  41. throw new \InvalidArgumentException('Invalid value for the connections option');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getDefault(IClientOptions $options)
  47. {
  48. return new ConnectionFactory();
  49. }
  50. }