|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file Couch.php |
|
5
|
|
|
* @brief This file contains the Couch class. |
|
6
|
|
|
* @details |
|
7
|
|
|
* @author Filippo F. Fadda |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
//! This is the main Elephant on Couch library namespace. |
|
11
|
|
|
namespace Surfer; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Surfer\Adapter\IClientAdapter; |
|
15
|
|
|
use Surfer\Message\Request; |
|
16
|
|
|
use Surfer\Message\Response; |
|
17
|
|
|
use Surfer\Hook\IChunkHook; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @brief The Surfer HTTP client. You need an instance of this class to make your HTTP requests. |
|
22
|
|
|
* @nosubgrouping |
|
23
|
|
|
* @todo Add Memcached support. Remember to use Memcached extension, not memcache. |
|
24
|
|
|
* @todo Add Post File support. |
|
25
|
|
|
* @todo Check ISO-8859-1 because CouchDB uses it, in particular utf8_encode(). |
|
26
|
|
|
*/ |
|
27
|
|
|
class Surfer { |
|
28
|
|
|
|
|
29
|
|
|
//! The user agent name. |
|
30
|
|
|
const USER_AGENT_NAME = "Surfer"; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var IClientAdapter $client |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @brief Creates a Couch class instance. |
|
40
|
|
|
* @param IClientAdapter $adapter An instance of a class that implements the IClientAdapter interface. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(IClientAdapter $adapter) { |
|
43
|
|
|
$this->client = $adapter; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @brief This method is used to send a Request to CouchDB. |
|
49
|
|
|
* @details If you want call a not supported CouchDB API, you can use this function to send your request.\n |
|
50
|
|
|
* You can also provide an instance of a class that implements the IChunkHook interface, to deal with a chunked |
|
51
|
|
|
* response. |
|
52
|
|
|
* @param Request $request The Request object. |
|
53
|
|
|
* @param IChunkHook $chunkHook (optional) A class instance that implements the IChunkHook interface. |
|
54
|
|
|
* @return Response |
|
55
|
|
|
*/ |
|
56
|
|
|
public function send(Request $request, IChunkHook $chunkHook = NULL) { |
|
57
|
|
|
// Sets user agent information. |
|
58
|
|
|
$request->setHeaderField(Request::USER_AGENT_HF, self::USER_AGENT_NAME); |
|
59
|
|
|
|
|
60
|
|
|
// We accept JSON. |
|
61
|
|
|
$request->setHeaderField(Request::ACCEPT_HF, "application/json"); |
|
62
|
|
|
|
|
63
|
|
|
// We close the connection after read the response. |
|
64
|
|
|
// NOTE: we don't use anymore the connection header field, because we use the same socket until the end of script. |
|
65
|
|
|
//$request->setHeaderField(Message::CONNECTION_HF, "close"); |
|
66
|
|
|
|
|
67
|
|
|
$response = $this->client->send($request, $chunkHook); |
|
68
|
|
|
|
|
69
|
|
|
// 1xx - Informational Status Codes |
|
70
|
|
|
// 2xx - Success Status Codes |
|
71
|
|
|
// 3xx - Redirection Status Codes |
|
72
|
|
|
// 4xx - Client Error Status Codes |
|
73
|
|
|
// 5xx - Server Error Status Codes |
|
74
|
|
|
$statusCode = (int)$response->getStatusCode(); |
|
75
|
|
|
|
|
76
|
|
|
switch ($statusCode) { |
|
77
|
|
|
case ($statusCode >= 200 && $statusCode < 300): |
|
78
|
|
|
break; |
|
79
|
|
|
case ($statusCode < 200): |
|
80
|
|
|
//$this->handleInformational($request, $response); |
|
81
|
|
|
break; |
|
82
|
|
|
case ($statusCode < 400): |
|
83
|
|
|
//$this->handleRedirection($request, $response); |
|
84
|
|
|
break; |
|
85
|
|
|
case ($statusCode < 500): |
|
86
|
|
|
throw new Exception\ClientErrorException($request, $response); |
|
87
|
|
|
case ($statusCode >= 500): |
|
88
|
|
|
throw new Exception\ServerErrorException($request, $response); |
|
89
|
|
|
default: |
|
90
|
|
|
throw new Exception\UnknownResponseException($request, $response); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $response; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |