ConnectionFactory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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;
  11. use Predis\Profile\ServerProfileInterface;
  12. use Predis\Connection\SingleConnectionInterface;
  13. use Predis\Connection\ClusterConnectionInterface;
  14. use Predis\Connection\ReplicationConnectionInterface;
  15. use Predis\Profile\ServerProfile;
  16. /**
  17. * Provides a default factory for Redis connections that maps URI schemes
  18. * to connection classes implementing Predis\Connection\SingleConnectionInterface.
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class ConnectionFactory implements ConnectionFactoryInterface
  23. {
  24. private $schemes;
  25. /**
  26. * Initializes a new instance of the default connection factory class used by Predis.
  27. */
  28. public function __construct()
  29. {
  30. $this->schemes = $this->getDefaultSchemes();
  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, ServerProfileInterface $profile = null)
  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 = new $initializer($parameters);
  95. $this->prepareConnection($connection, $profile ?: ServerProfile::getDefault());
  96. return $connection;
  97. }
  98. $connection = call_user_func($initializer, $parameters, $profile);
  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 createCluster(ClusterConnectionInterface $cluster, $parameters, ServerProfileInterface $profile = null)
  111. {
  112. foreach ($parameters as $node) {
  113. $cluster->add($node instanceof SingleConnectionInterface ? $node : $this->create($node, $profile));
  114. }
  115. return $cluster;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function createReplication(ReplicationConnectionInterface $replication, $parameters, ServerProfileInterface $profile = null)
  121. {
  122. foreach ($parameters as $node) {
  123. $replication->add($node instanceof SingleConnectionInterface ? $node : $this->create($node, $profile));
  124. }
  125. return $replication;
  126. }
  127. /**
  128. * Prepares a connection object after its initialization.
  129. *
  130. * @param SingleConnectionInterface $connection Instance of a connection object.
  131. * @param ServerProfileInterface $profile $connection Instance of a connection object.
  132. */
  133. protected function prepareConnection(SingleConnectionInterface $connection, ServerProfileInterface $profile)
  134. {
  135. $parameters = $connection->getParameters();
  136. if (isset($parameters->password)) {
  137. $command = $profile->createCommand('auth', array($parameters->password));
  138. $connection->pushInitCommand($command);
  139. }
  140. if (isset($parameters->database)) {
  141. $command = $profile->createCommand('select', array($parameters->database));
  142. $connection->pushInitCommand($command);
  143. }
  144. }
  145. }