chore(bootstrap.php): separately minify php when files are requested

This commit is contained in:
Cory Dransfeldt 2025-06-15 11:32:01 -07:00
parent 2b22d10b46
commit 320f5e53db
No known key found for this signature in database
6 changed files with 259 additions and 14 deletions

View file

@ -1,6 +1,37 @@
<?php
define('PROJECT_ROOT', realpath(__DIR__.'/..'));
define('PROJECT_ROOT', realpath(__DIR__ . '/..'));
require_once PROJECT_ROOT.'/vendor/autoload.php';
require_once PROJECT_ROOT.'/app/Utils/init.php';
require_once PROJECT_ROOT . '/vendor/autoload.php';
require_once PROJECT_ROOT . '/app/Utils/init.php';
use voku\helper\HtmlMin;
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? '';
$isApi = str_starts_with($requestUri, '/api/');
$isHtml = stripos($acceptHeader, 'text/html') !== false || empty($acceptHeader);
if (!$isApi && $isHtml) {
ob_start(function ($buffer) {
if (stripos($buffer, '<body') !== false) {
try {
$minifier = new HtmlMin();
$buffer = $minifier->minify($buffer);
if (stripos($buffer, '</body>') === false) {
$buffer .= '</body>';
}
if (stripos($buffer, '</html>') === false) {
$buffer .= '</html>';
}
} catch (Throwable $e) {
error_log("HTML minification failed: " . $e->getMessage());
}
}
return $buffer;
});
}