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