ConnectionFactory.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Profiles\ServerProfile;
  15. /**
  16. * Provides a default factory for Redis connections that maps URI schemes
  17. * to connection classes implementing the Predis\Network\IConnectionSingle
  18. * interface.
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class ConnectionFactory implements IConnectionFactory
  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\Network\StreamConnection',
  41. 'unix' => 'Predis\Network\StreamConnection',
  42. 'http' => 'Predis\Network\WebdisConnection',
  43. );
  44. }
  45. /**
  46. * Checks if the provided argument represents a valid connection class
  47. * implementing the Predis\Network\IConnectionSingle interface. 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\Network\IConnectionSingle')) {
  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, IServerProfile $profile = null)
  84. {
  85. if (!$parameters instanceof IConnectionParameters) {
  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 IConnectionSingle) {
  100. throw new \InvalidArgumentException(
  101. 'Objects returned by connection initializers must implement ' .
  102. 'the Predis\Network\IConnectionSingle interface'
  103. );
  104. }
  105. return $connection;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function createCluster(IConnectionCluster $cluster, $parameters, IServerProfile $profile = null)
  111. {
  112. foreach ($parameters as $node) {
  113. $cluster->add($node instanceof IConnectionSingle ? $node : $this->create($node, $profile));
  114. }
  115. return $cluster;
  116. }
  117. /**
  118. * Prepares a connection object after its initialization.
  119. *
  120. * @param IConnectionSingle $connection Instance of a connection object.
  121. * @param IServerProfile $profile $connection Instance of a connection object.
  122. */
  123. protected function prepareConnection(IConnectionSingle $connection, IServerProfile $profile)
  124. {
  125. $parameters = $connection->getParameters();
  126. if (isset($parameters->password)) {
  127. $command = $profile->createCommand('auth', array($parameters->password));
  128. $connection->pushInitCommand($command);
  129. }
  130. if (isset($parameters->database)) {
  131. $command = $profile->createCommand('select', array($parameters->database));
  132. $connection->pushInitCommand($command);
  133. }
  134. }
  135. }