Factory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Predis\Command\RawCommand;
  12. /**
  13. * Standard connection factory for creating connections to Redis nodes.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. class Factory implements FactoryInterface
  18. {
  19. private $defaults = array();
  20. protected $schemes = array(
  21. 'tcp' => 'Predis\Connection\StreamConnection',
  22. 'unix' => 'Predis\Connection\StreamConnection',
  23. 'tls' => 'Predis\Connection\StreamConnection',
  24. 'redis' => 'Predis\Connection\StreamConnection',
  25. 'rediss' => '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. * @throws \InvalidArgumentException
  36. *
  37. * @return mixed
  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. * Assigns a default set of parameters applied to new connections.
  104. *
  105. * The set of parameters passed to create a new connection have precedence
  106. * over the default values set for the connection factory.
  107. *
  108. * @param array $parameters Set of connection parameters.
  109. */
  110. public function setDefaultParameters(array $parameters)
  111. {
  112. $this->defaults = $parameters;
  113. }
  114. /**
  115. * Returns the default set of parameters applied to new connections.
  116. *
  117. * @return array
  118. */
  119. public function getDefaultParameters()
  120. {
  121. return $this->defaults;
  122. }
  123. /**
  124. * Creates a connection parameters instance from the supplied argument.
  125. *
  126. * @param mixed $parameters Original connection parameters.
  127. *
  128. * @return ParametersInterface
  129. */
  130. protected function createParameters($parameters)
  131. {
  132. if (is_string($parameters)) {
  133. $parameters = Parameters::parse($parameters);
  134. } else {
  135. $parameters = $parameters ?: array();
  136. }
  137. if ($this->defaults) {
  138. $parameters += $this->defaults;
  139. }
  140. return new Parameters($parameters);
  141. }
  142. /**
  143. * Prepares a connection instance after its initialization.
  144. *
  145. * @param NodeConnectionInterface $connection Connection instance.
  146. */
  147. protected function prepareConnection(NodeConnectionInterface $connection)
  148. {
  149. $parameters = $connection->getParameters();
  150. if (isset($parameters->password)) {
  151. $connection->addConnectCommand(
  152. new RawCommand('AUTH', array($parameters->password))
  153. );
  154. }
  155. if (isset($parameters->database)) {
  156. $connection->addConnectCommand(
  157. new RawCommand('SELECT', array($parameters->database))
  158. );
  159. }
  160. }
  161. }