-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathEndpoint.php
231 lines (197 loc) · 5.3 KB
/
Endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* ArangoDB PHP client: endpoint
*
* @package ArangoDBClient
* @author Jan Steemann
* @copyright Copyright 2012, triagens GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
/**
* Endpoint specification
*
* An endpoint contains the server location the client connects to
* the following endpoint types are currently supported (more to be added later):
* <ul>
* <li> tcp://host:port for tcp connections
* <li> unix://socket for UNIX sockets (provided the server supports this)
* <li> ssl://host:port for SSL connections (provided the server supports this)
* </ul>
*
* Note: SSL support is added in ArangoDB server 1.1<br>
*
* <br>
*
* @package ArangoDBClient
* @since 0.2
*/
class Endpoint
{
/**
* Current endpoint value
*
* @var string
*/
private $_value;
/**
* TCP endpoint type
*/
const TYPE_TCP = 'tcp';
/**
* SSL endpoint type
*/
const TYPE_SSL = 'ssl';
/**
* UNIX socket endpoint type
*/
const TYPE_UNIX = 'unix';
/**
* Regexp for TCP endpoints
*/
const REGEXP_TCP = '/^(tcp|http):\/\/(.+?):(\d+)\/?$/';
/**
* Regexp for SSL endpoints
*/
const REGEXP_SSL = '/^(ssl|https):\/\/(.+?):(\d+)\/?$/';
/**
* Regexp for UNIX socket endpoints
*/
const REGEXP_UNIX = '/^unix:\/\/(.+)$/';
/**
* Endpoint index
*/
const ENTRY_ENDPOINT = 'endpoint';
/**
* Databases index
*/
const ENTRY_DATABASES = 'databases';
/**
* Create a new endpoint
*
* @param string $value - endpoint specification
*
* @throws ClientException
*
*/
public function __construct($value)
{
if (!self::isValid($value)) {
throw new ClientException(sprintf("invalid endpoint specification '%s'", $value));
}
$this->_value = $value;
}
/**
* Return a string representation of the endpoint
*
* @magic
*
* @return string - string representation of the endpoint
*/
public function __toString()
{
return $this->_value;
}
/**
* Return the type of an endpoint
*
* @param string $value - endpoint specification value
*
* @return string - endpoint type
*/
public static function getType($value)
{
if (preg_match(self::REGEXP_TCP, $value)) {
return self::TYPE_TCP;
}
if (preg_match(self::REGEXP_SSL, $value)) {
return self::TYPE_SSL;
}
if (preg_match(self::REGEXP_UNIX, $value)) {
return self::TYPE_UNIX;
}
return null;
}
/**
* Return normalize an endpoint string - will convert http: into tcp:, and https: into ssl:
*
* @param string $value - endpoint string
*
* @return string - normalized endpoint string
*/
public static function normalize($value)
{
return preg_replace([ "/http:/", "/https:/" ], [ "tcp:", "ssl:" ], $value);
}
/**
* Return the host name of an endpoint
*
* @param string $value - endpoint specification value
*
* @return string - host name
*/
public static function getHost($value)
{
if (preg_match(self::REGEXP_TCP, $value, $matches)) {
return preg_replace("/^http:/", "tcp:", $matches[2]);
}
if (preg_match(self::REGEXP_SSL, $value, $matches)) {
return preg_replace("/^https:/", "ssl:", $matches[2]);
}
return null;
}
/**
* check whether an endpoint specification is valid
*
* @param string $mixed - endpoint specification value (can be a string or an array of strings)
*
* @return bool - true if endpoint specification is valid, false otherwise
*/
public static function isValid($value)
{
if (is_string($value)) {
$value = [ $value ];
}
if (!is_array($value) || count($value) === 0) {
return false;
}
foreach ($value as $ep) {
if (!is_string($ep)) {
return false;
}
$type = self::getType($ep);
if ($type === null) {
return false;
}
}
return true;
}
/**
* List endpoints
*
* This will list the endpoints that are configured on the server
*
* @param Connection $connection - the connection to be used
*
* @link https://www.arangodb.com/docs/stable/http/endpoints.html
* @return array $responseArray - The response array.
* @throws \ArangoDBClient\Exception
*/
public static function listEndpoints(Connection $connection)
{
$response = $connection->get(Urls::URL_ENDPOINT);
return $response->getJson();
}
/**
* Replaces "localhost" in hostname with "[::1]" in order to make these values the same
* for later comparisons
*
* @param string $hostname - hostname
*
* @return string - normalized hostname
*/
public static function normalizeHostname($hostname) {
// replace "localhost" with [::1] as arangod does
return preg_replace("/^(tcp|ssl|https?):\/\/(localhost|127\.0\.0\.1):/", "\\1://[::1]:", $hostname);
}
}
class_alias(Endpoint::class, '\triagens\ArangoDb\Endpoint');