ConnectionFactory.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Profile\ServerProfile;
  12. use Predis\Profile\ServerProfileInterface;
  13. /**
  14. * Provides a default factory for Redis connections that maps URI schemes
  15. * to connection classes implementing Predis\Connection\SingleConnectionInterface.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class ConnectionFactory implements ConnectionFactoryInterface
  20. {
  21. protected $schemes;
  22. protected $profile;
  23. /**
  24. * Initializes a new instance of the default connection factory class used by Predis.
  25. *
  26. * @param ServerProfileInterface $profile Server profile used to initialize new connections.
  27. */
  28. public function __construct(ServerProfileInterface $profile = null)
  29. {
  30. $this->schemes = $this->getDefaultSchemes();
  31. $this->profile = $profile;
  32. }
  33. /**
  34. * Returns a named array that maps URI schemes to connection classes.
  35. *
  36. * @return array Map of URI schemes and connection classes.
  37. */
  38. protected function getDefaultSchemes()
  39. {
  40. return array(
  41. 'tcp' => 'Predis\Connection\StreamConnection',
  42. 'unix' => 'Predis\Connection\StreamConnection',
  43. 'http' => 'Predis\Connection\WebdisConnection',
  44. );
  45. }
  46. /**
  47. * Checks if the provided argument represents a valid connection class
  48. * implementing Predis\Connection\SingleConnectionInterface. Optionally,
  49. * callable objects are used for lazy initialization of connection objects.
  50. *
  51. * @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
  52. * @return mixed
  53. */
  54. protected function checkInitializer($initializer)
  55. {
  56. if (is_callable($initializer)) {
  57. return $initializer;
  58. }
  59. $initializerReflection = new \ReflectionClass($initializer);
  60. if (!$initializerReflection->isSubclassOf('Predis\Connection\SingleConnectionInterface')) {
  61. throw new \InvalidArgumentException(
  62. 'A connection initializer must be a valid connection class or a callable object'
  63. );
  64. }
  65. return $initializer;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function define($scheme, $initializer)
  71. {
  72. $this->schemes[$scheme] = $this->checkInitializer($initializer);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function undefine($scheme)
  78. {
  79. unset($this->schemes[$scheme]);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function create($parameters)
  85. {
  86. if (!$parameters instanceof ConnectionParametersInterface) {
  87. $parameters = new ConnectionParameters($parameters ?: array());
  88. }
  89. $scheme = $parameters->scheme;
  90. if (!isset($this->schemes[$scheme])) {
  91. throw new \InvalidArgumentException("Unknown connection scheme: $scheme");
  92. }
  93. $initializer = $this->schemes[$scheme];
  94. if (is_callable($initializer)) {
  95. $connection = call_user_func($initializer, $parameters, $this);
  96. } else {
  97. $connection = new $initializer($parameters);
  98. $this->prepareConnection($connection);
  99. }
  100. if (!$connection instanceof SingleConnectionInterface) {
  101. throw new \InvalidArgumentException(
  102. 'Objects returned by connection initializers must implement ' .
  103. 'Predis\Connection\SingleConnectionInterface'
  104. );
  105. }
  106. return $connection;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function createAggregated(AggregatedConnectionInterface $connection, Array $parameters)
  112. {
  113. foreach ($parameters as $node) {
  114. $connection->add($node instanceof SingleConnectionInterface ? $node : $this->create($node));
  115. }
  116. return $connection;
  117. }
  118. /**
  119. * Prepares a connection object after its initialization.
  120. *
  121. * @param SingleConnectionInterface $connection Instance of a connection object.
  122. */
  123. protected function prepareConnection(SingleConnectionInterface $connection)
  124. {
  125. if (isset($this->profile)) {
  126. $parameters = $connection->getParameters();
  127. if (isset($parameters->password)) {
  128. $command = $this->profile->createCommand('auth', array($parameters->password));
  129. $connection->pushInitCommand($command);
  130. }
  131. if (isset($parameters->database)) {
  132. $command = $this->profile->createCommand('select', array($parameters->database));
  133. $connection->pushInitCommand($command);
  134. }
  135. }
  136. }
  137. /**
  138. * Sets the server profile used to create initialization commands for connections.
  139. *
  140. * @param ServerProfileInterface $profile Server profile instance.
  141. */
  142. public function setProfile(ServerProfileInterface $profile)
  143. {
  144. $this->profile = $profile;
  145. }
  146. /**
  147. * Returns the server profile used to create initialization commands for connections.
  148. *
  149. * @return ServerProfileInterface
  150. */
  151. public function getProfile()
  152. {
  153. return $this->profile;
  154. }
  155. }