AbstractTransport.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Elastica\Transport;
  3. use Elastica\Connection;
  4. use Elastica\Exception\InvalidException;
  5. use Elastica\Param;
  6. use Elastica\Request;
  7. /**
  8. * Elastica Abstract Transport object.
  9. *
  10. * @author Nicolas Ruflin <spam@ruflin.com>
  11. */
  12. abstract class AbstractTransport extends Param
  13. {
  14. /**
  15. * @var \Elastica\Connection
  16. */
  17. protected $_connection;
  18. /**
  19. * Construct transport.
  20. *
  21. * @param \Elastica\Connection $connection Connection object
  22. */
  23. public function __construct(Connection $connection = null)
  24. {
  25. if ($connection) {
  26. $this->setConnection($connection);
  27. }
  28. }
  29. /**
  30. * @return \Elastica\Connection Connection object
  31. */
  32. public function getConnection()
  33. {
  34. return $this->_connection;
  35. }
  36. /**
  37. * @param \Elastica\Connection $connection Connection object
  38. *
  39. * @return $this
  40. */
  41. public function setConnection(Connection $connection)
  42. {
  43. $this->_connection = $connection;
  44. return $this;
  45. }
  46. /**
  47. * Executes the transport request.
  48. *
  49. * @param \Elastica\Request $request Request object
  50. * @param array $params Hostname, port, path, ...
  51. *
  52. * @return \Elastica\Response Response object
  53. */
  54. abstract public function exec(Request $request, array $params);
  55. /**
  56. * BOOL values true|false should be sanityzed and passed to Elasticsearch
  57. * as string.
  58. *
  59. * @param string $query
  60. *
  61. * @return mixed
  62. */
  63. public function sanityzeQueryStringBool($query)
  64. {
  65. foreach ($query as $key => $value) {
  66. if (is_bool($value)) {
  67. $query[$key] = ($value) ? 'true' : 'false';
  68. }
  69. }
  70. return $query;
  71. }
  72. /**
  73. * Create a transport.
  74. *
  75. * The $transport parameter can be one of the following values:
  76. *
  77. * * string: The short name of a transport. For instance "Http"
  78. * * object: An already instantiated instance of a transport
  79. * * array: An array with a "type" key which must be set to one of the two options. All other
  80. * keys in the array will be set as parameters in the transport instance
  81. *
  82. * @param mixed $transport A transport definition
  83. * @param \Elastica\Connection $connection A connection instance
  84. * @param array $params Parameters for the transport class
  85. *
  86. * @throws \Elastica\Exception\InvalidException
  87. *
  88. * @return AbstractTransport
  89. */
  90. public static function create($transport, Connection $connection, array $params = [])
  91. {
  92. if (is_array($transport) && isset($transport['type'])) {
  93. $transportParams = $transport;
  94. unset($transportParams['type']);
  95. $params = array_replace($params, $transportParams);
  96. $transport = $transport['type'];
  97. }
  98. if (is_string($transport)) {
  99. $specialTransports = [
  100. 'httpadapter' => 'HttpAdapter',
  101. 'nulltransport' => 'NullTransport',
  102. ];
  103. if (isset($specialTransports[strtolower($transport)])) {
  104. $transport = $specialTransports[strtolower($transport)];
  105. } else {
  106. $transport = ucfirst($transport);
  107. }
  108. $classNames = ["Elastica\\Transport\\$transport", $transport];
  109. foreach ($classNames as $className) {
  110. if (class_exists($className)) {
  111. $transport = new $className();
  112. break;
  113. }
  114. }
  115. }
  116. if ($transport instanceof self) {
  117. $transport->setConnection($connection);
  118. foreach ($params as $key => $value) {
  119. $transport->setParam($key, $value);
  120. }
  121. } else {
  122. throw new InvalidException('Invalid transport');
  123. }
  124. return $transport;
  125. }
  126. }