deployphp /
deployer
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* (c) Anton Medvedev <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Deployer\Utility; |
||
| 12 | |||
| 13 | use Deployer\Exception\HttpieException; |
||
| 14 | |||
| 15 | class Httpie |
||
| 16 | { |
||
| 17 | private string $method = 'GET'; |
||
| 18 | private string $url = ''; |
||
| 19 | private array $headers = []; |
||
| 20 | private string $body = ''; |
||
| 21 | private array $curlopts = []; |
||
| 22 | private bool $nothrow = false; |
||
| 23 | |||
| 24 | public function __construct() |
||
| 25 | { |
||
| 26 | if (!extension_loaded('curl')) { |
||
| 27 | throw new \Exception( |
||
| 28 | "Please, install curl extension.\n" . |
||
| 29 | "https://php.net/curl.installation", |
||
| 30 | ); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function get(string $url): Httpie |
||
| 35 | { |
||
| 36 | $http = new self(); |
||
| 37 | $http->method = 'GET'; |
||
| 38 | $http->url = $url; |
||
| 39 | return $http; |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function post(string $url): Httpie |
||
| 43 | { |
||
| 44 | $http = new self(); |
||
| 45 | $http->method = 'POST'; |
||
| 46 | $http->url = $url; |
||
| 47 | return $http; |
||
| 48 | } |
||
| 49 | |||
| 50 | public static function patch(string $url): Httpie |
||
| 51 | { |
||
| 52 | $http = new self(); |
||
| 53 | $http->method = 'PATCH'; |
||
| 54 | $http->url = $url; |
||
| 55 | return $http; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | public static function put(string $url): Httpie |
||
| 60 | { |
||
| 61 | $http = new self(); |
||
| 62 | $http->method = 'PUT'; |
||
| 63 | $http->url = $url; |
||
| 64 | return $http; |
||
| 65 | } |
||
| 66 | |||
| 67 | public static function delete(string $url): Httpie |
||
| 68 | { |
||
| 69 | $http = new self(); |
||
| 70 | $http->method = 'DELETE'; |
||
| 71 | $http->url = $url; |
||
| 72 | return $http; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function query(array $params): self |
||
| 76 | { |
||
| 77 | $this->url .= '?' . http_build_query($params); |
||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function header(string $header, string $value): self |
||
| 82 | { |
||
| 83 | $this->headers[$header] = $value; |
||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function body(string $body): self |
||
| 88 | { |
||
| 89 | $this->body = $body; |
||
| 90 | $this->headers = array_merge($this->headers, [ |
||
| 91 | 'Content-Length' => strlen($this->body), |
||
| 92 | ]); |
||
| 93 | return $this; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function jsonBody(array $data): self |
||
| 97 | { |
||
| 98 | $this->body = json_encode($data, JSON_PRETTY_PRINT); |
||
| 99 | $this->headers = array_merge($this->headers, [ |
||
| 100 | 'Content-Type' => 'application/json', |
||
| 101 | 'Content-Length' => strlen($this->body), |
||
| 102 | ]); |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function formBody(array $data): self |
||
| 107 | { |
||
| 108 | $this->body = http_build_query($data); |
||
| 109 | $this->headers = array_merge($this->headers, [ |
||
| 110 | 'Content-type' => 'application/x-www-form-urlencoded', |
||
| 111 | 'Content-Length' => strlen($this->body), |
||
| 112 | ]); |
||
| 113 | return $this; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param mixed $value |
||
| 118 | */ |
||
| 119 | public function setopt(int $key, $value): self |
||
| 120 | { |
||
| 121 | $this->curlopts[$key] = $value; |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function nothrow(bool $on = true): self |
||
| 126 | { |
||
| 127 | $this->nothrow = $on; |
||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function send(?array &$info = null): string |
||
| 132 | { |
||
| 133 | if ($this->url === '') { |
||
| 134 | throw new \RuntimeException('URL must not be empty to Httpie::send()'); |
||
| 135 | } |
||
| 136 | $ch = curl_init($this->url); |
||
| 137 | curl_setopt($ch, CURLOPT_USERAGENT, 'Deployer ' . DEPLOYER_VERSION); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 138 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method); |
||
| 139 | $headers = []; |
||
| 140 | foreach ($this->headers as $key => $value) { |
||
| 141 | $headers[] = "$key: $value"; |
||
| 142 | } |
||
| 143 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
| 144 | curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body); |
||
| 145 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 146 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 147 | curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
||
| 148 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
||
| 149 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
||
| 150 | foreach ($this->curlopts as $key => $value) { |
||
| 151 | curl_setopt($ch, $key, $value); |
||
| 152 | } |
||
| 153 | $result = curl_exec($ch); |
||
| 154 | $info = curl_getinfo($ch); |
||
| 155 | if ($result === false) { |
||
| 156 | if ($this->nothrow) { |
||
| 157 | $result = ''; |
||
| 158 | } else { |
||
| 159 | $error = curl_error($ch); |
||
| 160 | $errno = curl_errno($ch); |
||
| 161 | curl_close($ch); |
||
| 162 | throw new HttpieException($error, $errno); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | curl_close($ch); |
||
| 166 | return $result; |
||
|
0 ignored issues
–
show
|
|||
| 167 | } |
||
| 168 | |||
| 169 | public function getJson(): mixed |
||
| 170 | { |
||
| 171 | $result = $this->send(); |
||
| 172 | $response = json_decode($result, true); |
||
| 173 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
| 174 | throw new HttpieException( |
||
| 175 | 'JSON Error: ' . json_last_error_msg() . '\n' . |
||
| 176 | 'Response: ' . $result, |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | return $response; |
||
| 180 | } |
||
| 181 | } |
||
| 182 |