Settings.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Elastica\Cluster;
  3. use Elastica\Client;
  4. use Elastica\Request;
  5. /**
  6. * Cluster settings.
  7. *
  8. * @author Nicolas Ruflin <spam@ruflin.com>
  9. *
  10. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-update-settings.html
  11. */
  12. class Settings
  13. {
  14. /**
  15. * @var \Elastica\Client Client object
  16. */
  17. protected $_client = null;
  18. /**
  19. * Creates a cluster object.
  20. *
  21. * @param \Elastica\Client $client Connection client object
  22. */
  23. public function __construct(Client $client)
  24. {
  25. $this->_client = $client;
  26. }
  27. /**
  28. * Returns settings data.
  29. *
  30. * @return array Settings data (persistent and transient)
  31. */
  32. public function get()
  33. {
  34. return $this->request()->getData();
  35. }
  36. /**
  37. * Returns the current persistent settings of the cluster.
  38. *
  39. * If param is set, only specified setting is return.
  40. *
  41. * @param string $setting OPTIONAL Setting name to return
  42. *
  43. * @return array|string|null Settings data
  44. */
  45. public function getPersistent($setting = '')
  46. {
  47. $data = $this->get();
  48. $settings = $data['persistent'];
  49. if (!empty($setting)) {
  50. if (isset($settings[$setting])) {
  51. return $settings[$setting];
  52. }
  53. return;
  54. }
  55. return $settings;
  56. }
  57. /**
  58. * Returns the current transient settings of the cluster.
  59. *
  60. * If param is set, only specified setting is return.
  61. *
  62. * @param string $setting OPTIONAL Setting name to return
  63. *
  64. * @return array|string|null Settings data
  65. */
  66. public function getTransient($setting = '')
  67. {
  68. $data = $this->get();
  69. $settings = $data['transient'];
  70. if (!empty($setting)) {
  71. if (isset($settings[$setting])) {
  72. return $settings[$setting];
  73. }
  74. if (false !== strpos($setting, '.')) {
  75. // convert dot notation to nested arrays
  76. $keys = explode('.', $setting);
  77. foreach ($keys as $key) {
  78. if (isset($settings[$key])) {
  79. $settings = $settings[$key];
  80. } else {
  81. return;
  82. }
  83. }
  84. return $settings;
  85. }
  86. return;
  87. }
  88. return $settings;
  89. }
  90. /**
  91. * Sets persistent setting.
  92. *
  93. * @param string $key
  94. * @param string $value
  95. *
  96. * @return \Elastica\Response
  97. */
  98. public function setPersistent($key, $value)
  99. {
  100. return $this->set(
  101. [
  102. 'persistent' => [
  103. $key => $value,
  104. ],
  105. ]
  106. );
  107. }
  108. /**
  109. * Sets transient settings.
  110. *
  111. * @param string $key
  112. * @param string $value
  113. *
  114. * @return \Elastica\Response
  115. */
  116. public function setTransient($key, $value)
  117. {
  118. return $this->set(
  119. [
  120. 'transient' => [
  121. $key => $value,
  122. ],
  123. ]
  124. );
  125. }
  126. /**
  127. * Sets the cluster to read only.
  128. *
  129. * Second param can be used to set it persistent
  130. *
  131. * @param bool $readOnly
  132. * @param bool $persistent
  133. *
  134. * @return \Elastica\Response $response
  135. */
  136. public function setReadOnly($readOnly = true, $persistent = false)
  137. {
  138. $key = 'cluster.blocks.read_only';
  139. return $persistent
  140. ? $this->setPersistent($key, $readOnly)
  141. : $this->setTransient($key, $readOnly);
  142. }
  143. /**
  144. * Set settings for cluster.
  145. *
  146. * @param array $settings Raw settings (including persistent or transient)
  147. *
  148. * @return \Elastica\Response
  149. */
  150. public function set(array $settings)
  151. {
  152. return $this->request($settings, Request::PUT);
  153. }
  154. /**
  155. * Get the client.
  156. *
  157. * @return \Elastica\Client
  158. */
  159. public function getClient()
  160. {
  161. return $this->_client;
  162. }
  163. /**
  164. * Sends settings request.
  165. *
  166. * @param array $data OPTIONAL Data array
  167. * @param string $method OPTIONAL Transfer method (default = \Elastica\Request::GET)
  168. *
  169. * @return \Elastica\Response Response object
  170. */
  171. public function request(array $data = [], $method = Request::GET)
  172. {
  173. $path = '_cluster/settings';
  174. return $this->getClient()->request($path, $method, $data);
  175. }
  176. }