Factory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Connection;
  11. use InvalidArgumentException;
  12. use UnexpectedValueException;
  13. use ReflectionClass;
  14. use Predis\Command\RawCommand;
  15. /**
  16. * Standard connection factory for creating connections to Redis nodes.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class Factory implements FactoryInterface
  21. {
  22. protected $schemes = array(
  23. 'tcp' => 'Predis\Connection\StreamConnection',
  24. 'unix' => 'Predis\Connection\StreamConnection',
  25. 'http' => 'Predis\Connection\WebdisConnection',
  26. );
  27. /**
  28. * Checks if the provided argument represents a valid connection class
  29. * implementing Predis\Connection\NodeConnectionInterface. Optionally,
  30. * callable objects are used for lazy initialization of connection objects.
  31. *
  32. * @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
  33. * @return mixed
  34. */
  35. protected function checkInitializer($initializer)
  36. {
  37. if (is_callable($initializer)) {
  38. return $initializer;
  39. }
  40. $class = new ReflectionClass($initializer);
  41. if (!$class->isSubclassOf('Predis\Connection\NodeConnectionInterface')) {
  42. throw new InvalidArgumentException(
  43. 'A connection initializer must be a valid connection class or a callable object.'
  44. );
  45. }
  46. return $initializer;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function define($scheme, $initializer)
  52. {
  53. $this->schemes[$scheme] = $this->checkInitializer($initializer);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function undefine($scheme)
  59. {
  60. unset($this->schemes[$scheme]);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function create($parameters)
  66. {
  67. if (!$parameters instanceof ParametersInterface) {
  68. $parameters = Parameters::create($parameters);
  69. }
  70. $scheme = $parameters->scheme;
  71. if (!isset($this->schemes[$scheme])) {
  72. throw new InvalidArgumentException("Unknown connection scheme: '$scheme'.");
  73. }
  74. $initializer = $this->schemes[$scheme];
  75. if (is_callable($initializer)) {
  76. $connection = call_user_func($initializer, $parameters, $this);
  77. } else {
  78. $connection = new $initializer($parameters);
  79. $this->prepareConnection($connection);
  80. }
  81. if (!$connection instanceof NodeConnectionInterface) {
  82. throw new UnexpectedValueException(
  83. "Objects returned by connection initializers must implement ".
  84. "'Predis\Connection\NodeConnectionInterface'."
  85. );
  86. }
  87. return $connection;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function aggregate(AggregateConnectionInterface $connection, array $parameters)
  93. {
  94. foreach ($parameters as $node) {
  95. $connection->add($node instanceof NodeConnectionInterface ? $node : $this->create($node));
  96. }
  97. }
  98. /**
  99. * Prepares a connection instance after its initialization.
  100. *
  101. * @param NodeConnectionInterface $connection Connection instance.
  102. */
  103. protected function prepareConnection(NodeConnectionInterface $connection)
  104. {
  105. $parameters = $connection->getParameters();
  106. if (isset($parameters->password)) {
  107. $connection->addConnectCommand(
  108. new RawCommand(array('AUTH', $parameters->password))
  109. );
  110. }
  111. if (isset($parameters->database)) {
  112. $connection->addConnectCommand(
  113. new RawCommand(array('SELECT', $parameters->database))
  114. );
  115. }
  116. }
  117. }