1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * This file is part of the FOSHttpCache package. |
||||
5 | * |
||||
6 | * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
||||
7 | * |
||||
8 | * For the full copyright and license information, please view the LICENSE |
||||
9 | * file that was distributed with this source code. |
||||
10 | */ |
||||
11 | |||||
12 | namespace FOS\HttpCache\Test; |
||||
13 | |||||
14 | use Http\Client\HttpClient as PhpHttpClient; |
||||
15 | use Http\Discovery\HttpClientDiscovery; |
||||
16 | use Http\Discovery\MessageFactoryDiscovery; |
||||
17 | use Http\Discovery\UriFactoryDiscovery; |
||||
18 | use Psr\Http\Message\RequestInterface; |
||||
19 | use Psr\Http\Message\ResponseInterface; |
||||
20 | use Psr\Http\Message\UriInterface; |
||||
21 | |||||
22 | /** |
||||
23 | * Provides a very simple way to fetch HTTP responses, auto-creating a client. |
||||
24 | */ |
||||
25 | class HttpClient |
||||
26 | { |
||||
27 | /** |
||||
28 | * HTTP client for requests to the application. |
||||
29 | * |
||||
30 | * @var PhpHttpClient |
||||
31 | */ |
||||
32 | private $httpClient; |
||||
33 | |||||
34 | /** |
||||
35 | * @var string |
||||
36 | */ |
||||
37 | private $hostname; |
||||
38 | |||||
39 | /** |
||||
40 | * @var string |
||||
41 | */ |
||||
42 | private $port; |
||||
43 | |||||
44 | /** |
||||
45 | * @param string $hostname Default hostname if not specified in the URL |
||||
46 | * @param string $port Default port if not specified in the URL |
||||
47 | */ |
||||
48 | 33 | public function __construct($hostname, $port) |
|||
49 | { |
||||
50 | 33 | $this->hostname = $hostname; |
|||
51 | 33 | $this->port = $port; |
|||
52 | 33 | } |
|||
53 | |||||
54 | /** |
||||
55 | * Get HTTP response from your application. |
||||
56 | * |
||||
57 | * @param string $uri HTTP URI |
||||
58 | * @param array $headers HTTP headers |
||||
59 | * @param string $method HTTP method |
||||
60 | * |
||||
61 | * @return ResponseInterface |
||||
62 | */ |
||||
63 | 33 | public function getResponse($uri, array $headers, $method) |
|||
64 | { |
||||
65 | 33 | $request = $this->createRequest($method, $uri, $headers); |
|||
66 | |||||
67 | 33 | return $this->sendRequest($request); |
|||
68 | } |
||||
69 | |||||
70 | /** |
||||
71 | * Send PSR HTTP request to your application. |
||||
72 | * |
||||
73 | * @return ResponseInterface |
||||
74 | */ |
||||
75 | 33 | public function sendRequest(RequestInterface $request) |
|||
76 | { |
||||
77 | // Close connections to make sure invalidation (PURGE/BAN) requests will |
||||
78 | // not interfere with content (GET) requests. |
||||
79 | 33 | $request = $request->withHeader('Connection', 'Close'); |
|||
80 | |||||
81 | 33 | return $this->getHttpClient()->sendRequest($request); |
|||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * Get HTTP client for your application. |
||||
86 | * |
||||
87 | * @return PhpHttpClient |
||||
88 | */ |
||||
89 | 33 | private function getHttpClient() |
|||
90 | { |
||||
91 | 33 | if (null === $this->httpClient) { |
|||
92 | 33 | $this->httpClient = HttpClientDiscovery::find(); |
|||
93 | } |
||||
94 | |||||
95 | 33 | return $this->httpClient; |
|||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * Create a request. |
||||
100 | * |
||||
101 | * @param string $method |
||||
102 | * @param string $uri |
||||
103 | * @param array $headers |
||||
104 | * |
||||
105 | * @return RequestInterface |
||||
106 | * |
||||
107 | * @throws \Exception |
||||
108 | */ |
||||
109 | 33 | private function createRequest($method, $uri, $headers) |
|||
110 | { |
||||
111 | 33 | $uri = $this->createUri($uri); |
|||
112 | 33 | if ('' === $uri->getHost()) { |
|||
113 | // Add base URI host |
||||
114 | 33 | $uri = $uri->withHost($this->hostname); |
|||
115 | } |
||||
116 | |||||
117 | 33 | if (!$uri->getPort()) { |
|||
0 ignored issues
–
show
|
|||||
118 | 33 | $uri = $uri->withPort($this->port); |
|||
0 ignored issues
–
show
$this->port of type string is incompatible with the type integer|null expected by parameter $port of Psr\Http\Message\UriInterface::withPort() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
119 | } |
||||
120 | |||||
121 | 33 | if ('' === $uri->getScheme()) { |
|||
122 | 33 | $uri = $uri->withScheme('http'); |
|||
123 | } |
||||
124 | |||||
125 | 33 | return MessageFactoryDiscovery::find()->createRequest( |
|||
126 | 33 | $method, |
|||
127 | $uri, |
||||
128 | $headers |
||||
129 | ); |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * Create PSR-7 URI object from URI string. |
||||
134 | * |
||||
135 | * @param string $uriString |
||||
136 | * |
||||
137 | * @return UriInterface |
||||
138 | */ |
||||
139 | 33 | private function createUri($uriString) |
|||
140 | { |
||||
141 | 33 | return UriFactoryDiscovery::find()->createUri($uriString); |
|||
142 | } |
||||
143 | } |
||||
144 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: