Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StaticContentController
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
4
0.00% covered (danger)
0.00%
0 / 1
 __invoke
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
4
1<?php
2namespace Apie\TypescriptClientBuilder\Controllers;
3
4use Nyholm\Psr7\Response;
5use Nyholm\Psr7\Stream;
6use Psr\Http\Message\ResponseInterface;
7use Psr\Http\Message\ServerRequestInterface;
8
9class 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        
27        if ($mimeType === false) {
28            $mimeType = 'application/octet-stream';
29        }
30        // RFC 9239: text/javascript is the official JavaScript media type.
31        if ($mimeType === 'application/javascript') {
32            $mimeType = 'text/javascript';
33        }
34        return new Response(
35            200,
36            ['Content-Type' => $mimeType],
37            $stream
38        );
39    }
40}