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