ConnectionFactory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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;
  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 = array(
  21. 'tcp' => 'Predis\Connection\StreamConnection',
  22. 'unix' => '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\SingleConnectionInterface. 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. * @return mixed
  32. */
  33. protected function checkInitializer($initializer)
  34. {
  35. if (is_callable($initializer)) {
  36. return $initializer;
  37. }
  38. $initializerReflection = new \ReflectionClass($initializer);
  39. if (!$initializerReflection->isSubclassOf('Predis\Connection\SingleConnectionInterface')) {
  40. throw new \InvalidArgumentException(
  41. 'A connection initializer must be a valid connection class or a callable object'
  42. );
  43. }
  44. return $initializer;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function define($scheme, $initializer)
  50. {
  51. $this->schemes[$scheme] = $this->checkInitializer($initializer);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function undefine($scheme)
  57. {
  58. unset($this->schemes[$scheme]);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function create($parameters)
  64. {
  65. if (!$parameters instanceof ConnectionParametersInterface) {
  66. $parameters = new ConnectionParameters($parameters);
  67. }
  68. $scheme = $parameters->scheme;
  69. if (!isset($this->schemes[$scheme])) {
  70. throw new \InvalidArgumentException("Unknown connection scheme: $scheme");
  71. }
  72. $initializer = $this->schemes[$scheme];
  73. if (is_callable($initializer)) {
  74. $connection = call_user_func($initializer, $parameters, $this);
  75. } else {
  76. $connection = new $initializer($parameters);
  77. $this->prepareConnection($connection);
  78. }
  79. if (!$connection instanceof SingleConnectionInterface) {
  80. throw new \InvalidArgumentException(
  81. 'Objects returned by connection initializers must implement ' .
  82. 'Predis\Connection\SingleConnectionInterface'
  83. );
  84. }
  85. return $connection;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function aggregate(AggregatedConnectionInterface $connection, array $parameters)
  91. {
  92. foreach ($parameters as $node) {
  93. $connection->add($node instanceof SingleConnectionInterface ? $node : $this->create($node));
  94. }
  95. }
  96. /**
  97. * Prepares a connection object after its initialization.
  98. *
  99. * @param SingleConnectionInterface $connection Instance of a connection object.
  100. */
  101. protected function prepareConnection(SingleConnectionInterface $connection)
  102. {
  103. $parameters = $connection->getParameters();
  104. if (isset($parameters->password)) {
  105. $command = new Command\RawCommand(array('AUTH', $parameters->password));
  106. $connection->pushInitCommand($command);
  107. }
  108. if (isset($parameters->database)) {
  109. $command = new Command\RawCommand(array('SELECT', $parameters->database));
  110. $connection->pushInitCommand($command);
  111. }
  112. }
  113. }