Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
request.php
1<?php
2
10namespace Bitrix\Main\Web\Http;
11
12use Psr\Http\Message\UriInterface;
13use Psr\Http\Message\RequestInterface;
14use Psr\Http\Message\StreamInterface;
15
16class Request extends Message implements RequestInterface
17{
18 protected $requestTarget;
19 protected UriInterface $uri;
20 protected $method;
21
22 public function __construct(string $method, UriInterface $uri, array $headers = null, StreamInterface $body = null, string $version = null)
23 {
24 parent::__construct($headers, $body, $version);
25
26 $this->method = $method;
27 $this->uri = $uri;
28
29 // PSR-7: During construction, implementations MUST attempt to set the Host header from a provided URI if no Host header is provided.
30 if ($uri->getHost() != '' && !$this->hasHeader('Host'))
31 {
32 $this->headers->set('Host', $uri->getHost());
33 }
34 }
35
39 public function getRequestTarget(): string
40 {
41 if ($this->requestTarget !== null)
42 {
43 return $this->requestTarget;
44 }
45
46 $target = $this->uri->getPath();
47
48 if ($target == '')
49 {
50 $target = '/';
51 }
52
53 $query = $this->uri->getQuery();
54
55 if ($query != '')
56 {
57 $target .= '?' . $query;
58 }
59
60 return $target;
61 }
62
66 public function withRequestTarget(string $requestTarget): RequestInterface
67 {
68 $new = clone $this;
69 $new->requestTarget = $requestTarget;
70
71 return $new;
72 }
73
77 public function getMethod(): string
78 {
79 return $this->method;
80 }
81
85 public function withMethod(string $method): RequestInterface
86 {
87 $new = clone $this;
88 $new->method = $method;
89
90 return $new;
91 }
92
96 public function getUri(): UriInterface
97 {
98 return $this->uri;
99 }
100
104 public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface
105 {
106 $new = clone $this;
107 $new->uri = $uri;
108
109 $newHost = $uri->getHost();
110
111 if ($newHost != '')
112 {
113 if (!$preserveHost || !$new->hasHeader('Host'))
114 {
115 $new->headers->set('Host', $newHost);
116 }
117 }
118
119 return $new;
120 }
121
122 public function __clone()
123 {
124 $this->uri = clone $this->uri;
125 parent::__clone();
126 }
127}
StreamInterface $body
Definition message.php:19
withUri(UriInterface $uri, bool $preserveHost=false)
Definition request.php:104
__construct(string $method, UriInterface $uri, array $headers=null, StreamInterface $body=null, string $version=null)
Definition request.php:22
withRequestTarget(string $requestTarget)
Definition request.php:66
withMethod(string $method)
Definition request.php:85