|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace roxblnfk\SmartStream\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
11
|
|
|
use Psr\Http\Message\StreamInterface; |
|
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
14
|
|
|
use roxblnfk\SmartStream\Stream\BucketStream; |
|
15
|
|
|
use Yiisoft\Http\Header; |
|
16
|
|
|
|
|
17
|
|
|
final class BucketStreamMiddleware implements MiddlewareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
private ContainerInterface $container; |
|
20
|
|
|
private StreamFactoryInterface $streamFactory; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(ContainerInterface $container) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->container = $container; |
|
25
|
|
|
$this->streamFactory = $container->get(StreamFactoryInterface::class); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
29
|
|
|
{ |
|
30
|
|
|
$response = $handler->handle($request); |
|
31
|
|
|
$stream = $response->getBody(); |
|
32
|
|
|
if (!$stream instanceof BucketStream || $stream->hasConverter()) { |
|
33
|
|
|
return $response; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$bucket = $stream->getBucket(); |
|
37
|
|
|
|
|
38
|
|
|
// Bucket has been detached |
|
39
|
|
|
if ($bucket === null) { |
|
40
|
|
|
return $stream->isReadable() ? $response : $response->withBody($this->streamFactory->createStream('')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!$bucket->isFormatable() && !$stream->isReadable()) { |
|
44
|
|
|
$response = $response->withBody($this->createReadableStream($bucket->getData())); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Set MIME type |
|
48
|
|
|
$matching = $stream->getMatchedResult(); |
|
|
|
|
|
|
49
|
|
|
if ($matching !== null && $matching->getMimeType() !== null) { |
|
|
|
|
|
|
50
|
|
|
$response = $response->withHeader(Header::CONTENT_TYPE, $matching->getMimeType()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Update request |
|
54
|
|
|
if ($bucket->getCode() !== null) { |
|
55
|
|
|
$response = $response->withStatus($bucket->getCode()); |
|
56
|
|
|
} |
|
57
|
|
|
return $this->addHeaders($response, $bucket->getHeaders()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function addHeaders(ResponseInterface $response, array $headers): ResponseInterface |
|
61
|
|
|
{ |
|
62
|
|
|
foreach ($headers as $header => $value) { |
|
63
|
|
|
$response = $response->withHeader($header, $value); |
|
64
|
|
|
} |
|
65
|
|
|
return $response; |
|
66
|
|
|
} |
|
67
|
|
|
private function createReadableStream($data): StreamInterface |
|
68
|
|
|
{ |
|
69
|
|
|
if ($data instanceof \SplFileInfo) { |
|
70
|
|
|
return $this->streamFactory->createStreamFromFile($data->getRealPath()); |
|
71
|
|
|
} |
|
72
|
|
|
if (is_resource($data)) { |
|
73
|
|
|
return $this->streamFactory->createStreamFromResource($data); |
|
74
|
|
|
} |
|
75
|
|
|
return $this->streamFactory->createStream(is_string($data) ? $data : ''); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.