Factory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Command\RawCommand;
  12. /**
  13. * Standard connection factory for creating connections to Redis nodes.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. class Factory implements FactoryInterface
  18. {
  19. protected $schemes = array(
  20. 'tcp' => 'Predis\Connection\StreamConnection',
  21. 'unix' => 'Predis\Connection\StreamConnection',
  22. 'redis' => 'Predis\Connection\StreamConnection',
  23. 'http' => 'Predis\Connection\WebdisConnection',
  24. );
  25. /**
  26. * Checks if the provided argument represents a valid connection class
  27. * implementing Predis\Connection\NodeConnectionInterface. Optionally,
  28. * callable objects are used for lazy initialization of connection objects.
  29. *
  30. * @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
  31. *
  32. * @throws \InvalidArgumentException
  33. *
  34. * @return mixed
  35. */
  36. protected function checkInitializer($initializer)
  37. {
  38. if (is_callable($initializer)) {
  39. return $initializer;
  40. }
  41. $class = new \ReflectionClass($initializer);
  42. if (!$class->isSubclassOf('Predis\Connection\NodeConnectionInterface')) {
  43. throw new \InvalidArgumentException(
  44. 'A connection initializer must be a valid connection class or a callable object.'
  45. );
  46. }
  47. return $initializer;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function define($scheme, $initializer)
  53. {
  54. $this->schemes[$scheme] = $this->checkInitializer($initializer);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function undefine($scheme)
  60. {
  61. unset($this->schemes[$scheme]);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function create($parameters)
  67. {
  68. if (!$parameters instanceof ParametersInterface) {
  69. $parameters = $this->createParameters($parameters);
  70. }
  71. $scheme = $parameters->scheme;
  72. if (!isset($this->schemes[$scheme])) {
  73. throw new \InvalidArgumentException("Unknown connection scheme: '$scheme'.");
  74. }
  75. $initializer = $this->schemes[$scheme];
  76. if (is_callable($initializer)) {
  77. $connection = call_user_func($initializer, $parameters, $this);
  78. } else {
  79. $connection = new $initializer($parameters);
  80. $this->prepareConnection($connection);
  81. }
  82. if (!$connection instanceof NodeConnectionInterface) {
  83. throw new \UnexpectedValueException(
  84. 'Objects returned by connection initializers must implement '.
  85. "'Predis\Connection\NodeConnectionInterface'."
  86. );
  87. }
  88. return $connection;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function aggregate(AggregateConnectionInterface $connection, array $parameters)
  94. {
  95. foreach ($parameters as $node) {
  96. $connection->add($node instanceof NodeConnectionInterface ? $node : $this->create($node));
  97. }
  98. }
  99. /**
  100. * Creates a connection parameters instance from the supplied argument.
  101. *
  102. * @param mixed $parameters Original connection parameters.
  103. *
  104. * @return ParametersInterface
  105. */
  106. protected function createParameters($parameters)
  107. {
  108. return Parameters::create($parameters);
  109. }
  110. /**
  111. * Prepares a connection instance after its initialization.
  112. *
  113. * @param NodeConnectionInterface $connection Connection instance.
  114. */
  115. protected function prepareConnection(NodeConnectionInterface $connection)
  116. {
  117. $parameters = $connection->getParameters();
  118. if (isset($parameters->password)) {
  119. $connection->addConnectCommand(
  120. new RawCommand(array('AUTH', $parameters->password))
  121. );
  122. }
  123. if (isset($parameters->database)) {
  124. $connection->addConnectCommand(
  125. new RawCommand(array('SELECT', $parameters->database))
  126. );
  127. }
  128. }
  129. }