ConnectionFactory.php 5.2 KB

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