|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\Curl; |
|
4
|
|
|
|
|
5
|
|
|
class ResponseFromCache extends Response |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var string */ |
|
8
|
|
|
protected $url; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @param mixed $headersSeparator could be a string (the separator between headers and content or FALSE |
|
12
|
|
|
* @param array<string, int|string> $info |
|
13
|
|
|
*/ |
|
14
|
3 |
|
public function __construct( |
|
15
|
|
|
string $filePathOrContent, |
|
16
|
|
|
?string $url = null, |
|
17
|
|
|
array $info = [], |
|
18
|
|
|
$headersSeparator = \PHP_EOL.\PHP_EOL |
|
19
|
|
|
) { |
|
20
|
3 |
|
$content = file_exists($filePathOrContent) ? file_get_contents($filePathOrContent) : $filePathOrContent; |
|
21
|
|
|
|
|
22
|
3 |
|
if (! $content) { |
|
23
|
|
|
throw new \Exception($filePathOrContent.' doesn\'t exist'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
3 |
|
if (false !== $headersSeparator && \is_string($headersSeparator) && '' !== $headersSeparator) { |
|
27
|
3 |
|
list($this->headers, $this->content) = explode($headersSeparator, $content, 2); |
|
28
|
|
|
} else { |
|
29
|
|
|
$this->content = $content; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
$this->info = $info; |
|
33
|
3 |
|
$this->url = (string) $url; |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function getRequest(): ?Request |
|
37
|
|
|
{ |
|
38
|
3 |
|
return null; // todo serialize request to return it even from cache |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getUrl(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->url; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getEffectiveUrl(): ?string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->url; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getStatusCode(): int |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->headers) { |
|
54
|
|
|
$headers = $this->getHeaders(); |
|
55
|
|
|
|
|
56
|
|
|
return (int) explode(' ', $headers[0], 2)[1]; // @phpstan-ignore-line |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return (int) $this->getInfo('http_code'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
3 |
|
* @psalm-suppress InvalidReturnStatement |
|
64
|
|
|
* @psalm-suppress NullableReturnStatement |
|
65
|
3 |
|
* @psalm-suppress InvalidReturnType |
|
66
|
3 |
|
* @psalm-suppress InvalidNullableReturnType |
|
67
|
3 |
|
*/ |
|
68
|
|
|
public function getContentType(): string |
|
69
|
|
|
{ |
|
70
|
|
|
$headers = $this->getHeaders(); |
|
71
|
|
|
if (null !== $headers && isset($headers['content-type'])) { |
|
72
|
3 |
|
return $headers['content-type']; // @phpstan-ignore-line |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->getInfo('content_type'); // @phpstan-ignore-line |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|