ConnectionFactory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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;
  11. use Predis\Network\IConnectionSingle;
  12. /**
  13. * Provides a default factory for Redis connections that maps URI schemes
  14. * to connection classes implementing the Predis\Network\IConnectionSingle
  15. * interface.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class ConnectionFactory implements IConnectionFactory
  20. {
  21. private static $globalSchemes;
  22. private $instanceSchemes = array();
  23. /**
  24. * @param array $schemesMap Map of URI schemes to connection classes.
  25. */
  26. public function __construct(Array $schemesMap = null)
  27. {
  28. $this->instanceSchemes = self::ensureDefaultSchemes();
  29. if (isset($schemesMap)) {
  30. foreach ($schemesMap as $scheme => $initializer) {
  31. $this->defineConnection($scheme, $initializer);
  32. }
  33. }
  34. }
  35. /**
  36. * Checks if the provided argument represents a valid connection class
  37. * implementing the Predis\Network\IConnectionSingle interface. Optionally,
  38. * callable objects are used for lazy initialization of connection objects.
  39. *
  40. * @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
  41. */
  42. private static function checkConnectionInitializer($initializer)
  43. {
  44. if (is_callable($initializer)) {
  45. return;
  46. }
  47. $initializerReflection = new \ReflectionClass($initializer);
  48. if (!$initializerReflection->isSubclassOf('\Predis\Network\IConnectionSingle')) {
  49. throw new \InvalidArgumentException(
  50. 'A connection initializer must be a valid connection class or a callable object'
  51. );
  52. }
  53. }
  54. /**
  55. * Ensures that the default global URI schemes map is initialized.
  56. *
  57. * @return array
  58. */
  59. private static function ensureDefaultSchemes()
  60. {
  61. if (!isset(self::$globalSchemes)) {
  62. self::$globalSchemes = array(
  63. 'tcp' => '\Predis\Network\StreamConnection',
  64. 'unix' => '\Predis\Network\StreamConnection',
  65. );
  66. }
  67. return self::$globalSchemes;
  68. }
  69. /**
  70. * Defines a new URI scheme => connection class relation at class level.
  71. *
  72. * @param string $scheme URI scheme
  73. * @param mixed $connectionInitializer FQN of a connection class or a callable for lazy initialization.
  74. */
  75. public static function define($scheme, $connectionInitializer)
  76. {
  77. self::ensureDefaultSchemes();
  78. self::checkConnectionInitializer($connectionInitializer);
  79. self::$globalSchemes[$scheme] = $connectionInitializer;
  80. }
  81. /**
  82. * Defines a new URI scheme => connection class relation at instance level.
  83. *
  84. * @param string $scheme URI scheme
  85. * @param mixed $connectionInitializer FQN of a connection class or a callable for lazy initialization.
  86. */
  87. public function defineConnection($scheme, $connectionInitializer)
  88. {
  89. self::checkConnectionInitializer($connectionInitializer);
  90. $this->instanceSchemes[$scheme] = $connectionInitializer;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function create($parameters)
  96. {
  97. if (!$parameters instanceof IConnectionParameters) {
  98. $parameters = new ConnectionParameters($parameters);
  99. }
  100. $scheme = $parameters->scheme;
  101. if (!isset($this->instanceSchemes[$scheme])) {
  102. throw new \InvalidArgumentException("Unknown connection scheme: $scheme");
  103. }
  104. $initializer = $this->instanceSchemes[$scheme];
  105. if (!is_callable($initializer)) {
  106. return new $initializer($parameters);
  107. }
  108. $connection = call_user_func($initializer, $parameters);
  109. if (!$connection instanceof IConnectionSingle) {
  110. throw new \InvalidArgumentException(
  111. 'Objects returned by connection initializers must implement ' .
  112. 'the Predis\Network\IConnectionSingle interface'
  113. );
  114. }
  115. return $connection;
  116. }
  117. }