|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IQParts\Content; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Stream; |
|
6
|
|
|
use IQParts\Content\Object\CacheControl; |
|
7
|
|
|
use IQParts\Content\Object\File; |
|
8
|
|
|
use League\Flysystem\FileNotFoundException; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AssetResolver |
|
14
|
|
|
* @package IQParts\Framework\Content |
|
15
|
|
|
*/ |
|
16
|
|
|
final class AssetResolver |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var MimeDetector |
|
20
|
|
|
*/ |
|
21
|
|
|
private $detector; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Filesystem |
|
24
|
|
|
*/ |
|
25
|
|
|
private $filesystem; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* AssetResolver constructor. |
|
29
|
|
|
* @param Filesystem $filesystem |
|
30
|
|
|
* @internal param string $assetDir |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Filesystem $filesystem ) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->detector = new MimeDetector(); |
|
35
|
|
|
$this->filesystem = $filesystem ; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $file |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
|
|
public function exists(string $file): bool |
|
43
|
|
|
{ |
|
44
|
|
|
// Will never throw as we check if the file exists. |
|
45
|
|
|
return $this->filesystem->exists($file) && $this->filesystem->get($file) instanceof File; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param RequestInterface $request |
|
50
|
|
|
* @param ResponseInterface $response |
|
51
|
|
|
* @param CacheControl $cacheControl |
|
52
|
|
|
* @param bool $inline |
|
53
|
|
|
* @return ResponseInterface|bool |
|
54
|
|
|
* @throws FileNotFoundException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function resolve( |
|
57
|
|
|
RequestInterface $request, |
|
58
|
|
|
ResponseInterface $response, |
|
59
|
|
|
CacheControl $cacheControl, |
|
60
|
|
|
bool $inline = true |
|
61
|
|
|
) |
|
62
|
|
|
{ |
|
63
|
|
|
$target = $request->getRequestTarget(); |
|
64
|
|
|
|
|
65
|
|
|
$file = $this->filesystem->get($target); |
|
66
|
|
|
|
|
67
|
|
|
/** @var ResponseInterface $response */ |
|
68
|
|
|
$response = $response->withHeader('Cache-Control', (string) $cacheControl); |
|
69
|
|
|
$response = $response->withHeader('Content-Type', $this->detector->detectFromExtension( |
|
70
|
|
|
$file->getBasename()) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
if ($inline) { |
|
74
|
|
|
$response = $response->withHeader('Content-Disposition', 'inline; filename="' . $file->getBasename() . '";'); |
|
75
|
|
|
} else { |
|
76
|
|
|
$response = $response->withHeader('Content-Disposition', 'attachment; filename="' . $file->getBasename() . '";'); |
|
77
|
|
|
} |
|
78
|
|
|
$response = $response->withHeader('Content-Length', (string)$file->getSize()); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Will always return a stream |
|
82
|
|
|
*/ |
|
83
|
|
|
$stream = $file->getStream(); |
|
|
|
|
|
|
84
|
|
|
return $response->withBody(new Stream($stream))->withStatus(200); |
|
85
|
|
|
} |
|
86
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: