o2system /
kernel
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Kernel\Http\Message; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Kernel\Http\Abstracts\AbstractMessage; |
||||
| 19 | use O2System\Psr\Http\Header\RequestFieldInterface; |
||||
| 20 | use O2System\Psr\Http\Message\RequestInterface; |
||||
| 21 | use O2System\Psr\Http\Message\UriInterface; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Class Request |
||||
| 25 | * |
||||
| 26 | * @package O2System\Curl |
||||
| 27 | */ |
||||
| 28 | class Request extends AbstractMessage implements |
||||
| 29 | RequestInterface, |
||||
| 30 | RequestFieldInterface |
||||
| 31 | { |
||||
| 32 | /** |
||||
| 33 | * Request::$method |
||||
| 34 | * |
||||
| 35 | * Request Method |
||||
| 36 | * |
||||
| 37 | * @var string |
||||
| 38 | */ |
||||
| 39 | protected $method = 'GET'; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Request::$uri |
||||
| 43 | * |
||||
| 44 | * Request Uri |
||||
| 45 | * |
||||
| 46 | * @var Uri|\O2System\Kernel\Http\Message\Uri |
||||
| 47 | */ |
||||
| 48 | protected $uri; |
||||
| 49 | |||||
| 50 | // ------------------------------------------------------------------------ |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * Request::__construct |
||||
| 54 | */ |
||||
| 55 | public function __construct() |
||||
| 56 | { |
||||
| 57 | $this->uri = new Uri(); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | // ------------------------------------------------------------------------ |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Request::__get |
||||
| 64 | * |
||||
| 65 | * @param string $property |
||||
| 66 | * |
||||
| 67 | * @return mixed |
||||
| 68 | */ |
||||
| 69 | public function __get($property) |
||||
| 70 | { |
||||
| 71 | if(property_exists($this, $property)) { |
||||
| 72 | return $this->{$property}; |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | return null; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | // ------------------------------------------------------------------------ |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Request::getClientIpAddress |
||||
| 82 | * |
||||
| 83 | * @return bool|string |
||||
| 84 | */ |
||||
| 85 | public function getClientIpAddress() |
||||
| 86 | { |
||||
| 87 | if ($ipAddresses = config()->getItem('ipAddresses')) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 88 | return input()->ipAddress($ipAddresses->proxy); |
||||
|
0 ignored issues
–
show
The method
ipAddress() does not exist on O2System\Kernel\Cli\Input.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 89 | } |
||||
| 90 | |||||
| 91 | return false; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | // ------------------------------------------------------------------------ |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * Request::getClientUserAgent |
||||
| 98 | * |
||||
| 99 | * @return string |
||||
| 100 | */ |
||||
| 101 | public function getClientUserAgent() |
||||
| 102 | { |
||||
| 103 | return input()->userAgent(); |
||||
|
0 ignored issues
–
show
The method
userAgent() does not exist on O2System\Kernel\Cli\Input.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 104 | } |
||||
| 105 | |||||
| 106 | // ------------------------------------------------------------------------ |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * Request::isCli |
||||
| 110 | * |
||||
| 111 | * Determines if this request was made from the command line (CLI). |
||||
| 112 | * |
||||
| 113 | * @return bool |
||||
| 114 | */ |
||||
| 115 | public function isCli() |
||||
| 116 | { |
||||
| 117 | return (PHP_SAPI === 'cli' || defined('STDIN')); |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | // ------------------------------------------------------------------------ |
||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * Request::isAjax |
||||
| 124 | * |
||||
| 125 | * Test to see if a request contains the HTTP_X_REQUESTED_WITH header. |
||||
| 126 | * |
||||
| 127 | * @return bool |
||||
| 128 | */ |
||||
| 129 | public function isAjax() |
||||
| 130 | { |
||||
| 131 | return ( ! empty($_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) && |
||||
| 132 | strtolower($_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) === 'xmlhttprequest'); |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | //-------------------------------------------------------------------- |
||||
| 136 | |||||
| 137 | /** |
||||
| 138 | * Request::isSecure |
||||
| 139 | * |
||||
| 140 | * Attempts to detect if the current connection is secure through |
||||
| 141 | * a few different methods. |
||||
| 142 | * |
||||
| 143 | * @return bool |
||||
| 144 | */ |
||||
| 145 | public function isSecure() |
||||
| 146 | { |
||||
| 147 | if ( ! empty($_SERVER[ 'HTTPS' ]) && strtolower($_SERVER[ 'HTTPS' ]) !== 'off') { |
||||
| 148 | return true; |
||||
| 149 | } elseif (isset($_SERVER[ 'HTTP_X_FORWARDED_PROTO' ]) && $_SERVER[ 'HTTP_X_FORWARDED_PROTO' ] === 'https') { |
||||
| 150 | return true; |
||||
| 151 | } elseif ( ! empty($_SERVER[ 'HTTP_FRONT_END_HTTPS' ]) && strtolower( |
||||
| 152 | $_SERVER[ 'HTTP_FRONT_END_HTTPS' ] |
||||
| 153 | ) !== 'off' |
||||
| 154 | ) { |
||||
| 155 | return true; |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | return false; |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | //-------------------------------------------------------------------- |
||||
| 162 | |||||
| 163 | /** |
||||
| 164 | * Request::getTime |
||||
| 165 | * |
||||
| 166 | * @param string $format |
||||
| 167 | * |
||||
| 168 | * @return bool|mixed|string |
||||
| 169 | */ |
||||
| 170 | public function getTime($format = null) |
||||
| 171 | { |
||||
| 172 | return isset($format) |
||||
| 173 | ? date($format, $_SERVER[ 'REQUEST_TIME' ]) |
||||
| 174 | : $_SERVER[ 'REQUEST_TIME' ]; |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | //-------------------------------------------------------------------- |
||||
| 178 | |||||
| 179 | /** |
||||
| 180 | * Request::getServer |
||||
| 181 | * |
||||
| 182 | * @return \O2System\Kernel\Http\Message\ServerRequest |
||||
| 183 | */ |
||||
| 184 | public function getServer() |
||||
| 185 | { |
||||
| 186 | return server_request(); |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | //-------------------------------------------------------------------- |
||||
| 190 | |||||
| 191 | /** |
||||
| 192 | * RequestInterface::getRequestTarget |
||||
| 193 | * |
||||
| 194 | * Retrieves the message's request target. |
||||
| 195 | * |
||||
| 196 | * Retrieves the message's request-target either as it will appear (for |
||||
| 197 | * clients), as it appeared at request (for servers), or as it was |
||||
| 198 | * specified for the instance (see withRequestTarget()). |
||||
| 199 | * |
||||
| 200 | * In most cases, this will be the origin-form of the composed URI, |
||||
| 201 | * unless a value was provided to the concrete implementation (see |
||||
| 202 | * withRequestTarget() below). |
||||
| 203 | * |
||||
| 204 | * If no URI is available, and no request-target has been specifically |
||||
| 205 | * provided, this method MUST return the string "/". |
||||
| 206 | * |
||||
| 207 | * @return string |
||||
| 208 | */ |
||||
| 209 | public function getRequestTarget() |
||||
| 210 | { |
||||
| 211 | $requestTarget = '/'; |
||||
| 212 | |||||
| 213 | if (empty($this->target)) { |
||||
|
0 ignored issues
–
show
The property
target does not exist on O2System\Kernel\Http\Message\Request. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 214 | if ($this->uri instanceof Uri) { |
||||
|
0 ignored issues
–
show
|
|||||
| 215 | $requestTarget = $this->uri->getPath(); |
||||
| 216 | |||||
| 217 | if (null !== ($query = $this->uri->getQuery())) { |
||||
|
0 ignored issues
–
show
|
|||||
| 218 | $requestTarget .= '?' . $query; |
||||
| 219 | } |
||||
| 220 | } |
||||
| 221 | } |
||||
| 222 | |||||
| 223 | return $requestTarget; |
||||
| 224 | } |
||||
| 225 | |||||
| 226 | // ------------------------------------------------------------------------ |
||||
| 227 | |||||
| 228 | /** |
||||
| 229 | * RequestInterface::withRequestTarget |
||||
| 230 | * |
||||
| 231 | * Return an instance with the specific request-target. |
||||
| 232 | * |
||||
| 233 | * If the request needs a non-origin-form request-target — e.g., for |
||||
| 234 | * specifying an absolute-form, authority-form, or asterisk-form — |
||||
| 235 | * this method may be used to create an instance with the specified |
||||
| 236 | * request-target, verbatim. |
||||
| 237 | * |
||||
| 238 | * This method MUST be implemented in such a way as to retain the |
||||
| 239 | * immutability of the message, and MUST return an instance that has the |
||||
| 240 | * changed request target. |
||||
| 241 | * |
||||
| 242 | * @see http://tools.ietf.org/html/rfc7230#section-5.3 (for the various |
||||
| 243 | * request-target forms allowed in request messages) |
||||
| 244 | * |
||||
| 245 | * @param mixed $requestTarget |
||||
| 246 | * |
||||
| 247 | * @return static |
||||
| 248 | */ |
||||
| 249 | public function withRequestTarget($requestTarget) |
||||
| 250 | { |
||||
| 251 | $requestTarget = trim($requestTarget); |
||||
| 252 | $parseTarget = parse_url($requestTarget); |
||||
| 253 | |||||
| 254 | $uri = $this->uri; |
||||
| 255 | |||||
| 256 | if (isset($parseTarget[ 'path' ])) { |
||||
| 257 | $uri = $this->uri->withPath($parseTarget[ 'path' ]); |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | if (isset($parseTarget[ 'query' ])) { |
||||
| 261 | $uri = $this->uri->withPath($parseTarget[ 'query' ]); |
||||
| 262 | } |
||||
| 263 | |||||
| 264 | $this->uri = $uri; |
||||
| 265 | |||||
| 266 | return $this; |
||||
| 267 | } |
||||
| 268 | |||||
| 269 | // ------------------------------------------------------------------------ |
||||
| 270 | |||||
| 271 | /** |
||||
| 272 | * RequestInterface::getMethod |
||||
| 273 | * |
||||
| 274 | * Retrieves the HTTP method of the request. |
||||
| 275 | * |
||||
| 276 | * @return string Returns the request method. |
||||
| 277 | */ |
||||
| 278 | public function getMethod() |
||||
| 279 | { |
||||
| 280 | return $this->method; |
||||
| 281 | } |
||||
| 282 | |||||
| 283 | // ------------------------------------------------------------------------ |
||||
| 284 | |||||
| 285 | /** |
||||
| 286 | * RequestInterface::withMethod |
||||
| 287 | * |
||||
| 288 | * Return an instance with the provided HTTP method. |
||||
| 289 | * |
||||
| 290 | * While HTTP method names are typically all uppercase characters, HTTP |
||||
| 291 | * method names are case-sensitive and thus implementations SHOULD NOT |
||||
| 292 | * modify the given string. |
||||
| 293 | * |
||||
| 294 | * This method MUST be implemented in such a way as to retain the |
||||
| 295 | * immutability of the message, and MUST return an instance that has the |
||||
| 296 | * changed request method. |
||||
| 297 | * |
||||
| 298 | * @param string $method Case-sensitive method. |
||||
| 299 | * |
||||
| 300 | * @return static |
||||
| 301 | * @throws \InvalidArgumentException for invalid HTTP methods. |
||||
| 302 | */ |
||||
| 303 | public function withMethod($method) |
||||
| 304 | { |
||||
| 305 | $method = strtoupper($method); |
||||
| 306 | |||||
| 307 | if (in_array( |
||||
| 308 | $method, |
||||
| 309 | [ |
||||
| 310 | 'OPTIONS', |
||||
| 311 | 'GET', |
||||
| 312 | 'HEAD', |
||||
| 313 | 'PATCH', |
||||
| 314 | 'POST', |
||||
| 315 | 'PUT', |
||||
| 316 | 'DELETE', |
||||
| 317 | 'TRACE', |
||||
| 318 | 'CONNECT', |
||||
| 319 | ] |
||||
| 320 | )) { |
||||
| 321 | $this->method = $method; |
||||
| 322 | |||||
| 323 | return $this; |
||||
| 324 | } |
||||
| 325 | |||||
| 326 | throw new \InvalidArgumentException('Invalid HTTP Method'); |
||||
| 327 | } |
||||
| 328 | |||||
| 329 | // ------------------------------------------------------------------------ |
||||
| 330 | |||||
| 331 | /** |
||||
| 332 | * RequestInterface::getUri |
||||
| 333 | * |
||||
| 334 | * Retrieves the URI instance. |
||||
| 335 | * |
||||
| 336 | * This method MUST return a UriInterface instance. |
||||
| 337 | * |
||||
| 338 | * @see http://tools.ietf.org/html/rfc3986#section-4.3 |
||||
| 339 | * @return Uri|\O2System\Kernel\Http\Message\Uri Returns a UriInterface instance |
||||
| 340 | * representing the URI of the request. |
||||
| 341 | */ |
||||
| 342 | public function &getUri() |
||||
| 343 | { |
||||
| 344 | if (empty($this->uri)) { |
||||
| 345 | $this->uri = new Uri(); |
||||
| 346 | } |
||||
| 347 | |||||
| 348 | return $this->uri; |
||||
| 349 | } |
||||
| 350 | |||||
| 351 | // ------------------------------------------------------------------------ |
||||
| 352 | |||||
| 353 | /** |
||||
| 354 | * Request::setUri |
||||
| 355 | * |
||||
| 356 | * @param \O2System\Psr\Http\Message\UriInterface $uri |
||||
| 357 | */ |
||||
| 358 | public function setUri(UriInterface $uri) |
||||
| 359 | { |
||||
| 360 | $this->uri = $uri; |
||||
|
0 ignored issues
–
show
$uri is of type O2System\Psr\Http\Message\UriInterface, but the property $uri was declared to be of type O2System\Kernel\Http\Message\Uri. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly. Either this assignment is in error or an instanceof check should be added for that assignment. class Alien {}
class Dalek extends Alien {}
class Plot
{
/** @var Dalek */
public $villain;
}
$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
$plot->villain = $alien;
}
Loading history...
|
|||||
| 361 | } |
||||
| 362 | |||||
| 363 | // ------------------------------------------------------------------------ |
||||
| 364 | |||||
| 365 | /** |
||||
| 366 | * RequestInterface::withUri |
||||
| 367 | * |
||||
| 368 | * Returns an instance with the provided URI. |
||||
| 369 | * |
||||
| 370 | * This method MUST update the Host header of the returned request by |
||||
| 371 | * default if the URI contains a host component. If the URI does not |
||||
| 372 | * contain a host component, any pre-existing Host header MUST be carried |
||||
| 373 | * over to the returned request. |
||||
| 374 | * |
||||
| 375 | * You can opt-in to preserving the original state of the Host header by |
||||
| 376 | * setting `$preserveHost` to `true`. When `$preserveHost` is set to |
||||
| 377 | * `true`, this method interacts with the Host header in the following ways: |
||||
| 378 | * |
||||
| 379 | * - If the Host header is missing or empty, and the new URI contains |
||||
| 380 | * a host component, this method MUST update the Host header in the returned |
||||
| 381 | * request. |
||||
| 382 | * - If the Host header is missing or empty, and the new URI does not contain a |
||||
| 383 | * host component, this method MUST NOT update the Host header in the returned |
||||
| 384 | * request. |
||||
| 385 | * - If a Host header is present and non-empty, this method MUST NOT update |
||||
| 386 | * the Host header in the returned request. |
||||
| 387 | * |
||||
| 388 | * This method MUST be implemented in such a way as to retain the |
||||
| 389 | * immutability of the message, and MUST return an instance that has the |
||||
| 390 | * new UriInterface instance. |
||||
| 391 | * |
||||
| 392 | * @see http://tools.ietf.org/html/rfc3986#section-4.3 |
||||
| 393 | * |
||||
| 394 | * @param UriInterface $uri New request URI to use. |
||||
| 395 | * @param bool $preserveHost Preserve the original state of the Host header. |
||||
| 396 | * |
||||
| 397 | * @return static |
||||
| 398 | */ |
||||
| 399 | public function withUri(UriInterface $uri, $preserveHost = false) |
||||
| 400 | { |
||||
| 401 | $message = clone $this; |
||||
| 402 | $message->uri = $uri; |
||||
|
0 ignored issues
–
show
$uri is of type O2System\Psr\Http\Message\UriInterface, but the property $uri was declared to be of type O2System\Kernel\Http\Message\Uri. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly. Either this assignment is in error or an instanceof check should be added for that assignment. class Alien {}
class Dalek extends Alien {}
class Plot
{
/** @var Dalek */
public $villain;
}
$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
$plot->villain = $alien;
}
Loading history...
|
|||||
| 403 | |||||
| 404 | if ($preserveHost) { |
||||
| 405 | if (null !== ($host = $uri->getHost())) { |
||||
|
0 ignored issues
–
show
|
|||||
| 406 | if (null !== ($port = $uri->getPort())) { |
||||
| 407 | $host .= ':' . $port; |
||||
| 408 | } |
||||
| 409 | |||||
| 410 | $message->withHeader('Host', $host); |
||||
| 411 | } |
||||
| 412 | } |
||||
| 413 | |||||
| 414 | return $message; |
||||
| 415 | } |
||||
| 416 | |||||
| 417 | //-------------------------------------------------------------------- |
||||
| 418 | |||||
| 419 | /** |
||||
| 420 | * Request::getIterator |
||||
| 421 | * |
||||
| 422 | * Retrieve an external iterator |
||||
| 423 | * |
||||
| 424 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||||
| 425 | * @return \Traversable An instance of an object implementing <b>Iterator</b> or |
||||
| 426 | * <b>Traversable</b> |
||||
| 427 | * @since 5.0.0 |
||||
| 428 | */ |
||||
| 429 | public function getIterator() |
||||
| 430 | { |
||||
| 431 | return new \ArrayIterator($_REQUEST); |
||||
| 432 | } |
||||
| 433 | |||||
| 434 | //-------------------------------------------------------------------- |
||||
| 435 | |||||
| 436 | /** |
||||
| 437 | * Request::count |
||||
| 438 | * |
||||
| 439 | * Count elements of an object |
||||
| 440 | * |
||||
| 441 | * @link http://php.net/manual/en/countable.count.php |
||||
| 442 | * @return int The custom count as an integer. |
||||
| 443 | * </p> |
||||
| 444 | * <p> |
||||
| 445 | * The return value is cast to an integer. |
||||
| 446 | * @since 5.1.0 |
||||
| 447 | */ |
||||
| 448 | public function count() |
||||
| 449 | { |
||||
| 450 | return count($_REQUEST); |
||||
| 451 | } |
||||
| 452 | } |