|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PLASTIC package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
|
7
|
|
|
* <https://github.com/adhocore> |
|
8
|
|
|
* |
|
9
|
|
|
* Licensed under MIT license. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ahc\Plastic; |
|
13
|
|
|
|
|
14
|
|
|
class Client |
|
15
|
|
|
{ |
|
16
|
|
|
protected $segments = []; |
|
17
|
|
|
protected $method = 'GET'; |
|
18
|
|
|
protected $pretty = \false; |
|
19
|
|
|
protected $hasBody = \false; |
|
20
|
|
|
protected $host = 'http://localhost:9200'; |
|
21
|
|
|
protected $verbs = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD']; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(string $host = \null, bool $pretty = \false) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($host !== null) { |
|
26
|
|
|
$this->host = $host; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->pretty = $pretty; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function __get(string $key): self |
|
33
|
|
|
{ |
|
34
|
|
|
if (\in_array($verb = \strtoupper($key), $this->verbs, \true)) { |
|
35
|
|
|
$this->reset($verb); |
|
36
|
|
|
} else { |
|
37
|
|
|
$this->segments[] = $this->getPart($key); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __call(string $method, array $data = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->segments[] = $this->getPart($method); |
|
46
|
|
|
|
|
47
|
|
|
list($data, $params) = $this->prepareData($data); |
|
48
|
|
|
|
|
49
|
|
|
return $this->call($data, $params); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function reset(string $verb) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->segments = []; |
|
55
|
|
|
$this->method = $verb; |
|
56
|
|
|
$this->hasBody = $verb === 'POST' || $verb === 'PUT'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function getPart(string $part): string |
|
60
|
|
|
{ |
|
61
|
|
|
return \preg_match('/^_(\d+)$/', $part) ? \substr($part, 1) : $part; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function prepareData(array $data): array |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->pretty) { |
|
67
|
|
|
$data[1]['pretty'] = 'true'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$params = empty($data[1]) ? '' : $this->asQuery($data[1]); |
|
71
|
|
|
$data = empty($data[0]) ? '' : $this->serialize($data[0]); |
|
72
|
|
|
|
|
73
|
|
|
return [$data, $params]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function serialize($data): string |
|
77
|
|
|
{ |
|
78
|
|
|
$data = $this->hasBody ? $this->asJson($data) : $this->asQuery($data); |
|
79
|
|
|
$data = \str_replace('"', '\"', $data); |
|
80
|
|
|
|
|
81
|
|
|
return $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function asJson($data): string |
|
85
|
|
|
{ |
|
86
|
|
|
return \json_encode($data); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function asQuery($data): string |
|
90
|
|
|
{ |
|
91
|
|
|
return \http_build_query($data); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function call(string $data, string $params) |
|
95
|
|
|
{ |
|
96
|
|
|
$curl = "curl --silent -X {$this->method}"; |
|
97
|
|
|
$url = "{$this->host}/{$this->uri()}?{$params}"; |
|
98
|
|
|
|
|
99
|
|
|
if ($this->hasBody) { |
|
100
|
|
|
$curl .= ' -H "content-type: application/json" -d "' . $data . '"'; |
|
101
|
|
|
} else { |
|
102
|
|
|
$url .= "&{$data}"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$curl .= ' "' . $url . '"'; |
|
106
|
|
|
|
|
107
|
|
|
return \shell_exec($curl); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function uri(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return \implode('/', $this->segments); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|