1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Narrowspark\Mimetypes\MimeTypeByExtensionGuesser; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use RingCentral\Psr7\Response; |
9
|
|
|
use ScriptFUSION\Byte\ByteFormatter; |
10
|
|
|
use function RingCentral\Psr7\stream_for; |
|
|
|
|
11
|
|
|
|
12
|
|
|
final class WebrootPreloadMiddleware |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $files = []; |
18
|
|
|
|
19
|
10 |
|
public function __construct(string $webroot, LoggerInterface $logger = null) |
20
|
|
|
{ |
21
|
10 |
|
$totalSize = 0; |
22
|
10 |
|
$byteFormatter = (new ByteFormatter())->setPrecision(2)->setFormat('%v%u'); |
23
|
10 |
|
$directory = new \RecursiveDirectoryIterator($webroot); |
|
|
|
|
24
|
10 |
|
$directory = new \RecursiveIteratorIterator($directory); |
25
|
10 |
|
$directory = iterator_to_array($directory); |
26
|
10 |
|
usort($directory, function ($a, $b) { |
27
|
10 |
|
return $a->getPathname() <=> $b->getPathname(); |
28
|
10 |
|
}); |
29
|
10 |
|
foreach ($directory as $fileinfo) { |
30
|
10 |
|
if (!$fileinfo->isFile()) { |
31
|
10 |
|
continue; |
32
|
|
|
} |
33
|
|
|
|
34
|
10 |
|
$filePath = str_replace( |
35
|
|
|
[ |
36
|
10 |
|
$webroot, |
37
|
10 |
|
DIRECTORY_SEPARATOR, |
38
|
10 |
|
'//', |
39
|
|
|
], |
40
|
|
|
[ |
41
|
10 |
|
DIRECTORY_SEPARATOR, |
42
|
10 |
|
'/', |
43
|
10 |
|
'/', |
44
|
|
|
], |
45
|
10 |
|
$fileinfo->getPathname() |
46
|
|
|
); |
47
|
|
|
|
48
|
10 |
|
$this->files[$filePath] = [ |
49
|
10 |
|
'contents' => file_get_contents($fileinfo->getPathname()), |
50
|
|
|
]; |
51
|
|
|
|
52
|
10 |
|
$mime = MimeTypeByExtensionGuesser::guess($fileinfo->getExtension()); |
53
|
10 |
|
list($mime) = explode(';', $mime); |
54
|
10 |
|
if (strpos($mime, '/') !== false) { |
55
|
10 |
|
$this->files[$filePath]['mime'] = $mime; |
56
|
|
|
} |
57
|
|
|
|
58
|
10 |
|
if ($logger instanceof LoggerInterface) { |
59
|
1 |
|
$fileSize = strlen($this->files[$filePath]['contents']); |
60
|
1 |
|
$totalSize += $fileSize; |
61
|
10 |
|
$logger->debug($filePath . ': ' . $byteFormatter->format($fileSize)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
10 |
|
if ($logger instanceof LoggerInterface) { |
66
|
1 |
|
$logger->info('Preloaded ' . count($this->files) . ' file(s) with a combined size of ' . $byteFormatter->format($totalSize) . ' from "' . $webroot . '" into memory'); |
67
|
|
|
} |
68
|
10 |
|
} |
69
|
|
|
|
70
|
9 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
71
|
|
|
{ |
72
|
9 |
|
$path = $request->getUri()->getPath(); |
73
|
9 |
|
if (!isset($this->files[$path])) { |
74
|
1 |
|
return $next($request); |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
$response = (new Response(200))->withBody(stream_for($this->files[$path]['contents'])); |
78
|
8 |
|
if (!isset($this->files[$path]['mime'])) { |
79
|
|
|
return $response; |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
return $response->withHeader('Content-Type', $this->files[$path]['mime']); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|