Changeset 52328 for trunk/src/wp-includes/Requests/Session.php
- Timestamp:
- 12/06/2021 09:29:00 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Session.php
r52244 r52328 3 3 * Session handler for persistent requests and default parameters 4 4 * 5 * @package Requests\SessionHandler 5 * @package Requests 6 * @subpackage Session Handler 6 7 */ 7 8 namespace WpOrg\Requests;9 10 use WpOrg\Requests\Cookie\Jar;11 use WpOrg\Requests\Exception\InvalidArgument;12 use WpOrg\Requests\Iri;13 use WpOrg\Requests\Requests;14 use WpOrg\Requests\Utility\InputValidator;15 8 16 9 /** … … 22 15 * a shared cookie jar), then overridden for individual requests. 23 16 * 24 * @package Requests\SessionHandler 17 * @package Requests 18 * @subpackage Session Handler 25 19 */ 26 class Session {20 class Requests_Session { 27 21 /** 28 22 * Base URL for requests … … 39 33 * @var array 40 34 */ 41 public $headers = [];35 public $headers = array(); 42 36 43 37 /** … … 49 43 * @var array 50 44 */ 51 public $data = [];45 public $data = array(); 52 46 53 47 /** … … 62 56 * @var array 63 57 */ 64 public $options = [];58 public $options = array(); 65 59 66 60 /** 67 61 * Create a new session 68 62 * 69 * @param string| Stringable|null $url Base URL for requests63 * @param string|null $url Base URL for requests 70 64 * @param array $headers Default headers for requests 71 65 * @param array $data Default data for requests 72 66 * @param array $options Default options for requests 73 * 74 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string, Stringable or null. 75 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $headers argument is not an array. 76 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data argument is not an array. 77 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 78 */ 79 public function __construct($url = null, $headers = [], $data = [], $options = []) { 80 if ($url !== null && InputValidator::is_string_or_stringable($url) === false) { 81 throw InvalidArgument::create(1, '$url', 'string|Stringable|null', gettype($url)); 82 } 83 84 if (is_array($headers) === false) { 85 throw InvalidArgument::create(2, '$headers', 'array', gettype($headers)); 86 } 87 88 if (is_array($data) === false) { 89 throw InvalidArgument::create(3, '$data', 'array', gettype($data)); 90 } 91 92 if (is_array($options) === false) { 93 throw InvalidArgument::create(4, '$options', 'array', gettype($options)); 94 } 95 67 */ 68 public function __construct($url = null, $headers = array(), $data = array(), $options = array()) { 96 69 $this->url = $url; 97 70 $this->headers = $headers; … … 100 73 101 74 if (empty($this->options['cookies'])) { 102 $this->options['cookies'] = new Jar();75 $this->options['cookies'] = new Requests_Cookie_Jar(); 103 76 } 104 77 } … … 107 80 * Get a property's value 108 81 * 109 * @param string $ name Property name.82 * @param string $key Property key 110 83 * @return mixed|null Property value, null if none found 111 84 */ 112 public function __get($ name) {113 if (isset($this->options[$ name])) {114 return $this->options[$ name];85 public function __get($key) { 86 if (isset($this->options[$key])) { 87 return $this->options[$key]; 115 88 } 116 89 … … 121 94 * Set a property's value 122 95 * 123 * @param string $ name Property name.96 * @param string $key Property key 124 97 * @param mixed $value Property value 125 98 */ 126 public function __set($ name, $value) {127 $this->options[$ name] = $value;99 public function __set($key, $value) { 100 $this->options[$key] = $value; 128 101 } 129 102 … … 131 104 * Remove a property's value 132 105 * 133 * @param string $ name Property name.134 */ 135 public function __isset($ name) {136 return isset($this->options[$ name]);106 * @param string $key Property key 107 */ 108 public function __isset($key) { 109 return isset($this->options[$key]); 137 110 } 138 111 … … 140 113 * Remove a property's value 141 114 * 142 * @param string $name Property name. 143 */ 144 public function __unset($name) { 145 unset($this->options[$name]); 115 * @param string $key Property key 116 */ 117 public function __unset($key) { 118 if (isset($this->options[$key])) { 119 unset($this->options[$key]); 120 } 146 121 } 147 122 148 123 /**#@+ 149 * @see \WpOrg\Requests\Session::request()124 * @see request() 150 125 * @param string $url 151 126 * @param array $headers 152 127 * @param array $options 153 * @return \WpOrg\Requests\Response128 * @return Requests_Response 154 129 */ 155 130 /** 156 131 * Send a GET request 157 132 */ 158 public function get($url, $headers = [], $options = []) {133 public function get($url, $headers = array(), $options = array()) { 159 134 return $this->request($url, $headers, null, Requests::GET, $options); 160 135 } … … 163 138 * Send a HEAD request 164 139 */ 165 public function head($url, $headers = [], $options = []) {140 public function head($url, $headers = array(), $options = array()) { 166 141 return $this->request($url, $headers, null, Requests::HEAD, $options); 167 142 } … … 170 145 * Send a DELETE request 171 146 */ 172 public function delete($url, $headers = [], $options = []) {147 public function delete($url, $headers = array(), $options = array()) { 173 148 return $this->request($url, $headers, null, Requests::DELETE, $options); 174 149 } … … 176 151 177 152 /**#@+ 178 * @see \WpOrg\Requests\Session::request()153 * @see request() 179 154 * @param string $url 180 155 * @param array $headers 181 156 * @param array $data 182 157 * @param array $options 183 * @return \WpOrg\Requests\Response158 * @return Requests_Response 184 159 */ 185 160 /** 186 161 * Send a POST request 187 162 */ 188 public function post($url, $headers = [], $data = [], $options = []) {163 public function post($url, $headers = array(), $data = array(), $options = array()) { 189 164 return $this->request($url, $headers, $data, Requests::POST, $options); 190 165 } … … 193 168 * Send a PUT request 194 169 */ 195 public function put($url, $headers = [], $data = [], $options = []) {170 public function put($url, $headers = array(), $data = array(), $options = array()) { 196 171 return $this->request($url, $headers, $data, Requests::PUT, $options); 197 172 } … … 200 175 * Send a PATCH request 201 176 * 202 * Note: Unlike {@see \WpOrg\Requests\Session::post()} and {@see \WpOrg\Requests\Session::put()},203 * `$headers` is required, as thespecification recommends that should send an ETag177 * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the 178 * specification recommends that should send an ETag 204 179 * 205 180 * @link https://tools.ietf.org/html/rfc5789 206 181 */ 207 public function patch($url, $headers, $data = [], $options = []) {182 public function patch($url, $headers, $data = array(), $options = array()) { 208 183 return $this->request($url, $headers, $data, Requests::PATCH, $options); 209 184 } … … 216 191 * parsing. 217 192 * 218 * @see \WpOrg\Requests\Requests::request() 193 * @see Requests::request() 194 * 195 * @throws Requests_Exception On invalid URLs (`nonhttp`) 219 196 * 220 197 * @param string $url URL to request 221 198 * @param array $headers Extra headers to send with the request 222 199 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests 223 * @param string $type HTTP request type (use \WpOrg\Requests\Requests constants) 224 * @param array $options Options for the request (see {@see \WpOrg\Requests\Requests::request()}) 225 * @return \WpOrg\Requests\Response 226 * 227 * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`) 228 */ 229 public function request($url, $headers = [], $data = [], $type = Requests::GET, $options = []) { 200 * @param string $type HTTP request type (use Requests constants) 201 * @param array $options Options for the request (see {@see Requests::request}) 202 * @return Requests_Response 203 */ 204 public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) { 230 205 $request = $this->merge_request(compact('url', 'headers', 'data', 'options')); 231 206 … … 236 211 * Send multiple HTTP requests simultaneously 237 212 * 238 * @see \WpOrg\Requests\Requests::request_multiple() 239 * 240 * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple()}) 241 * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request()}) 242 * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object) 243 * 244 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access. 245 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 246 */ 247 public function request_multiple($requests, $options = []) { 248 if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) { 249 throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests)); 250 } 251 252 if (is_array($options) === false) { 253 throw InvalidArgument::create(2, '$options', 'array', gettype($options)); 254 } 255 213 * @see Requests::request_multiple() 214 * 215 * @param array $requests Requests data (see {@see Requests::request_multiple}) 216 * @param array $options Global and default options (see {@see Requests::request}) 217 * @return array Responses (either Requests_Response or a Requests_Exception object) 218 */ 219 public function request_multiple($requests, $options = array()) { 256 220 foreach ($requests as $key => $request) { 257 221 $requests[$key] = $this->merge_request($request, false); … … 269 233 * Merge a request's data with the default data 270 234 * 271 * @param array $request Request data (same form as {@see \WpOrg\Requests\Session::request_multiple()})235 * @param array $request Request data (same form as {@see request_multiple}) 272 236 * @param boolean $merge_options Should we merge options as well? 273 237 * @return array Request data … … 275 239 protected function merge_request($request, $merge_options = true) { 276 240 if ($this->url !== null) { 277 $request['url'] = Iri::absolutize($this->url, $request['url']);241 $request['url'] = Requests_IRI::absolutize($this->url, $request['url']); 278 242 $request['url'] = $request['url']->uri; 279 243 } 280 244 281 245 if (empty($request['headers'])) { 282 $request['headers'] = [];246 $request['headers'] = array(); 283 247 } 284 248 $request['headers'] = array_merge($this->headers, $request['headers']); … … 293 257 } 294 258 295 if ($merge_options === true) {259 if ($merge_options !== false) { 296 260 $request['options'] = array_merge($this->options, $request['options']); 297 261
Note: See TracChangeset
for help on using the changeset viewer.