forked from erichard/glide-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymfonyResponseFactory.php
47 lines (38 loc) · 1.38 KB
/
SymfonyResponseFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Erichard\Bundle\GlideBundle;
use League\Flysystem\FilesystemInterface;
use League\Glide\Responses\ResponseFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class SymfonyResponseFactory implements ResponseFactoryInterface
{
/** @var Request|null */
protected $request;
public function __construct(Request $request = null)
{
$this->request = $request;
}
public function create(FilesystemInterface $cache, $path)
{
$stream = $cache->readStream($path);
$response = new StreamedResponse();
$response->headers->set('Content-Type', $cache->getMimetype($path));
$response->headers->set('Content-Length', $cache->getSize($path));
$response->setPublic();
$response->setMaxAge(31536000);
$response->setSharedMaxAge(31536000);
$response->setExpires(date_create()->modify('+1 years'));
if ($this->request) {
$response->setLastModified(date_create()->setTimestamp($cache->getTimestamp($path)));
$response->isNotModified($this->request);
}
$response->setCallback(function () use ($stream) {
if (0 !== ftell($stream)) {
rewind($stream);
}
fpassthru($stream);
fclose($stream);
});
return $response;
}
}