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 | * @package Solarium |
---|
36 | * @subpackage Client |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Base class for all adapters |
---|
41 | * |
---|
42 | * The goal of an adapter is to accept a query, execute it and return the right |
---|
43 | * result object. This is actually quite a complex task as it involves the |
---|
44 | * handling of all Solr communication. |
---|
45 | * |
---|
46 | * The adapter structure allows for varying implementations of this task. |
---|
47 | * |
---|
48 | * Most adapters will use some sort of HTTP client. In that case the |
---|
49 | * Solarium_Client_Request request builders and Solarium_Client_Response |
---|
50 | * response parsers can be used to simplify HTTP communication. |
---|
51 | * See {@link Solarium_Client_Adapter_Http} as an example. |
---|
52 | * |
---|
53 | * However an adapter may also implement all logic by itself if needed. |
---|
54 | * |
---|
55 | * @package Solarium |
---|
56 | * @subpackage Client |
---|
57 | */ |
---|
58 | abstract class Solarium_Client_Adapter extends Solarium_Configurable |
---|
59 | { |
---|
60 | /** |
---|
61 | * Default options |
---|
62 | * |
---|
63 | * The defaults match a standard Solr example instance as distributed by |
---|
64 | * the Apache Lucene Solr project. |
---|
65 | * |
---|
66 | * @var array |
---|
67 | */ |
---|
68 | protected $_options = array( |
---|
69 | 'host' => '127.0.0.1', |
---|
70 | 'port' => 8983, |
---|
71 | 'path' => '/solr', |
---|
72 | 'core' => null, |
---|
73 | 'timeout' => 5, |
---|
74 | ); |
---|
75 | |
---|
76 | /** |
---|
77 | * Initialization hook |
---|
78 | * |
---|
79 | * In this case the path needs to be cleaned of trailing slashes. |
---|
80 | * @see setPath() |
---|
81 | */ |
---|
82 | protected function _init() |
---|
83 | { |
---|
84 | foreach ($this->_options AS $name => $value) { |
---|
85 | switch ($name) { |
---|
86 | case 'path': |
---|
87 | $this->setPath($value); |
---|
88 | break; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Set host option |
---|
95 | * |
---|
96 | * @param string $host This can be a hostname or an IP address |
---|
97 | * @return Solarium_Client Provides fluent interface |
---|
98 | */ |
---|
99 | public function setHost($host) |
---|
100 | { |
---|
101 | return $this->_setOption('host', $host); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Get host option |
---|
106 | * |
---|
107 | * @return string |
---|
108 | */ |
---|
109 | public function getHost() |
---|
110 | { |
---|
111 | return $this->getOption('host'); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Set port option |
---|
116 | * |
---|
117 | * @param int $port Common values are 80, 8080 and 8983 |
---|
118 | * @return Solarium_Client Provides fluent interface |
---|
119 | */ |
---|
120 | public function setPort($port) |
---|
121 | { |
---|
122 | return $this->_setOption('port', $port); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Get port option |
---|
127 | * |
---|
128 | * @return int |
---|
129 | */ |
---|
130 | public function getPort() |
---|
131 | { |
---|
132 | return $this->getOption('port'); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Set path option |
---|
137 | * |
---|
138 | * If the path has a trailing slash it will be removed. |
---|
139 | * |
---|
140 | * @param string $path |
---|
141 | * @return Solarium_Client Provides fluent interface |
---|
142 | */ |
---|
143 | public function setPath($path) |
---|
144 | { |
---|
145 | if (substr($path, -1) == '/') $path = substr($path, 0, -1); |
---|
146 | |
---|
147 | return $this->_setOption('path', $path); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Get path option |
---|
152 | * |
---|
153 | * @return string |
---|
154 | */ |
---|
155 | public function getPath() |
---|
156 | { |
---|
157 | return $this->getOption('path'); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Set core option |
---|
162 | * |
---|
163 | * @param string $core |
---|
164 | * @return Solarium_Client Provides fluent interface |
---|
165 | */ |
---|
166 | public function setCore($core) |
---|
167 | { |
---|
168 | return $this->_setOption('core', $core); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Get core option |
---|
173 | * |
---|
174 | * @return string |
---|
175 | */ |
---|
176 | public function getCore() |
---|
177 | { |
---|
178 | return $this->getOption('core'); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Set timeout option |
---|
183 | * |
---|
184 | * @param int $timeout |
---|
185 | * @return Solarium_Client Provides fluent interface |
---|
186 | */ |
---|
187 | public function setTimeout($timeout) |
---|
188 | { |
---|
189 | return $this->_setOption('timeout', $timeout); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Get timeout option |
---|
194 | * |
---|
195 | * @return string |
---|
196 | */ |
---|
197 | public function getTimeout() |
---|
198 | { |
---|
199 | return $this->getOption('timeout'); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Execute a request |
---|
204 | * |
---|
205 | * Abstract method to require an implementation inside all adapters. |
---|
206 | * |
---|
207 | * @abstract |
---|
208 | * @param Solarium_Client_Request $request |
---|
209 | * @return Solarium_Client_Response |
---|
210 | */ |
---|
211 | abstract public function execute($request); |
---|
212 | |
---|
213 | /** |
---|
214 | * Get the base url for all requests |
---|
215 | * |
---|
216 | * Based on host, path, port and core options. |
---|
217 | * |
---|
218 | * @return string |
---|
219 | */ |
---|
220 | public function getBaseUri() |
---|
221 | { |
---|
222 | $uri = 'http://' . $this->getHost() . ':' . $this->getPort() . $this->getPath() . '/'; |
---|
223 | |
---|
224 | $core = $this->getCore(); |
---|
225 | if (!empty($core)) { |
---|
226 | $uri .= $core.'/'; |
---|
227 | } |
---|
228 | |
---|
229 | return $uri; |
---|
230 | } |
---|
231 | } |
---|