bearsunday /
BEAR.Sunday
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\Sunday\Provide\Transfer; |
||
| 6 | |||
| 7 | use BEAR\Resource\ResourceObject; |
||
| 8 | use BEAR\Sunday\Extension\Transfer\TransferInterface; |
||
| 9 | use Override; |
||
| 10 | |||
| 11 | class HttpResponder implements TransferInterface |
||
| 12 | { |
||
| 13 | public function __construct( |
||
| 14 | private HeaderInterface $header, |
||
| 15 | 9 | private ConditionalResponseInterface $condResponse, |
|
| 16 | ) { |
||
| 17 | 9 | } |
|
| 18 | 1 | ||
| 19 | /** |
||
| 20 | 1 | * {@inheritDoc} |
|
| 21 | */ |
||
| 22 | #[Override] |
||
| 23 | public function __invoke(ResourceObject $ro, array $server): void |
||
| 24 | 8 | { |
|
| 25 | 8 | /** @var array{HTTP_IF_NONE_MATCH?: string} $server */ |
|
| 26 | $isModifed = $this->condResponse->isModified($ro, $server); |
||
| 27 | $output = $isModifed ? $this->getOutput($ro, $server) : $this->condResponse->getOutput($ro->headers); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | |||
| 29 | 8 | foreach ($output->headers as $label => $value) { |
|
| 30 | 8 | // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName |
|
| 31 | header("{$label}: {$value}", false); |
||
| 32 | } |
||
| 33 | |||
| 34 | 8 | // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName |
|
| 35 | http_response_code($output->code); |
||
| 36 | |||
| 37 | 8 | echo $output->view; |
|
| 38 | 8 | } |
|
| 39 | |||
| 40 | 9 | /** @param array<string, string> $server */ |
|
| 41 | private function getOutput(ResourceObject $ro, array $server): Output |
||
| 42 | 9 | { |
|
| 43 | 9 | $ro->toString(); // render and set headers |
|
| 44 | 9 | ||
| 45 | return new Output($ro->code, ($this->header)($ro, $server), $ro->view ?? $ro->toString()); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |