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
+
32
+ /**
33
+ * The Solarium Client is the main accesspoint for interaction with Solr
34
+ */
35
+ class Solarium_Client extends Solarium_Configurable
36
+ {
37
+
38
+ /**
39
+ * Default options
40
+ *
41
+ * @var array
42
+ */
43
+ protected $ _options = array (
44
+ 'host ' => '127.0.0.1 ' ,
45
+ 'port ' => 80 ,
46
+ 'path ' => '/solr ' ,
47
+ 'core ' => null ,
48
+ 'adapter ' => 'Solarium_Client_Adapter_Stream ' ,
49
+ );
50
+
51
+ /**
52
+ * Adapter instance
53
+ *
54
+ * @var Solarium_Client_Adapter
55
+ */
56
+ protected $ _adapter ;
57
+
58
+ /**
59
+ * Init options array. Some options might need some extra checks or setup
60
+ * work.
61
+ *
62
+ * @return void
63
+ */
64
+ protected function _init ()
65
+ {
66
+ foreach ($ this ->_options AS $ name => $ value ) {
67
+ switch ($ name ) {
68
+ case 'path ' :
69
+ $ this ->setPath ($ value );
70
+ break ;
71
+ }
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Set an option
77
+ *
78
+ * @param string $name
79
+ * @param mixed $value
80
+ * @return void
81
+ */
82
+ protected function _setOption ($ name , $ value )
83
+ {
84
+ parent ::_setOption ($ name , $ value );
85
+
86
+ if (null !== $ this ->_adapter ) {
87
+ $ this ->_adapter ->setOptions ($ this ->_options );
88
+ }
89
+
90
+ return $ this ;
91
+ }
92
+
93
+ /**
94
+ * Set host option
95
+ *
96
+ * @param string $host
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
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
+ * @return int
128
+ */
129
+ public function getPort ()
130
+ {
131
+ return $ this ->getOption ('port ' );
132
+ }
133
+
134
+ /**
135
+ * Set path option
136
+ *
137
+ * @param string $path
138
+ * @return Solarium_Client Provides fluent interface
139
+ */
140
+ public function setPath ($ path )
141
+ {
142
+ // remove trailing slashes
143
+ if (substr ($ path , -1 ) == '/ ' ) $ path = substr ($ path , 0 , -1 );
144
+
145
+ return $ this ->_setOption ('path ' , $ path );
146
+ }
147
+
148
+ /**
149
+ * Get path option
150
+ *
151
+ * @return void
152
+ */
153
+ public function getPath ()
154
+ {
155
+ return $ this ->getOption ('path ' );
156
+ }
157
+
158
+ /**
159
+ * Set core option
160
+ *
161
+ * @param string $core
162
+ * @return Solarium_Client Provides fluent interface
163
+ */
164
+ public function setCore ($ core )
165
+ {
166
+ return $ this ->_setOption ('core ' , $ core );
167
+ }
168
+
169
+ /**
170
+ * Get core option
171
+ *
172
+ * @return string
173
+ */
174
+ public function getCore ()
175
+ {
176
+ return $ this ->getOption ('core ' );
177
+ }
178
+
179
+ /**
180
+ * Set the adapter
181
+ *
182
+ * @param string|Solarium_Client_Adapter $adapter
183
+ * @return Solarium_Client Provides fluent interface
184
+ */
185
+ public function setAdapter ($ adapter )
186
+ {
187
+ if (is_string ($ adapter )) {
188
+ $ adapter = new $ adapter ;
189
+ }
190
+
191
+ $ adapter ->setOptions ($ this ->_options );
192
+ $ this ->_adapter = $ adapter ;
193
+ return $ this ;
194
+ }
195
+
196
+ /**
197
+ * Get the adapter instance
198
+ *
199
+ * @return Solarium_Client_Adapter
200
+ */
201
+ public function getAdapter ()
202
+ {
203
+ if (null === $ this ->_adapter ) {
204
+ $ this ->setAdapter ($ this ->_options ['adapter ' ]);
205
+ }
206
+
207
+ return $ this ->_adapter ;
208
+ }
209
+
210
+ /**
211
+ * Execute a ping query
212
+ *
213
+ * @param Solarium_Query_Ping $query
214
+ * @return Solarium_Result_Ping
215
+ */
216
+ public function ping ($ query )
217
+ {
218
+ return $ this ->getAdapter ()->ping ($ query );
219
+ }
220
+
221
+ /**
222
+ * Execute an update query
223
+ *
224
+ * @param Solarium_Query_Update $query
225
+ * @return Solarium_Result_Select
226
+ */
227
+ public function update ($ query )
228
+ {
229
+ return $ this ->getAdapter ()->update ($ query );
230
+ }
231
+
232
+ /**
233
+ * Execute a select query
234
+ *
235
+ * @param Solarium_Query_Select $query
236
+ * @return Solarium_Result_Select
237
+ */
238
+ public function select ($ query )
239
+ {
240
+ return $ this ->getAdapter ()->select ($ query );
241
+ }
242
+ }
0 commit comments