|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Static wrapper for the HTTP class. |
|
7
|
|
|
* For cleaner calls. |
|
8
|
|
|
* |
|
9
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
10
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
11
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
12
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
13
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
14
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
15
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
16
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
17
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
18
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
19
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Glynn Quelch <[email protected]> |
|
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
23
|
|
|
* @package PinkCrab\HTTP |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace PinkCrab\HTTP; |
|
27
|
|
|
|
|
28
|
|
|
use WP_HTTP_Response; |
|
29
|
|
|
use Nyholm\Psr7\Stream; |
|
30
|
|
|
use PinkCrab\HTTP\HTTP; |
|
31
|
|
|
use Psr\Http\Message\UriInterface; |
|
32
|
|
|
use Psr\Http\Message\StreamInterface; |
|
33
|
|
|
use Psr\Http\Message\RequestInterface; |
|
34
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
35
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
36
|
|
|
|
|
37
|
|
|
class HTTP_Helper { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Instance of HTTP class. |
|
41
|
|
|
* |
|
42
|
|
|
* @var HTTP|null |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static $http; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the current HTTP instance. |
|
48
|
|
|
* Creates if doesnt exist. |
|
49
|
|
|
* |
|
50
|
|
|
* @return HTTP |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function get_http(): HTTP { |
|
53
|
|
|
if ( ! static::$http ) { |
|
54
|
|
|
static::$http = new HTTP(); |
|
55
|
|
|
} |
|
56
|
|
|
return static::$http; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns a ServerRequest with current globals. |
|
61
|
|
|
* |
|
62
|
|
|
* @return ServerRequestInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function global_server_request(): ServerRequestInterface { |
|
65
|
|
|
return static::get_http() |
|
66
|
|
|
->request_from_globals(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Wrapper for making a PS7 request. |
|
71
|
|
|
* |
|
72
|
|
|
* @uses Nyholm\Psr7::Request() |
|
73
|
|
|
* @param string $method HTTP method |
|
74
|
|
|
* @param string|UriInterface $uri URI |
|
75
|
|
|
* @param array<string, string> $headers Request headers |
|
76
|
|
|
* @param string|resource|StreamInterface|null $body Request body |
|
77
|
|
|
* @param string $version Protocol version |
|
78
|
|
|
* |
|
79
|
|
|
* @return RequestInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function request( |
|
82
|
|
|
string $method, |
|
83
|
|
|
$uri, |
|
84
|
|
|
array $headers = array(), |
|
85
|
|
|
$body = null, |
|
86
|
|
|
string $version = '1.1' |
|
87
|
|
|
): RequestInterface { |
|
88
|
|
|
return static::get_http() |
|
89
|
|
|
->psr7_request( $method, $uri, $headers, $body, $version ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns a PS7 Response object. |
|
94
|
|
|
* |
|
95
|
|
|
* @param integer $status |
|
96
|
|
|
* @param array<string, string> $headers |
|
97
|
|
|
* @param array<string, string>|string|resource|StreamInterface|null $body |
|
98
|
|
|
* @param string $version |
|
99
|
|
|
* @param string|null $reason |
|
100
|
|
|
* |
|
101
|
|
|
* @return ResponseInterface |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function response( |
|
104
|
|
|
$body = null, |
|
105
|
|
|
int $status = 200, |
|
106
|
|
|
array $headers = array(), |
|
107
|
|
|
string $version = '1.1', |
|
108
|
|
|
?string $reason = null |
|
109
|
|
|
): ResponseInterface { |
|
110
|
|
|
return static::get_http() |
|
111
|
|
|
->psr7_response( $body, $status, $headers, $version, $reason ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns a WP_Rest_Response |
|
116
|
|
|
* |
|
117
|
|
|
* @param integer $status |
|
118
|
|
|
* @param array<string, string> $headers |
|
119
|
|
|
* @param mixed $data |
|
120
|
|
|
* @return WP_HTTP_Response |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function wp_response( |
|
123
|
|
|
$data = null, |
|
124
|
|
|
int $status = 200, |
|
125
|
|
|
array $headers = array() |
|
126
|
|
|
): WP_HTTP_Response { |
|
127
|
|
|
return static::get_http() |
|
128
|
|
|
->wp_response( $data, $status, $headers ); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Wraps any value which can be json encoded in a StreamInterface |
|
133
|
|
|
* |
|
134
|
|
|
* @param string|integer|float|object|array<mixed> $value |
|
135
|
|
|
* @return StreamInterface |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function stream_from_scalar( $value ): StreamInterface { |
|
138
|
|
|
return Stream::create( json_encode( $value ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|