Factory.php 3.8 KB

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