[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Autoloader for Requests for PHP. 4 * 5 * Include this file if you'd like to avoid having to create your own autoloader. 6 * 7 * @package Requests 8 * @since 2.0.0 9 * 10 * @codeCoverageIgnore 11 */ 12 13 namespace WpOrg\Requests; 14 15 /* 16 * Ensure the autoloader is only declared once. 17 * This safeguard is in place as this is the typical entry point for this library 18 * and this file being required unconditionally could easily cause 19 * fatal "Class already declared" errors. 20 */ 21 if (class_exists('WpOrg\Requests\Autoload') === false) { 22 23 /** 24 * Autoloader for Requests for PHP. 25 * 26 * This autoloader supports the PSR-4 based Requests 2.0.0 classes in a case-sensitive manner 27 * as the most common server OS-es are case-sensitive and the file names are in mixed case. 28 * 29 * For the PSR-0 Requests 1.x BC-layer, requested classes will be treated case-insensitively. 30 * 31 * @package Requests 32 */ 33 final class Autoload { 34 35 /** 36 * List of the old PSR-0 class names in lowercase as keys with their PSR-4 case-sensitive name as a value. 37 * 38 * @var array 39 */ 40 private static $deprecated_classes = [ 41 // Interfaces. 42 'requests_auth' => '\WpOrg\Requests\Auth', 43 'requests_hooker' => '\WpOrg\Requests\HookManager', 44 'requests_proxy' => '\WpOrg\Requests\Proxy', 45 'requests_transport' => '\WpOrg\Requests\Transport', 46 47 // Classes. 48 'requests_cookie' => '\WpOrg\Requests\Cookie', 49 'requests_exception' => '\WpOrg\Requests\Exception', 50 'requests_hooks' => '\WpOrg\Requests\Hooks', 51 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', 52 'requests_ipv6' => '\WpOrg\Requests\Ipv6', 53 'requests_iri' => '\WpOrg\Requests\Iri', 54 'requests_response' => '\WpOrg\Requests\Response', 55 'requests_session' => '\WpOrg\Requests\Session', 56 'requests_ssl' => '\WpOrg\Requests\Ssl', 57 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', 58 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', 59 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', 60 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', 61 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', 62 'requests_transport_fsockopen' => '\WpOrg\Requests\Transport\Fsockopen', 63 'requests_utility_caseinsensitivedictionary' => '\WpOrg\Requests\Utility\CaseInsensitiveDictionary', 64 'requests_utility_filterediterator' => '\WpOrg\Requests\Utility\FilteredIterator', 65 'requests_exception_http' => '\WpOrg\Requests\Exception\Http', 66 'requests_exception_transport' => '\WpOrg\Requests\Exception\Transport', 67 'requests_exception_transport_curl' => '\WpOrg\Requests\Exception\Transport\Curl', 68 'requests_exception_http_304' => '\WpOrg\Requests\Exception\Http\Status304', 69 'requests_exception_http_305' => '\WpOrg\Requests\Exception\Http\Status305', 70 'requests_exception_http_306' => '\WpOrg\Requests\Exception\Http\Status306', 71 'requests_exception_http_400' => '\WpOrg\Requests\Exception\Http\Status400', 72 'requests_exception_http_401' => '\WpOrg\Requests\Exception\Http\Status401', 73 'requests_exception_http_402' => '\WpOrg\Requests\Exception\Http\Status402', 74 'requests_exception_http_403' => '\WpOrg\Requests\Exception\Http\Status403', 75 'requests_exception_http_404' => '\WpOrg\Requests\Exception\Http\Status404', 76 'requests_exception_http_405' => '\WpOrg\Requests\Exception\Http\Status405', 77 'requests_exception_http_406' => '\WpOrg\Requests\Exception\Http\Status406', 78 'requests_exception_http_407' => '\WpOrg\Requests\Exception\Http\Status407', 79 'requests_exception_http_408' => '\WpOrg\Requests\Exception\Http\Status408', 80 'requests_exception_http_409' => '\WpOrg\Requests\Exception\Http\Status409', 81 'requests_exception_http_410' => '\WpOrg\Requests\Exception\Http\Status410', 82 'requests_exception_http_411' => '\WpOrg\Requests\Exception\Http\Status411', 83 'requests_exception_http_412' => '\WpOrg\Requests\Exception\Http\Status412', 84 'requests_exception_http_413' => '\WpOrg\Requests\Exception\Http\Status413', 85 'requests_exception_http_414' => '\WpOrg\Requests\Exception\Http\Status414', 86 'requests_exception_http_415' => '\WpOrg\Requests\Exception\Http\Status415', 87 'requests_exception_http_416' => '\WpOrg\Requests\Exception\Http\Status416', 88 'requests_exception_http_417' => '\WpOrg\Requests\Exception\Http\Status417', 89 'requests_exception_http_418' => '\WpOrg\Requests\Exception\Http\Status418', 90 'requests_exception_http_428' => '\WpOrg\Requests\Exception\Http\Status428', 91 'requests_exception_http_429' => '\WpOrg\Requests\Exception\Http\Status429', 92 'requests_exception_http_431' => '\WpOrg\Requests\Exception\Http\Status431', 93 'requests_exception_http_500' => '\WpOrg\Requests\Exception\Http\Status500', 94 'requests_exception_http_501' => '\WpOrg\Requests\Exception\Http\Status501', 95 'requests_exception_http_502' => '\WpOrg\Requests\Exception\Http\Status502', 96 'requests_exception_http_503' => '\WpOrg\Requests\Exception\Http\Status503', 97 'requests_exception_http_504' => '\WpOrg\Requests\Exception\Http\Status504', 98 'requests_exception_http_505' => '\WpOrg\Requests\Exception\Http\Status505', 99 'requests_exception_http_511' => '\WpOrg\Requests\Exception\Http\Status511', 100 'requests_exception_http_unknown' => '\WpOrg\Requests\Exception\Http\StatusUnknown', 101 ]; 102 103 /** 104 * Register the autoloader. 105 * 106 * Note: the autoloader is *prepended* in the autoload queue. 107 * This is done to ensure that the Requests 2.0 autoloader takes precedence 108 * over a potentially (dependency-registered) Requests 1.x autoloader. 109 * 110 * @internal This method contains a safeguard against the autoloader being 111 * registered multiple times. This safeguard uses a global constant to 112 * (hopefully/in most cases) still function correctly, even if the 113 * class would be renamed. 114 * 115 * @return void 116 */ 117 public static function register() { 118 if (defined('REQUESTS_AUTOLOAD_REGISTERED') === false) { 119 spl_autoload_register([self::class, 'load'], true); 120 define('REQUESTS_AUTOLOAD_REGISTERED', true); 121 } 122 } 123 124 /** 125 * Autoloader. 126 * 127 * @param string $class_name Name of the class name to load. 128 * 129 * @return bool Whether a class was loaded or not. 130 */ 131 public static function load($class_name) { 132 // Check that the class starts with "Requests" (PSR-0) or "WpOrg\Requests" (PSR-4). 133 $psr_4_prefix_pos = strpos($class_name, 'WpOrg\\Requests\\'); 134 135 if (stripos($class_name, 'Requests') !== 0 && $psr_4_prefix_pos !== 0) { 136 return false; 137 } 138 139 $class_lower = strtolower($class_name); 140 141 if ($class_lower === 'requests') { 142 // Reference to the original PSR-0 Requests class. 143 $file = dirname(__DIR__) . '/library/Requests.php'; 144 } elseif ($psr_4_prefix_pos === 0) { 145 // PSR-4 classname. 146 $file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php'; 147 } 148 149 if (isset($file) && file_exists($file)) { 150 include $file; 151 return true; 152 } 153 154 /* 155 * Okay, so the class starts with "Requests", but we couldn't find the file. 156 * If this is one of the deprecated/renamed PSR-0 classes being requested, 157 * let's alias it to the new name and throw a deprecation notice. 158 */ 159 if (isset(self::$deprecated_classes[$class_lower])) { 160 /* 161 * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations 162 * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`. 163 * The constant needs to be defined before the first deprecated class is requested 164 * via this autoloader. 165 */ 166 if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) { 167 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error 168 trigger_error( 169 'The PSR-0 `Requests_...` class names in the Requests library are deprecated.' 170 . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', 171 E_USER_DEPRECATED 172 ); 173 174 // Prevent the deprecation notice from being thrown twice. 175 if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) { 176 define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true); 177 } 178 } 179 180 // Create an alias and let the autoloader recursively kick in to load the PSR-4 class. 181 return class_alias(self::$deprecated_classes[$class_lower], $class_name, true); 182 } 183 184 return false; 185 } 186 } 187 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |