PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Solarium/Core/Client/Endpoint.php

http://github.com/basdenooijer/solarium
PHP | 305 lines | 117 code | 27 blank | 161 comment | 4 complexity | 54230693f32ba1de9d7d66b6efc0d3e6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright 2011 Bas de Nooijer. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this listof conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * The views and conclusions contained in the software and documentation are
  28. * those of the authors and should not be interpreted as representing official
  29. * policies, either expressed or implied, of the copyright holder.
  30. *
  31. * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
  32. * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
  33. * @link http://www.solarium-project.org/
  34. */
  35. /**
  36. * @namespace
  37. */
  38. namespace Solarium\Core\Client;
  39. use Solarium\Core\Configurable;
  40. /**
  41. * Class for describing an endpoint
  42. */
  43. class Endpoint extends Configurable
  44. {
  45. /**
  46. * Default options
  47. *
  48. * The defaults match a standard Solr example instance as distributed by
  49. * the Apache Lucene Solr project.
  50. *
  51. * @var array
  52. */
  53. protected $options = array(
  54. 'scheme' => 'http',
  55. 'host' => '127.0.0.1',
  56. 'port' => 8983,
  57. 'path' => '/solr',
  58. 'core' => null,
  59. 'timeout' => 5,
  60. );
  61. /**
  62. * Initialization hook
  63. *
  64. * In this case the path needs to be cleaned of trailing slashes.
  65. * @see setPath()
  66. */
  67. protected function init()
  68. {
  69. foreach ($this->options as $name => $value) {
  70. switch ($name) {
  71. case 'path':
  72. $this->setPath($value);
  73. break;
  74. }
  75. }
  76. }
  77. /**
  78. * Get key value
  79. *
  80. * @return string
  81. */
  82. public function getKey()
  83. {
  84. return $this->getOption('key');
  85. }
  86. /**
  87. * Set key value
  88. *
  89. * @param string $value
  90. * @return self Provides fluent interface
  91. */
  92. public function setKey($value)
  93. {
  94. return $this->setOption('key', $value);
  95. }
  96. /**
  97. * Set host option
  98. *
  99. * @param string $host This can be a hostname or an IP address
  100. * @return self Provides fluent interface
  101. */
  102. public function setHost($host)
  103. {
  104. return $this->setOption('host', $host);
  105. }
  106. /**
  107. * Get host option
  108. *
  109. * @return string
  110. */
  111. public function getHost()
  112. {
  113. return $this->getOption('host');
  114. }
  115. /**
  116. * Set port option
  117. *
  118. * @param int $port Common values are 80, 8080 and 8983
  119. * @return self Provides fluent interface
  120. */
  121. public function setPort($port)
  122. {
  123. return $this->setOption('port', $port);
  124. }
  125. /**
  126. * Get port option
  127. *
  128. * @return int
  129. */
  130. public function getPort()
  131. {
  132. return $this->getOption('port');
  133. }
  134. /**
  135. * Set path option
  136. *
  137. * If the path has a trailing slash it will be removed.
  138. *
  139. * @param string $path
  140. * @return self Provides fluent interface
  141. */
  142. public function setPath($path)
  143. {
  144. if (substr($path, -1) == '/') {
  145. $path = substr($path, 0, -1);
  146. }
  147. return $this->setOption('path', $path);
  148. }
  149. /**
  150. * Get path option
  151. *
  152. * @return string
  153. */
  154. public function getPath()
  155. {
  156. return $this->getOption('path');
  157. }
  158. /**
  159. * Set core option
  160. *
  161. * @param string $core
  162. * @return self Provides fluent interface
  163. */
  164. public function setCore($core)
  165. {
  166. return $this->setOption('core', $core);
  167. }
  168. /**
  169. * Get core option
  170. *
  171. * @return string
  172. */
  173. public function getCore()
  174. {
  175. return $this->getOption('core');
  176. }
  177. /**
  178. * Set timeout option
  179. *
  180. * @param int $timeout
  181. * @return self Provides fluent interface
  182. */
  183. public function setTimeout($timeout)
  184. {
  185. return $this->setOption('timeout', $timeout);
  186. }
  187. /**
  188. * Get timeout option
  189. *
  190. * @return string
  191. */
  192. public function getTimeout()
  193. {
  194. return $this->getOption('timeout');
  195. }
  196. /**
  197. * Set scheme option
  198. *
  199. * @param string $scheme
  200. * @return self Provides fluent interface
  201. */
  202. public function setScheme($scheme)
  203. {
  204. return $this->setOption('scheme', $scheme);
  205. }
  206. /**
  207. * Get scheme option
  208. *
  209. * @return string
  210. */
  211. public function getScheme()
  212. {
  213. return $this->getOption('scheme');
  214. }
  215. /**
  216. * Get the base url for all requests
  217. *
  218. * Based on host, path, port and core options.
  219. *
  220. * @return string
  221. */
  222. public function getBaseUri()
  223. {
  224. $uri = $this->getScheme() . '://' . $this->getHost() . ':' . $this->getPort() . $this->getPath() . '/';
  225. $core = $this->getCore();
  226. if (!empty($core)) {
  227. $uri .= $core.'/';
  228. }
  229. return $uri;
  230. }
  231. /**
  232. * Set HTTP basic auth settings
  233. *
  234. * If one or both values are NULL authentication will be disabled
  235. *
  236. * @param string $username
  237. * @param string $password
  238. * @return self Provides fluent interface
  239. */
  240. public function setAuthentication($username, $password)
  241. {
  242. $this->setOption('username', $username);
  243. $this->setOption('password', $password);
  244. return $this;
  245. }
  246. /**
  247. * Get HTTP basic auth settings
  248. *
  249. * @return array
  250. */
  251. public function getAuthentication()
  252. {
  253. return array(
  254. 'username' => $this->getOption('username'),
  255. 'password' => $this->getOption('password'),
  256. );
  257. }
  258. /**
  259. * Magic method enables a object to be transformed to a string
  260. *
  261. * Get a summary showing significant variables in the object
  262. * note: uri resource is decoded for readability
  263. *
  264. * @return string
  265. */
  266. public function __toString()
  267. {
  268. $output = __CLASS__ . '::__toString' . "\n"
  269. . 'base uri: ' . $this->getBaseUri() . "\n"
  270. . 'host: ' . $this->getHost() . "\n"
  271. . 'port: ' . $this->getPort() ."\n"
  272. . 'path: ' . $this->getPath() ."\n"
  273. . 'core: ' . $this->getCore() . "\n"
  274. . 'timeout: ' . $this->getTimeout() . "\n"
  275. . 'authentication: ' . print_r($this->getAuthentication(), 1);
  276. return $output;
  277. }
  278. }