forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestsLibrary.php
More file actions
298 lines (260 loc) · 7.28 KB
/
RequestsLibrary.php
File metadata and controls
298 lines (260 loc) · 7.28 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace WP_CLI;
use Exception;
use RuntimeException;
use WP_CLI;
/**
* Class RequestsLibrary.
*
* A class to manage the version and source of the Requests library used by WP-CLI.
*/
final class RequestsLibrary {
/**
* Version 1 of the Requests library.
*
* @var string
*/
const VERSION_V1 = 'v1';
/**
* Version 2 of the Requests library.
*
* @var string
*/
const VERSION_V2 = 'v2';
/**
* Array of valid versions for the Requests library.
*
* @var array<string>
*/
const VALID_VERSIONS = [ self::VERSION_V1, self::VERSION_V2 ];
/**
* Requests library bundled with WordPress Core being used.
*
* @var string
*/
const SOURCE_WP_CORE = 'wp-core';
/**
* Requests library bundled with WP-CLI being used.
*
* @var string
*/
const SOURCE_WP_CLI = 'wp-cli';
/**
* Array of valid source for the Requests library.
*
* @var array<string>
*/
const VALID_SOURCES = [ self::SOURCE_WP_CORE, self::SOURCE_WP_CLI ];
/**
* Class name of the Requests main class for v1.
*
* @var string
*/
const CLASS_NAME_V1 = '\Requests';
/**
* Class name of the Requests main class for v2.
*
* @var string
*/
const CLASS_NAME_V2 = '\WpOrg\Requests\Requests';
/**
* Version of the Requests library being used.
*
* @var string
*/
private static $version = self::VERSION_V2;
/**
* Source of the Requests library being used.
*
* @var string
*/
private static $source = self::SOURCE_WP_CLI;
/**
* Class name of the Requests library being used.
*
* @var string
*/
private static $class_name = self::CLASS_NAME_V2;
/**
* Check if the current version is v1.
*
* @return bool Whether the current version is v1.
*/
public static function is_v1() {
return self::get_version() === self::VERSION_V1;
}
/**
* Check if the current version is v2.
*
* @return bool Whether the current version is v2.
*/
public static function is_v2() {
return self::get_version() === self::VERSION_V2;
}
/**
* Check if the current source for the Requests library is WordPress Core.
*
* @return bool Whether the current source is WordPress Core.
*/
public static function is_core() {
return self::get_source() === self::SOURCE_WP_CORE;
}
/**
* Check if the current source for the Requests library is WP-CLI.
*
* @return bool Whether the current source is WP-CLI.
*/
public static function is_cli() {
return self::get_source() === self::SOURCE_WP_CLI;
}
/**
* Get the current version.
*
* @return string The current version.
*/
public static function get_version() {
return self::$version;
}
/**
* Set the version of the library.
*
* @param string $version The version to set.
* @throws RuntimeException if the version is invalid.
*/
public static function set_version( $version ) {
if ( ! is_string( $version ) ) {
throw new RuntimeException( 'RequestsLibrary::$version must be a string.' );
}
if ( ! in_array( $version, self::VALID_VERSIONS, true ) ) {
throw new RuntimeException(
sprintf(
'Invalid RequestsLibrary::$version, must be one of: %s.',
implode( ', ', self::VALID_VERSIONS )
)
);
}
WP_CLI::debug( 'Setting RequestsLibrary::$version to ' . $version, 'bootstrap' );
self::$version = $version;
}
/**
* Get the current class name.
*
* @return string The current class name.
* @throws RuntimeException if the class name is not set.
*/
public static function get_class_name() {
return self::$class_name;
}
/**
* Set the class name for the library.
*
* @param string $class_name The class name to set.
*/
public static function set_class_name( $class_name ) {
if ( ! is_string( $class_name ) ) {
throw new RuntimeException( 'RequestsLibrary::$class_name must be a string.' );
}
WP_CLI::debug( 'Setting RequestsLibrary::$class_name to ' . $class_name, 'bootstrap' );
self::$class_name = $class_name;
}
/**
* Get the current source.
*
* @return string The current source.
*/
public static function get_source() {
return self::$source;
}
/**
* Set the source of the library.
*
* @param string $source The source to set.
* @throws RuntimeException if the source is invalid.
*/
public static function set_source( $source ) {
if ( ! is_string( $source ) ) {
throw new RuntimeException( 'RequestsLibrary::$source must be a string.' );
}
if ( ! in_array( $source, self::VALID_SOURCES, true ) ) {
throw new RuntimeException(
sprintf(
'Invalid RequestsLibrary::$source, must be one of: %s.',
implode( ', ', self::VALID_SOURCES )
)
);
}
WP_CLI::debug( 'Setting RequestsLibrary::$source to ' . $source, 'bootstrap' );
self::$source = $source;
}
/**
* Check if a given exception was issued by the Requests library.
*
* This is used because we cannot easily catch multiple different exception
* classes with PHP 5.6. Because of that, we catch generic exceptions, check if
* they match the Requests library, and re-throw them if they do not.
*
* @param Exception $exception Exception to check.
* @return bool Whether the provided exception was issued by the Requests library.
*/
public static function is_requests_exception( Exception $exception ) {
return is_a( $exception, '\Requests_Exception' )
|| is_a( $exception, '\WpOrg\Requests\Exception' );
}
/**
* Register the autoloader for the Requests library.
*
* This checks for the detected setup and register the corresponding
* autoloader if it is still needed.
*/
public static function register_autoloader() {
$includes_path = defined( 'WPINC' ) ? WPINC : 'wp-includes';
if ( self::is_v1() && ! class_exists( self::CLASS_NAME_V1 ) ) {
if ( self::is_core() ) {
require_once ABSPATH . $includes_path . '/class-requests.php';
} else {
require_once WP_CLI_VENDOR_DIR . '/rmccue/requests/library/Requests.php';
}
\Requests::register_autoloader();
}
if ( self::is_v2() && ! class_exists( self::CLASS_NAME_V2 ) ) {
if ( self::is_core() ) {
require_once ABSPATH . $includes_path . '/Requests/Autoload.php';
} else {
self::maybe_define_wp_cli_root();
if ( file_exists( WP_CLI_ROOT . '/bundle/rmccue/requests/src/Autoload.php' ) ) {
require_once WP_CLI_ROOT . '/bundle/rmccue/requests/src/Autoload.php';
} else {
require_once WP_CLI_VENDOR_DIR . '/rmccue/requests/src/Autoload.php';
}
}
\WpOrg\Requests\Autoload::register();
}
}
/**
* Get the path to the bundled certificate.
*
* @return string The path to the bundled certificate.
*/
public static function get_bundled_certificate_path() {
if ( self::is_core() ) {
$includes_path = defined( 'WPINC' ) ? WPINC : 'wp-includes';
return ABSPATH . $includes_path . '/certificates/ca-bundle.crt';
} elseif ( self::is_v1() ) {
return WP_CLI_VENDOR_DIR . '/rmccue/requests/library/Requests/Transport/cacert.pem';
} else {
self::maybe_define_wp_cli_root();
if ( file_exists( WP_CLI_ROOT . '/bundle/rmccue/requests/certificates/cacert.pem' ) ) {
return WP_CLI_ROOT . '/bundle/rmccue/requests/certificates/cacert.pem';
}
return WP_CLI_VENDOR_DIR . '/rmccue/requests/certificates/cacert.pem';
}
}
/**
* Define WP_CLI_ROOT if it is not already defined.
*/
private static function maybe_define_wp_cli_root() {
if ( ! defined( 'WP_CLI_ROOT' ) ) {
define( 'WP_CLI_ROOT', dirname( dirname( __DIR__ ) ) );
}
}
}