Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StaticContentController
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
3.00
0.00% covered (danger)
0.00%
0 / 1
 __invoke
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
3.00
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        return new Response(
31            200,
32            ['Content-Type' => $mimeType],
33            $stream
34        );
35    }
36}