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\Discovery\HttpClientDiscovery; |
15
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
16
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
17
|
|
|
use Http\Client\HttpClient as PhpHttpClient; |
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 HttpClient |
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
|
29 |
|
public function __construct($hostname, $port) |
49
|
|
|
{ |
50
|
29 |
|
$this->hostname = $hostname; |
51
|
29 |
|
$this->port = $port; |
52
|
29 |
|
} |
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
|
29 |
|
public function getResponse($uri, array $headers, $method) |
64
|
|
|
{ |
65
|
|
|
// Close connections to make sure invalidation (PURGE/BAN) requests will |
66
|
|
|
// not interfere with content (GET) requests. |
67
|
29 |
|
$headers['Connection'] = 'Close'; |
68
|
29 |
|
$request = $this->createRequest($method, $uri, $headers); |
69
|
|
|
|
70
|
|
|
return $this->getHttpClient()->sendRequest($request); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get HTTP adapter for your application |
75
|
|
|
* |
76
|
|
|
* @return PhpHttpClient |
77
|
|
|
*/ |
78
|
|
|
private function getHttpClient() |
79
|
|
|
{ |
80
|
|
|
if ($this->httpClient === null) { |
81
|
|
|
$this->httpClient = HttpClientDiscovery::find(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->httpClient; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a request |
89
|
|
|
* |
90
|
|
|
* @param string $method |
91
|
|
|
* @param string $uri |
92
|
|
|
* @param array $headers |
93
|
|
|
* |
94
|
|
|
* @return RequestInterface |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
29 |
|
private function createRequest($method, $uri, $headers) |
98
|
|
|
{ |
99
|
29 |
|
$uri = $this->createUri($uri); |
100
|
|
|
if ($uri->getHost() === '') { |
101
|
|
|
// Add base URI host |
102
|
|
|
$uri = $uri->withHost($this->hostname); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!$uri->getPort()) { |
|
|
|
|
106
|
|
|
$uri = $uri->withPort($this->port); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($uri->getScheme() === '') { |
110
|
|
|
$uri = $uri->withScheme('http'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return MessageFactoryDiscovery::find()->createRequest( |
114
|
|
|
$method, |
115
|
|
|
$uri, |
116
|
|
|
$headers |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Create PSR-7 URI object from URI string |
122
|
|
|
* |
123
|
|
|
* @param string $uriString |
124
|
|
|
* |
125
|
|
|
* @return UriInterface |
126
|
|
|
*/ |
127
|
29 |
|
private function createUri($uriString) |
128
|
|
|
{ |
129
|
29 |
|
return UriFactoryDiscovery::find()->createUri($uriString); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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..