ConnectionFactory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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;
  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 Profile\ProfileInterface $profile Server profile used to initialize new connections.
  26. */
  27. public function __construct(Profile\ProfileInterface $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);
  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 aggregate(AggregatedConnectionInterface $connection, array $parameters)
  111. {
  112. foreach ($parameters as $node) {
  113. $connection->add($node instanceof SingleConnectionInterface ? $node : $this->create($node));
  114. }
  115. }
  116. /**
  117. * Prepares a connection object after its initialization.
  118. *
  119. * @param SingleConnectionInterface $connection Instance of a connection object.
  120. */
  121. protected function prepareConnection(SingleConnectionInterface $connection)
  122. {
  123. if (isset($this->profile)) {
  124. $parameters = $connection->getParameters();
  125. if (isset($parameters->password)) {
  126. $command = $this->profile->createCommand('auth', array($parameters->password));
  127. $connection->pushInitCommand($command);
  128. }
  129. if (isset($parameters->database)) {
  130. $command = $this->profile->createCommand('select', array($parameters->database));
  131. $connection->pushInitCommand($command);
  132. }
  133. }
  134. }
  135. /**
  136. * Sets the server profile used to create initialization commands for connections.
  137. *
  138. * @param Profile\ProfileInterface $profile Server profile instance.
  139. */
  140. public function setProfile(Profile\ProfileInterface $profile)
  141. {
  142. $this->profile = $profile;
  143. }
  144. /**
  145. * Returns the server profile used to create initialization commands for connections.
  146. *
  147. * @return Profile\ProfileInterface
  148. */
  149. public function getProfile()
  150. {
  151. return $this->profile;
  152. }
  153. }