| Total Complexity | 101 |
| Total Lines | 900 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Request |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Holds the global active request instance. |
||
| 44 | * |
||
| 45 | * @var bool $requestURI |
||
| 46 | */ |
||
| 47 | protected static $requestURI; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The base URL. |
||
| 51 | * |
||
| 52 | * @var string $baseUrl |
||
| 53 | */ |
||
| 54 | protected $baseUrl; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Gets cookies ($_COOKIE). |
||
| 58 | * |
||
| 59 | * @var string $cookies |
||
| 60 | */ |
||
| 61 | public $cookies; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Gets the string with format JSON. |
||
| 65 | * |
||
| 66 | * @var string|resource|null $content |
||
| 67 | */ |
||
| 68 | protected $content; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The default Locale this request. |
||
| 72 | * |
||
| 73 | * @var string $defaultLocale |
||
| 74 | */ |
||
| 75 | protected $defaultLocale = 'en'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Gets files request ($_FILES). |
||
| 79 | * |
||
| 80 | * @var string $files |
||
| 81 | */ |
||
| 82 | public $files; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The detected uri and server variables. |
||
| 86 | * |
||
| 87 | * @var string $http |
||
| 88 | */ |
||
| 89 | protected $http; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The decoded JSON content for the request. |
||
| 93 | * |
||
| 94 | * @var \Syscodes\Http\Contributors\Parameters|null $json |
||
| 95 | */ |
||
| 96 | protected $json; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The current language of the application. |
||
| 100 | * |
||
| 101 | * @var string $languages |
||
| 102 | */ |
||
| 103 | protected $languages; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The method name. |
||
| 107 | * |
||
| 108 | * @var string $method |
||
| 109 | */ |
||
| 110 | protected $method; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The path info of URL. |
||
| 114 | * |
||
| 115 | * @var string $pathInfo |
||
| 116 | */ |
||
| 117 | protected $pathInfo; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Request body parameters ($_POST). |
||
| 121 | * |
||
| 122 | * @var \Syscodes\Http\Contributors\Parameters $request |
||
| 123 | */ |
||
| 124 | public $request; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get request URI. |
||
| 128 | * |
||
| 129 | * @var string $requestToURI |
||
| 130 | */ |
||
| 131 | protected $requestToURI; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The detected uri and server variables ($_FILES). |
||
| 135 | * |
||
| 136 | * @var array $server |
||
| 137 | */ |
||
| 138 | public $server = []; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * List of routes uri. |
||
| 142 | * |
||
| 143 | * @var string|array $uri |
||
| 144 | */ |
||
| 145 | public $uri; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Stores the valid locale codes. |
||
| 149 | * |
||
| 150 | * @var array $validLocales |
||
| 151 | */ |
||
| 152 | protected $validLocales = []; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Constructor: Create new the Request class. |
||
| 156 | * |
||
| 157 | * @param array $request |
||
| 158 | * @param array $cookies |
||
| 159 | * @param array $files |
||
| 160 | * @param array $server |
||
| 161 | * @param string|resource|null $content (null by default) |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function __construct(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
||
| 166 | { |
||
| 167 | static::$requestURI = $this; |
||
|
|
|||
| 168 | |||
| 169 | $this->initialize($request, $cookies, $files, $server, $content); |
||
| 170 | |||
| 171 | $this->detectURI(config('app.uriProtocol'), config('app.baseUrl')); |
||
| 172 | |||
| 173 | $this->detectLocale(); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Sets the parameters for this request. |
||
| 178 | * |
||
| 179 | * @param array $request |
||
| 180 | * @param array $cookies |
||
| 181 | * @param array $files |
||
| 182 | * @param array $server |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public function initialize(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
||
| 187 | { |
||
| 188 | $this->request = new Parameters($request); |
||
| 189 | $this->cookies = new Inputs($cookies); |
||
| 190 | $this->files = new Files($files); |
||
| 191 | $this->server = new Server($server); |
||
| 192 | $this->headers = new Headers($this->server->all()); |
||
| 193 | |||
| 194 | $this->uri = new URI; |
||
| 195 | $this->http = new Http; |
||
| 196 | $this->method = null; |
||
| 197 | $this->baseUrl = null; |
||
| 198 | $this->content = $content; |
||
| 199 | $this->pathInfo = null; |
||
| 200 | $this->languages = null; |
||
| 201 | $this->validLocales = config('app.supportedLocales'); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Create a new Syscodes HTTP request from server variables. |
||
| 206 | * |
||
| 207 | * @return static |
||
| 208 | */ |
||
| 209 | public static function capture() |
||
| 210 | { |
||
| 211 | return static::createFromRequest(static::createFromRequestGlobals()); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Creates an Syscodes request from of the Request class instance. |
||
| 216 | * |
||
| 217 | * @param \Syscodes\Http\Request $request |
||
| 218 | * |
||
| 219 | * @return static |
||
| 220 | */ |
||
| 221 | public static function createFromRequest($request) |
||
| 222 | { |
||
| 223 | $newRequest = (new static)->duplicate( |
||
| 224 | $request->request->all(), $request->cookies->all(), |
||
| 225 | $request->files->all(), $request->server->all() |
||
| 226 | ); |
||
| 227 | |||
| 228 | $newRequest->content = $request->content; |
||
| 229 | |||
| 230 | return $newRequest; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Creates a new request with value from PHP's super global. |
||
| 235 | * |
||
| 236 | * @return static |
||
| 237 | */ |
||
| 238 | public static function createFromRequestGlobals() |
||
| 239 | { |
||
| 240 | $request = static::createFromRequestFactory($_POST, $_COOKIE, $_FILES, $_SERVER); |
||
| 241 | |||
| 242 | return $request; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Creates a new request from a factory. |
||
| 247 | * |
||
| 248 | * @param array $request |
||
| 249 | * @param array $cookies |
||
| 250 | * @param array $files |
||
| 251 | * @param array $server |
||
| 252 | * |
||
| 253 | * @return static |
||
| 254 | */ |
||
| 255 | protected static function createFromRequestFactory(array $request = [], array $cookies = [], array $files = [], array $server = []) |
||
| 256 | { |
||
| 257 | if (self::$requestURI) |
||
| 258 | { |
||
| 259 | $request = (self::$requestURI)($request, $cookies, $files, $server); |
||
| 260 | |||
| 261 | if ( ! $request instanceof self) |
||
| 262 | { |
||
| 263 | throw new LogicException('The Request active must return an instance of Syscodes\Http\Request'); |
||
| 264 | } |
||
| 265 | |||
| 266 | return $request; |
||
| 267 | } |
||
| 268 | |||
| 269 | return new static($request, $cookies, $files, $server); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Clones a request and overrides some of its parameters. |
||
| 274 | * |
||
| 275 | * @param array $request |
||
| 276 | * @param array $cookies |
||
| 277 | * @param array $files |
||
| 278 | * @param array $server |
||
| 279 | * |
||
| 280 | * @return static |
||
| 281 | */ |
||
| 282 | public function duplicate(array $request = [], array $cookies = [], array $files = [], array $server = []) |
||
| 283 | { |
||
| 284 | $duplicate = clone $this; |
||
| 285 | |||
| 286 | if (null !== $request) |
||
| 287 | { |
||
| 288 | $duplicate->request = new Parameters($request); |
||
| 289 | } |
||
| 290 | |||
| 291 | if (null !== $cookies) |
||
| 292 | { |
||
| 293 | $duplicate->cookies = new Inputs($cookies); |
||
| 294 | } |
||
| 295 | |||
| 296 | if (null !== $files) |
||
| 297 | { |
||
| 298 | $duplicate->files = new Files($files); |
||
| 299 | } |
||
| 300 | |||
| 301 | if (null !== $server) |
||
| 302 | { |
||
| 303 | $duplicate->server = new Server($server); |
||
| 304 | $duplicate->headers = new Headers($duplicate->server->all()); |
||
| 305 | } |
||
| 306 | |||
| 307 | $duplicate->uri = new URI; |
||
| 308 | $duplicate->http = new Http; |
||
| 309 | $duplicate->locale = null; |
||
| 310 | $duplicate->method = null; |
||
| 311 | $duplicate->baseUrl = null; |
||
| 312 | $duplicate->pathInfo = null; |
||
| 313 | $duplicate->validLocales = config('app.supportedLocales'); |
||
| 314 | |||
| 315 | return $duplicate; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the active request currently being used. |
||
| 320 | * |
||
| 321 | * @param \Syscodes\Http\Request|bool|null $request Overwrite current request |
||
| 322 | * before returning, false prevents |
||
| 323 | * overwrite |
||
| 324 | * |
||
| 325 | * @return \Syscodes\Http\Request |
||
| 326 | */ |
||
| 327 | public static function active($request = false) |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns the desired segment, or $default if it does not exist. |
||
| 339 | * |
||
| 340 | * @param int $index The segment number (1-based index) |
||
| 341 | * @param mixed $default Default value to return |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function segment($index, $default = null) |
||
| 346 | { |
||
| 347 | if ($request = static::active()) |
||
| 348 | { |
||
| 349 | return $request->uri->getSegment($index, $default); |
||
| 350 | } |
||
| 351 | |||
| 352 | return null; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns all segments in an array. For total of segments |
||
| 357 | * used the function PHP count(). |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public function segments() |
||
| 362 | { |
||
| 363 | if ($request = static::active()) |
||
| 364 | { |
||
| 365 | return $request->uri->getSegments(); |
||
| 366 | } |
||
| 367 | |||
| 368 | return null; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the total number of segment. |
||
| 373 | * |
||
| 374 | * @return int|null |
||
| 375 | */ |
||
| 376 | public function totalSegments() |
||
| 377 | { |
||
| 378 | if ($request = static::active()) |
||
| 379 | { |
||
| 380 | return $request->uri->getTotalSegments(); |
||
| 381 | } |
||
| 382 | |||
| 383 | return null; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Detects and returns the current URI based on a number of different server variables. |
||
| 388 | * |
||
| 389 | * @param string $protocol |
||
| 390 | * @param string $baseUrl |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function detectURI(string $protocol, string $baseUrl) |
||
| 395 | { |
||
| 396 | $this->uri->setPath($this->http->detectPath($protocol)); |
||
| 397 | |||
| 398 | $baseUrl = ! empty($baseUrl) ? rtrim($baseUrl, '/ ').'/' : $baseUrl; |
||
| 399 | |||
| 400 | if ( ! empty($baseUrl)) |
||
| 401 | { |
||
| 402 | $this->uri->setScheme(parse_url($baseUrl, PHP_URL_SCHEME)); |
||
| 403 | $this->uri->setHost(parse_url($baseUrl, PHP_URL_HOST)); |
||
| 404 | $this->uri->setPort(parse_url($baseUrl, PHP_URL_PORT)); |
||
| 405 | } |
||
| 406 | else |
||
| 407 | { |
||
| 408 | if ( ! $this->http->isCli()) |
||
| 409 | { |
||
| 410 | exit('You have an empty or invalid base URL. The baseURL value must be set in config/app.php, or through the .env file.'); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Handles setting up the locale, auto-detecting of language. |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function detectLocale() |
||
| 421 | { |
||
| 422 | $this->languages = $this->defaultLocale = config('app.locale'); |
||
| 423 | |||
| 424 | $this->setLocale($this->validLocales); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns the default locale as set. |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function getDefaultLocale() |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Gets the current locale, with a fallback to the default. |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getLocale() |
||
| 443 | { |
||
| 444 | return $this->languages ?: $this->defaultLocale; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Sets the locale string for this request. |
||
| 449 | * |
||
| 450 | * @param string $locale |
||
| 451 | * |
||
| 452 | * @return \Syscodes\Http\Request |
||
| 453 | */ |
||
| 454 | public function setLocale($locale) |
||
| 455 | { |
||
| 456 | if ( ! in_array($locale, $this->validLocales)) |
||
| 457 | { |
||
| 458 | $locale = $this->defaultLocale; |
||
| 459 | } |
||
| 460 | |||
| 461 | $this->languages = $locale; |
||
| 462 | |||
| 463 | try |
||
| 464 | { |
||
| 465 | if (class_exists('Locale', false)) |
||
| 466 | { |
||
| 467 | Locale::setDefault($locale); |
||
| 468 | } |
||
| 469 | } |
||
| 470 | catch (Exception $exception) {} |
||
| 471 | |||
| 472 | return $this; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Returns the full request string. |
||
| 477 | * |
||
| 478 | * @return string|null The Request string |
||
| 479 | */ |
||
| 480 | public function get() |
||
| 481 | { |
||
| 482 | if ($request = static::active()) |
||
| 483 | { |
||
| 484 | return $request->uri->get(); |
||
| 485 | } |
||
| 486 | |||
| 487 | return null; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the JSON payload for the request. |
||
| 492 | * |
||
| 493 | * @param string|null $key (null by default) |
||
| 494 | * @param mixed $default (null by default) |
||
| 495 | * |
||
| 496 | * @return \Syscodes\Http\Contributors\Parameters|mixed |
||
| 497 | */ |
||
| 498 | public function json($key = null, $default = null) |
||
| 499 | { |
||
| 500 | if ( ! isset($this->json)) { |
||
| 501 | $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
||
| 502 | } |
||
| 503 | |||
| 504 | if (is_null($key)) { |
||
| 505 | return $this->json; |
||
| 506 | } |
||
| 507 | |||
| 508 | return Arr::get($this->json->all(), $key, $default); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Set the JSON payload for the request |
||
| 513 | * |
||
| 514 | * @param \Syscodes\Http\Contributors\Parameters $json |
||
| 515 | * |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | public function setJson($json) |
||
| 519 | { |
||
| 520 | $this->json = $json; |
||
| 521 | |||
| 522 | return $this; |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Returns whether this is an AJAX request or not. |
||
| 527 | * |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function isXmlHttpRequest() |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Returns the input method used (GET, POST, DELETE, etc.). |
||
| 538 | * |
||
| 539 | * @return string |
||
| 540 | * |
||
| 541 | * @throws \LogicException |
||
| 542 | */ |
||
| 543 | public function getmethod() |
||
| 544 | { |
||
| 545 | if (null !== $this->method) |
||
| 546 | { |
||
| 547 | return $this->method; |
||
| 548 | } |
||
| 549 | |||
| 550 | $method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
||
| 551 | |||
| 552 | if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) |
||
| 553 | { |
||
| 554 | return $this->method = $method; |
||
| 555 | } |
||
| 556 | |||
| 557 | if ( ! preg_match('~^[A-Z]++$#~D', $method)) |
||
| 558 | { |
||
| 559 | throw new logicException(sprintf('Invalid method override "%s"', $method)); |
||
| 560 | } |
||
| 561 | |||
| 562 | return $this->method = $method; |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Sets the request method. |
||
| 567 | * |
||
| 568 | * @param string $method |
||
| 569 | * |
||
| 570 | * @return string |
||
| 571 | */ |
||
| 572 | public function setMethod(string $method) |
||
| 573 | { |
||
| 574 | $this->method = null; |
||
| 575 | |||
| 576 | $this->server->set('REQUEST_METHOD', $method); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Determine if the current request URI matches a pattern. |
||
| 581 | * |
||
| 582 | * @param mixed ...$patterns |
||
| 583 | * |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | public function is(...$patterns) |
||
| 587 | { |
||
| 588 | $path = $this->decodedPath(); |
||
| 589 | |||
| 590 | foreach ($patterns as $pattern) |
||
| 591 | { |
||
| 592 | if (Str::is($pattern, $path)) |
||
| 593 | { |
||
| 594 | return true; |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | return false; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Determine if the route name matches a given pattern. |
||
| 603 | * |
||
| 604 | * @param mixed ...$patterns |
||
| 605 | * |
||
| 606 | * @return bool |
||
| 607 | */ |
||
| 608 | public function routeIs(...$patterns) |
||
| 609 | { |
||
| 610 | return $this->route() && $this->route()->is(...$patterns); |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Get the route handling the request. |
||
| 615 | * |
||
| 616 | * @param string|null $param (null by default) |
||
| 617 | * @param mixed $default (null by default) |
||
| 618 | * |
||
| 619 | * @return \Syscodes\Routing\Route|object|string|null |
||
| 620 | */ |
||
| 621 | public function route($param = null, $default = null) |
||
| 622 | { |
||
| 623 | $route = $this->getRoute(); |
||
| 624 | |||
| 625 | if (is_null($route) || is_null($param)) |
||
| 626 | { |
||
| 627 | return $route; |
||
| 628 | } |
||
| 629 | |||
| 630 | return $route->parameter($param, $default); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Get the current decoded path info for the request. |
||
| 635 | * |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public function decodedPath() |
||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get the current path info for the request. |
||
| 645 | * |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | public function path() |
||
| 649 | { |
||
| 650 | $path = trim($this->getPathInfo(), '/'); |
||
| 651 | |||
| 652 | return $path == '' ? '/' : $path; |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Retunrs the request body content. |
||
| 657 | * |
||
| 658 | * @return string |
||
| 659 | */ |
||
| 660 | public function getContent() |
||
| 661 | { |
||
| 662 | if (null === $this->content || false === $this->content) |
||
| 663 | { |
||
| 664 | $this->content = file_get_contents('php://input'); |
||
| 665 | } |
||
| 666 | |||
| 667 | return $this->content; |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Returns the path being requested relative to the executed script. |
||
| 672 | * |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | public function getPathInfo() |
||
| 676 | { |
||
| 677 | if (null === $this->pathInfo) |
||
| 678 | { |
||
| 679 | $this->pathInfo = $this->http->parsePathInfo(); |
||
| 680 | } |
||
| 681 | |||
| 682 | return $this->pathInfo; |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Returns the root URL from which this request is executed. |
||
| 687 | * |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | public function getBaseUrl() |
||
| 691 | { |
||
| 692 | if (null === $this->baseUrl) |
||
| 693 | { |
||
| 694 | $this->baseUrl = $this->http->parseBaseUrl(); |
||
| 695 | } |
||
| 696 | |||
| 697 | return $this->baseUrl; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns the requested URI. |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | public function getRequestUri() |
||
| 706 | { |
||
| 707 | if (null === $this->requestToUri) |
||
| 708 | { |
||
| 709 | $this->requestToUri = $this->http->parseRequestUri(); |
||
| 710 | } |
||
| 711 | |||
| 712 | return $this->requestToUri; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Gets the request's scheme. |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | public function getScheme() |
||
| 721 | { |
||
| 722 | return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
||
| 723 | } |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Returns the host name. |
||
| 727 | * |
||
| 728 | * @return void |
||
| 729 | */ |
||
| 730 | public function getHost() |
||
| 731 | { |
||
| 732 | if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
||
| 733 | $host = $forawardedHost[0]; |
||
| 734 | } |
||
| 735 | elseif ( ! $host = $this->headers->get('HOST')) { |
||
| 736 | if ( ! $host = $this->server->get('SERVER_NAME')) { |
||
| 737 | $host = $this->server->get('REMOTE_ADDR', ''); |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | $host = $_SERVER['SERVER_NAME']; |
||
| 742 | |||
| 743 | $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
||
| 744 | |||
| 745 | return $host; |
||
| 746 | } |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Returns the port on which the request is made. |
||
| 750 | * |
||
| 751 | * @return int |
||
| 752 | */ |
||
| 753 | public function getPort() |
||
| 754 | { |
||
| 755 | if ( ! $this->server->get('HTTP_HOST')) |
||
| 756 | { |
||
| 757 | return $this->server->get('SERVER_PORT'); |
||
| 758 | } |
||
| 759 | |||
| 760 | return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Returns the HTTP host being requested. |
||
| 765 | * |
||
| 766 | * @return string |
||
| 767 | */ |
||
| 768 | public function getHttpHost() |
||
| 769 | { |
||
| 770 | $scheme = $this->getScheme(); |
||
| 771 | $port = $this->getPort(); |
||
| 772 | |||
| 773 | if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) |
||
| 774 | { |
||
| 775 | return $this->getHost(); |
||
| 776 | } |
||
| 777 | |||
| 778 | return $this->getHost().':'.$port; |
||
| 779 | } |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Gets the scheme and HTTP host. |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | public function getSchemeWithHttpHost() |
||
| 787 | { |
||
| 788 | return $this->getScheme().'://'.$this->getHttpHost(); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Get the root URL for the application. |
||
| 793 | * |
||
| 794 | * @return string |
||
| 795 | */ |
||
| 796 | public function root() |
||
| 797 | { |
||
| 798 | return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Get the URL for the request. |
||
| 803 | * |
||
| 804 | * @return string |
||
| 805 | */ |
||
| 806 | public function url() |
||
| 807 | { |
||
| 808 | return trim(preg_replace('/\?.*/', '', $this->get()), '/'); |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Returns the referer. |
||
| 813 | * |
||
| 814 | * @param string $default |
||
| 815 | * |
||
| 816 | * @return string |
||
| 817 | */ |
||
| 818 | public function referer(string $default = '') |
||
| 819 | { |
||
| 820 | return $this->server->get('HTTP_REFERER', $default); |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Attempts to detect if the current connection is secure through |
||
| 825 | * over HTTPS protocol. |
||
| 826 | * |
||
| 827 | * @return bool |
||
| 828 | */ |
||
| 829 | public function secure() |
||
| 830 | { |
||
| 831 | if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') |
||
| 832 | { |
||
| 833 | return true; |
||
| 834 | } |
||
| 835 | elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') |
||
| 836 | { |
||
| 837 | return true; |
||
| 838 | } |
||
| 839 | elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') |
||
| 840 | { |
||
| 841 | return true; |
||
| 842 | } |
||
| 843 | |||
| 844 | return false; |
||
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Returns the user agent. |
||
| 849 | * |
||
| 850 | * @param string|null $default |
||
| 851 | * |
||
| 852 | * @return string |
||
| 853 | */ |
||
| 854 | public function userAgent(string $default = null) |
||
| 855 | { |
||
| 856 | return $this->server->get('HTTP_USER_AGENT', $default); |
||
| 857 | } |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Get the route resolver. |
||
| 861 | * |
||
| 862 | * @return \Syscodes\Routing\Router |
||
| 863 | */ |
||
| 864 | public function getRoute() |
||
| 865 | { |
||
| 866 | return app('router'); |
||
| 867 | } |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Get an element from the request. |
||
| 871 | * |
||
| 872 | * @return string[] |
||
| 873 | */ |
||
| 874 | public function __get($key) |
||
| 875 | { |
||
| 876 | $all = $this->server->all(); |
||
| 877 | |||
| 878 | if (array_key_exists($key, $all)) |
||
| 879 | { |
||
| 880 | return $all[$key]; |
||
| 881 | } |
||
| 882 | else |
||
| 883 | { |
||
| 884 | return $key; |
||
| 885 | } |
||
| 886 | } |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Returns the Request as an HTTP string. |
||
| 890 | * |
||
| 891 | * @return string |
||
| 892 | */ |
||
| 893 | public function __toString() |
||
| 894 | { |
||
| 895 | try |
||
| 896 | { |
||
| 897 | $content = $this->getContent(); |
||
| 898 | } |
||
| 899 | catch (LogicException $e) |
||
| 900 | { |
||
| 901 | if (PHP_VERSION_ID > 70400) |
||
| 902 | { |
||
| 903 | throw $e; |
||
| 904 | } |
||
| 905 | |||
| 906 | return trigger_error($e, E_USER_ERROR); |
||
| 907 | } |
||
| 908 | |||
| 909 | $cookieHeader = ''; |
||
| 910 | $cookies = []; |
||
| 911 | |||
| 912 | foreach ($this->cookies as $key => $value) |
||
| 913 | { |
||
| 914 | $cookies[]= "{$key} = {$value}"; |
||
| 915 | } |
||
| 916 | |||
| 917 | if ( ! empty($cookies)) |
||
| 918 | { |
||
| 919 | $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
||
| 920 | } |
||
| 921 | |||
| 922 | return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
||
| 923 | $this->headers. |
||
| 924 | $cookieHeader."\r\n". |
||
| 925 | $content; |
||
| 926 | } |
||
| 927 | |||
| 928 | /** |
||
| 929 | * Clones the current request. |
||
| 930 | * |
||
| 931 | * @return void |
||
| 932 | */ |
||
| 933 | public function __clone() |
||
| 940 | } |
||
| 941 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..