Skip to content

Commit

Permalink
Add static file render
Browse files Browse the repository at this point in the history
  • Loading branch information
tourze authored Mar 15, 2022
1 parent ced135f commit b7d84ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Command/WorkermanHttpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Kernel;
use Doctrine\DBAL\Connection;
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -80,7 +82,23 @@ protected function runHttpServer(InputInterface $input, OutputInterface $output)
$this->fileMonitorTimer();
$this->resetServiceTimer($output);
};
$worker->onMessage = function (TcpConnection $connection, ServerRequest $psrRequest) use ($output) {

$detector = new FinfoMimeTypeDetector();
$worker->onMessage = function (TcpConnection $connection, ServerRequest $psrRequest) use ($output, $detector) {
$checkFile = "{$this->kernel->getProjectDir()}/public{$psrRequest->getUri()->getPath()}";
$checkFile = str_replace('..', '/', $checkFile);
//$output->writeln("正在处理:{$checkFile}");

if (is_file($checkFile)) {
$code = file_get_contents($checkFile);
$psrResponse = new Response(200, [
'Content-Type' => $detector->detectMimeType($checkFile, $code),
'Last-Modified' => gmdate('D, d M Y H:i:s', filemtime($checkFile)) . ' GMT',
], $code);
$connection->send(response_to_string($psrResponse), true);
return;
}

$this->kernel->boot();

// 将PSR规范的请求,转换为Symfony请求进行处理,最终再转换成PSR响应进行返回
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"type": "symfony-bundle",
"require": {
"php": ">=7.2",
"ext-fileinfo": "*",
"ext-posix": "*",
"ext-pcntl": "*",
"league/mime-type-detection": "^1.9",
"nyholm/psr7": "^1.5",
"symfony/framework-bundle": "^5.4",
"symfony/psr-http-message-bridge": "^2.1",
Expand Down

0 comments on commit b7d84ea

Please sign in to comment.