ConnectionFactory.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Profiles\IServerProfile;
  12. use Predis\Network\IConnectionSingle;
  13. use Predis\Network\IConnectionCluster;
  14. use Predis\Network\IConnectionReplication;
  15. use Predis\Profiles\ServerProfile;
  16. /**
  17. * Provides a default factory for Redis connections that maps URI schemes
  18. * to connection classes implementing the Predis\Network\IConnectionSingle
  19. * interface.
  20. *
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class ConnectionFactory implements IConnectionFactory
  24. {
  25. private $schemes;
  26. /**
  27. * Initializes a new instance of the default connection factory class used by Predis.
  28. */
  29. public function __construct()
  30. {
  31. $this->schemes = $this->getDefaultSchemes();
  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\Network\StreamConnection',
  42. 'unix' => 'Predis\Network\StreamConnection',
  43. 'http' => 'Predis\Network\WebdisConnection',
  44. );
  45. }
  46. /**
  47. * Checks if the provided argument represents a valid connection class
  48. * implementing the Predis\Network\IConnectionSingle interface. 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\Network\IConnectionSingle')) {
  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, IServerProfile $profile = null)
  85. {
  86. if (!$parameters instanceof IConnectionParameters) {
  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 = new $initializer($parameters);
  96. $this->prepareConnection($connection, $profile ?: ServerProfile::getDefault());
  97. return $connection;
  98. }
  99. $connection = call_user_func($initializer, $parameters, $profile);
  100. if (!$connection instanceof IConnectionSingle) {
  101. throw new \InvalidArgumentException(
  102. 'Objects returned by connection initializers must implement ' .
  103. 'the Predis\Network\IConnectionSingle interface'
  104. );
  105. }
  106. return $connection;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function createCluster(IConnectionCluster $cluster, $parameters, IServerProfile $profile = null)
  112. {
  113. foreach ($parameters as $node) {
  114. $cluster->add($node instanceof IConnectionSingle ? $node : $this->create($node, $profile));
  115. }
  116. return $cluster;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function createReplication(IConnectionReplication $replication, $parameters, IServerProfile $profile = null)
  122. {
  123. foreach ($parameters as $node) {
  124. $replication->add($node instanceof IConnectionSingle ? $node : $this->create($node, $profile));
  125. }
  126. return $replication;
  127. }
  128. /**
  129. * Prepares a connection object after its initialization.
  130. *
  131. * @param IConnectionSingle $connection Instance of a connection object.
  132. * @param IServerProfile $profile $connection Instance of a connection object.
  133. */
  134. protected function prepareConnection(IConnectionSingle $connection, IServerProfile $profile)
  135. {
  136. $parameters = $connection->getParameters();
  137. if (isset($parameters->password)) {
  138. $command = $profile->createCommand('auth', array($parameters->password));
  139. $connection->pushInitCommand($command);
  140. }
  141. if (isset($parameters->database)) {
  142. $command = $profile->createCommand('select', array($parameters->database));
  143. $connection->pushInitCommand($command);
  144. }
  145. }
  146. }