@@ -80,6 +80,13 @@ class Connection
80
80
*/
81
81
private $ _batchRequest = false ;
82
82
83
+ /**
84
+ * custom queue name (leave empty if no custom queue is required)
85
+ *
86
+ * @var string
87
+ */
88
+ private $ _customQueue = null ;
89
+
83
90
/**
84
91
* $_database string
85
92
*
@@ -157,14 +164,44 @@ public function setOption($name, $value) {
157
164
}
158
165
else if ($ name === ConnectionOptions::OPTION_CONNECTION ) {
159
166
// set keep-alive flag
160
- $ this ->_useKeepAlive = ($ value === 'Keep-Alive ' );
167
+ $ this ->_useKeepAlive = (strtolower ( $ value) === 'keep-alive ' );
161
168
}
162
169
else if ($ name === ConnectionOptions::OPTION_DATABASE ) {
163
170
// set database
164
171
$ this ->setDatabase ($ value );
165
172
}
166
173
}
167
174
175
+
176
+ /**
177
+ * Enables a custom queue name for all actions of the connection
178
+ *
179
+ * @param string $queueName - queue name
180
+ * @param number $count - number of requests the custom queue will be used for
181
+ */
182
+
183
+ public function enableCustomQueue ($ queueName , $ count = null )
184
+ {
185
+ $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE ] = $ queueName ;
186
+
187
+ if ($ count !== null ) {
188
+ if (! is_numeric ($ count ) || $ count <= 0 ) {
189
+ throw new ClientException ('Invalid value for count value of custom queues ' );
190
+ }
191
+ $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT ] = $ count ;
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Disable usage of custom queue for all actions of the connection
197
+ */
198
+ public function disableCustomQueue ()
199
+ {
200
+ $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE ] = null ;
201
+ $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT ] = null ;
202
+ }
203
+
204
+
168
205
/**
169
206
* Issue an HTTP GET request
170
207
*
@@ -175,9 +212,9 @@ public function setOption($name, $value) {
175
212
*
176
213
* @return HttpResponse
177
214
*/
178
- public function get ($ url , $ customHeader = array ())
215
+ public function get ($ url , array $ customHeaders = array ())
179
216
{
180
- $ response = $ this ->executeRequest (HttpHelper::METHOD_GET , $ url , '' , $ customHeader );
217
+ $ response = $ this ->executeRequest (HttpHelper::METHOD_GET , $ url , '' , $ customHeaders );
181
218
182
219
return $ this ->parseResponse ($ response );
183
220
}
@@ -193,9 +230,9 @@ public function get($url, $customHeader = array())
193
230
*
194
231
* @return HttpResponse
195
232
*/
196
- public function post ($ url , $ data , $ customHeader = array ())
233
+ public function post ($ url , $ data , array $ customHeaders = array ())
197
234
{
198
- $ response = $ this ->executeRequest (HttpHelper::METHOD_POST , $ url , $ data , $ customHeader );
235
+ $ response = $ this ->executeRequest (HttpHelper::METHOD_POST , $ url , $ data , $ customHeaders );
199
236
200
237
return $ this ->parseResponse ($ response );
201
238
}
@@ -211,9 +248,9 @@ public function post($url, $data, $customHeader = array())
211
248
*
212
249
* @return HttpResponse
213
250
*/
214
- public function put ($ url , $ data , $ customHeader = array ())
251
+ public function put ($ url , $ data , array $ customHeaders = array ())
215
252
{
216
- $ response = $ this ->executeRequest (HttpHelper::METHOD_PUT , $ url , $ data , $ customHeader );
253
+ $ response = $ this ->executeRequest (HttpHelper::METHOD_PUT , $ url , $ data , $ customHeaders );
217
254
218
255
return $ this ->parseResponse ($ response );
219
256
}
@@ -228,9 +265,9 @@ public function put($url, $data, $customHeader = array())
228
265
*
229
266
* @return HttpResponse
230
267
*/
231
- public function head ($ url , $ customHeader = array ())
268
+ public function head ($ url , array $ customHeaders = array ())
232
269
{
233
- $ response = $ this ->executeRequest (HttpHelper::METHOD_HEAD , $ url , '' , $ customHeader );
270
+ $ response = $ this ->executeRequest (HttpHelper::METHOD_HEAD , $ url , '' , $ customHeaders );
234
271
235
272
return $ this ->parseResponse ($ response );
236
273
}
@@ -246,9 +283,9 @@ public function head($url, $customHeader = array())
246
283
*
247
284
* @return HttpResponse
248
285
*/
249
- public function patch ($ url , $ data , $ customHeader = array ())
286
+ public function patch ($ url , $ data , array $ customHeaders = array ())
250
287
{
251
- $ response = $ this ->executeRequest (HttpHelper::METHOD_PATCH , $ url , $ data , $ customHeader );
288
+ $ response = $ this ->executeRequest (HttpHelper::METHOD_PATCH , $ url , $ data , $ customHeaders );
252
289
253
290
return $ this ->parseResponse ($ response );
254
291
}
@@ -263,9 +300,9 @@ public function patch($url, $data, $customHeader = array())
263
300
*
264
301
* @return HttpResponse
265
302
*/
266
- public function delete ($ url , $ customHeader = array ())
303
+ public function delete ($ url , array $ customHeaders = array ())
267
304
{
268
- $ response = $ this ->executeRequest (HttpHelper::METHOD_DELETE , $ url , '' , $ customHeader );
305
+ $ response = $ this ->executeRequest (HttpHelper::METHOD_DELETE , $ url , '' , $ customHeaders );
269
306
270
307
return $ this ->parseResponse ($ response );
271
308
}
@@ -356,33 +393,52 @@ public function parseResponse(HttpResponse $response)
356
393
*
357
394
* @throws Exception
358
395
*
359
- * @param string $method - HTTP request method
360
- * @param string $url - HTTP URL
361
- * @param string $data - data to post in body
362
- * @param array $customHeader - any array containing header elements
396
+ * @param string $method - HTTP request method
397
+ * @param string $url - HTTP URL
398
+ * @param string $data - data to post in body
399
+ * @param array $customHeaders - any array containing header elements
363
400
*
364
401
* @return HttpResponse
365
402
*/
366
- private function executeRequest ($ method , $ url , $ data , $ customHeader = array ())
403
+ private function executeRequest ($ method , $ url , $ data , array $ customHeaders = array ())
367
404
{
368
405
HttpHelper::validateMethod ($ method );
369
406
$ database = $ this ->getDatabase ();
370
407
if ($ database === '' ) {
371
408
$ url = '/_db/ ' . '_system ' . $ url ;
372
409
} else {
373
- $ url = '/_db/ ' . $ database . $ url ;
410
+ $ url = '/_db/ ' . urlencode ( $ database) . $ url ;
374
411
}
375
412
413
+ // check if a custom queue should be used
414
+ if (! isset ($ customHeaders [ConnectionOptions::OPTION_CUSTOM_QUEUE ]) &&
415
+ $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE ] !== null ) {
416
+
417
+ $ customHeaders [HttpHelper::QUEUE_HEADER ] = $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE ];
418
+
419
+ // check if a counter is set for the custom queue
420
+ $ count = $ this ->_options [ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT ];
421
+ if ($ count !== null ) {
422
+ // yes, now decrease the counter
423
+
424
+ if ($ count === 1 ) {
425
+ $ this ->disableCustomQueue ();
426
+ }
427
+ else {
428
+ $ this ->_options ->offsetSet (ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT , $ count - 1 );
429
+ }
430
+ }
431
+ }
376
432
377
433
// create request data
378
434
if ($ this ->_batchRequest === false ) {
379
435
380
436
if ($ this ->_captureBatch === true ) {
381
437
$ this ->_options ->offsetSet (ConnectionOptions::OPTION_BATCHPART , true );
382
- $ request = HttpHelper::buildRequest ($ this ->_options , $ method , $ url , $ data , $ customHeader );
438
+ $ request = HttpHelper::buildRequest ($ this ->_options , $ method , $ url , $ data , $ customHeaders );
383
439
$ this ->_options ->offsetSet (ConnectionOptions::OPTION_BATCHPART , false );
384
440
} else {
385
- $ request = HttpHelper::buildRequest ($ this ->_options , $ method , $ url , $ data , $ customHeader );
441
+ $ request = HttpHelper::buildRequest ($ this ->_options , $ method , $ url , $ data , $ customHeaders );
386
442
}
387
443
388
444
if ($ this ->_captureBatch === true ) {
@@ -396,7 +452,7 @@ private function executeRequest($method, $url, $data, $customHeader = array())
396
452
397
453
$ this ->_options ->offsetSet (ConnectionOptions::OPTION_BATCH , true );
398
454
399
- $ request = HttpHelper::buildRequest ($ this ->_options , $ method , $ url , $ data , $ customHeader );
455
+ $ request = HttpHelper::buildRequest ($ this ->_options , $ method , $ url , $ data , $ customHeaders );
400
456
$ this ->_options ->offsetSet (ConnectionOptions::OPTION_BATCH , false );
401
457
}
402
458
0 commit comments