ClientConnectionFactory.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\ConnectionFactory;
  12. use Predis\Connection\ConnectionFactoryInterface;
  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 AbstractOption
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function filter(ClientOptionsInterface $options, $value)
  24. {
  25. if ($value instanceof ConnectionFactoryInterface) {
  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 ConnectionFactoryInterface) {
  37. throw new \InvalidArgumentException("Class $value must be an instance of Predis\Connection\ConnectionFactoryInterface");
  38. }
  39. return $factory;
  40. }
  41. throw new \InvalidArgumentException('Invalid value for the connections option');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getDefault(ClientOptionsInterface $options)
  47. {
  48. return new ConnectionFactory($options->profile);
  49. }
  50. }