Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StaticContentController | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
4 | |
0.00% |
0 / 1 |
| __invoke | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptClientBuilder\Controllers; |
| 3 | |
| 4 | use Nyholm\Psr7\Response; |
| 5 | use Nyholm\Psr7\Stream; |
| 6 | use Psr\Http\Message\ResponseInterface; |
| 7 | use Psr\Http\Message\ServerRequestInterface; |
| 8 | |
| 9 | class StaticContentController |
| 10 | { |
| 11 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
| 12 | { |
| 13 | $filename = $request->getAttribute('filename'); |
| 14 | $localFilepath = $request->getAttribute('localFilepath'); |
| 15 | $filePath = $localFilepath . '/' . $filename; |
| 16 | $handle = @fopen($filePath, 'rb'); |
| 17 | if ($handle === false) { |
| 18 | return new Response( |
| 19 | 404, |
| 20 | ['Content-Type' => 'text/plain'], |
| 21 | "File not found: $filename" |
| 22 | ); |
| 23 | } |
| 24 | $stream = Stream::create($handle); |
| 25 | $mimeType = mime_content_type($filePath); |
| 26 | // issue in PHP 8.3 |
| 27 | if ($mimeType === 'text/x-java') { |
| 28 | $mimeType = 'application/javascript'; |
| 29 | } elseif ($mimeType === false) { |
| 30 | $mimeType = 'application/octet-stream'; |
| 31 | } |
| 32 | return new Response( |
| 33 | 200, |
| 34 | ['Content-Type' => $mimeType], |
| 35 | $stream |
| 36 | ); |
| 37 | } |
| 38 | } |