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