Factory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 'redis' => 'Predis\Connection\StreamConnection',
  26. 'http' => 'Predis\Connection\WebdisConnection',
  27. );
  28. /**
  29. * Checks if the provided argument represents a valid connection class
  30. * implementing Predis\Connection\NodeConnectionInterface. Optionally,
  31. * callable objects are used for lazy initialization of connection objects.
  32. *
  33. * @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
  34. *
  35. * @return mixed
  36. *
  37. * @throws \InvalidArgumentException
  38. */
  39. protected function checkInitializer($initializer)
  40. {
  41. if (is_callable($initializer)) {
  42. return $initializer;
  43. }
  44. $class = new ReflectionClass($initializer);
  45. if (!$class->isSubclassOf('Predis\Connection\NodeConnectionInterface')) {
  46. throw new InvalidArgumentException(
  47. 'A connection initializer must be a valid connection class or a callable object.'
  48. );
  49. }
  50. return $initializer;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function define($scheme, $initializer)
  56. {
  57. $this->schemes[$scheme] = $this->checkInitializer($initializer);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function undefine($scheme)
  63. {
  64. unset($this->schemes[$scheme]);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function create($parameters)
  70. {
  71. if (!$parameters instanceof ParametersInterface) {
  72. $parameters = $this->createParameters($parameters);
  73. }
  74. $scheme = $parameters->scheme;
  75. if (!isset($this->schemes[$scheme])) {
  76. throw new InvalidArgumentException("Unknown connection scheme: '$scheme'.");
  77. }
  78. $initializer = $this->schemes[$scheme];
  79. if (is_callable($initializer)) {
  80. $connection = call_user_func($initializer, $parameters, $this);
  81. } else {
  82. $connection = new $initializer($parameters);
  83. $this->prepareConnection($connection);
  84. }
  85. if (!$connection instanceof NodeConnectionInterface) {
  86. throw new UnexpectedValueException(
  87. "Objects returned by connection initializers must implement ".
  88. "'Predis\Connection\NodeConnectionInterface'."
  89. );
  90. }
  91. return $connection;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function aggregate(AggregateConnectionInterface $connection, array $parameters)
  97. {
  98. foreach ($parameters as $node) {
  99. $connection->add($node instanceof NodeConnectionInterface ? $node : $this->create($node));
  100. }
  101. }
  102. /**
  103. * Creates a connection parameters instance from the supplied argument.
  104. *
  105. * @param mixed $parameters Original connection parameters.
  106. *
  107. * @return ParametersInterface
  108. */
  109. protected function createParameters($parameters)
  110. {
  111. return Parameters::create($parameters);
  112. }
  113. /**
  114. * Prepares a connection instance after its initialization.
  115. *
  116. * @param NodeConnectionInterface $connection Connection instance.
  117. */
  118. protected function prepareConnection(NodeConnectionInterface $connection)
  119. {
  120. $parameters = $connection->getParameters();
  121. if (isset($parameters->password)) {
  122. $connection->addConnectCommand(
  123. new RawCommand(array('AUTH', $parameters->password))
  124. );
  125. }
  126. if (isset($parameters->database)) {
  127. $connection->addConnectCommand(
  128. new RawCommand(array('SELECT', $parameters->database))
  129. );
  130. }
  131. }
  132. }