|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Glide\Responses; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use League\Flysystem\FilesystemInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
class PsrResponseFactory implements ResponseFactoryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Base response object. |
|
13
|
|
|
* @var ResponseInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Callback to create stream. |
|
19
|
|
|
* @var Closure |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $streamCallback; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Create PsrResponseFactory instance. |
|
25
|
|
|
* @param ResponseInterface $response Base response object. |
|
26
|
|
|
* @param Closure $streamCallback Callback to create stream. |
|
27
|
|
|
*/ |
|
28
|
6 |
|
public function __construct(ResponseInterface $response, Closure $streamCallback) |
|
29
|
|
|
{ |
|
30
|
6 |
|
$this->response = $response; |
|
31
|
6 |
|
$this->streamCallback = $streamCallback; |
|
32
|
6 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create response. |
|
36
|
|
|
* @param FilesystemInterface $cache Cache file system. |
|
37
|
|
|
* @param string $path Cached file path. |
|
38
|
|
|
* @return ResponseInterface Response object. |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function create(FilesystemInterface $cache, $path) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$stream = $this->streamCallback->__invoke( |
|
43
|
3 |
|
$cache->readStream($path) |
|
44
|
2 |
|
); |
|
45
|
|
|
|
|
46
|
3 |
|
$contentType = $cache->getMimetype($path); |
|
47
|
3 |
|
$contentLength = (string) $cache->getSize($path); |
|
48
|
3 |
|
$cacheControl = 'max-age=31536000, public'; |
|
49
|
3 |
|
$expires = date_create('+1 years')->format('D, d M Y H:i:s').' GMT'; |
|
50
|
|
|
|
|
51
|
3 |
|
return $this->response->withBody($stream) |
|
52
|
3 |
|
->withHeader('Content-Type', $contentType) |
|
|
|
|
|
|
53
|
3 |
|
->withHeader('Content-Length', $contentLength) |
|
54
|
3 |
|
->withHeader('Cache-Control', $cacheControl) |
|
55
|
3 |
|
->withHeader('Expires', $expires); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.