Files
geograsim/App/php/templates/_scripts.php
T
Adminator 1e51ef7def Nachtrag: alle bisher untracked Ordner + hängende Änderungen mit-committen
- Konzept/, didaktik_geografie/, didaktik_simulation/, v2-modules/, v2-platform/
- 12 code-workspace-Files
- STATUS-*.md
- viele M/D/R-Änderungen an bereits getrackten Files
- .gitignore verstärkt: **/.humaninput/, **/secret_keys.txt

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-08 02:27:02 +02:00

129 lines
5.1 KiB
PHP

<?php
/**
* Vite Manifest Reader
* Liest dist/.vite/manifest.json und gibt die korrekten Script-Tags aus.
* Im Dev-Modus (kein manifest vorhanden) wird direkt auf die TS-Quellen verwiesen.
*/
function viteAssets(string $entry): void {
$manifestPath = APP_ROOT . '/dist/.vite/manifest.json';
// Production: Lese Manifest
if (file_exists($manifestPath)) {
$manifest = json_decode(file_get_contents($manifestPath), true);
$chunk = $manifest[$entry] ?? null;
if (!$chunk) return;
// CSS
foreach ($chunk['css'] ?? [] as $css) {
echo '<link rel="stylesheet" href="' . BASE_PATH . '/dist/' . $css . '">' . "\n";
}
// Preload imports
foreach ($chunk['imports'] ?? [] as $importKey) {
$imp = $manifest[$importKey] ?? null;
if ($imp) {
echo '<link rel="modulepreload" crossorigin href="' . BASE_PATH . '/dist/' . $imp['file'] . '">' . "\n";
}
}
// Main entry
echo '<script type="module" crossorigin src="' . BASE_PATH . '/dist/' . $chunk['file'] . '"></script>' . "\n";
}
// Dev fallback: keine Manifest-Datei → direkte TS-Referenz (Vite Dev Server)
}
/** Session-Kontext als JS-Variable injizieren */
function injectSessionContext(): void {
$ctx = [
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
];
echo '<script>window.__GGS__ = ' . json_encode($ctx, JSON_UNESCAPED_UNICODE) . ';</script>' . "\n";
}
/**
* Plattform-Live-Client in eine Sim-Render-HTML einfügen.
*
* Stellt sicher, dass `window.__GGS__` mit `sessionId`, `simId`, `baseUrl`,
* `basePath`, `apiUrl` belegt ist (Sim-eigene Werte gewinnen) und lädt
* danach `assets/js/live-client.js`. Der Live-Client kümmert sich dann um
* Heartbeat / Pause-Polling / Spectator-Mode.
*
* In jedem Sim-Wrapper kurz vor `echo $html` aufrufen:
* $html = ggs_inject_live($html, 'logistik');
*/
function ggs_inject_live(string $html, string $simId): string {
$bp = BASE_PATH;
$bu = BASE_URL;
$sid = Session::studentId() ?? '';
$ctx = [
'sessionId' => $sid,
'simId' => $simId,
'baseUrl' => $bu,
'basePath' => $bp,
'apiUrl' => $bu . '/php/api',
];
$ctxJson = json_encode($ctx, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// Sim-eigene __GGS__-Felder gewinnen — wir ergänzen nur Plattform-Felder.
$bootstrap = '<script>window.__GGS__=Object.assign(' . $ctxJson . ',window.__GGS__||{});</script>';
$tag = '<script src="' . htmlspecialchars($bp . '/assets/js/live-client.js', ENT_QUOTES) . '" defer></script>';
$injection = $bootstrap . "\n" . $tag . "\n";
// Sicheres Inject: vor dem letzten </body>. Falls keines, einfach anhängen.
$pos = strrpos($html, '</body>');
if ($pos === false) return $html . "\n" . $injection;
return substr($html, 0, $pos) . $injection . substr($html, $pos);
}
/** Seite rendern: HTML laden, Session-Kontext + Favicon injizieren, ausgeben.
*
* Härtung: str_replace('</body>', ...) wäre zerstörerisch, wenn der String
* '</body>' in einem JavaScript-Literal vorkommt (z.B. teacher.html nutzt
* w.document.write('<html>…</body></html>') — die Replace würde mitten in
* den JS-String injizieren und den Parser zerschießen, der Rest des
* <script>-Blocks landet sichtbar als Text auf der Seite).
* Lösung: nur das LETZTE Vorkommen ersetzen (echtes Doc-Ende), via strrpos.
*/
function renderPage(string $htmlFile): void {
$html = file_get_contents(APP_ROOT . '/' . $htmlFile);
// Favicon-Block (ersetzt alte logo.png Referenz)
$bp = BASE_PATH;
$favicon = <<<HTML
<link rel="icon" type="image/png" href="{$bp}/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/svg+xml" href="{$bp}/favicon.svg">
<link rel="shortcut icon" href="{$bp}/favicon.ico">
<link rel="apple-touch-icon" sizes="180x180" href="{$bp}/apple-touch-icon.png">
<link rel="manifest" href="{$bp}/site.webmanifest">
HTML;
// Alten Favicon-Link entfernen (alle Vorkommen — sind harmlos zu ersetzen, weil im <head>)
if (preg_match('/<link[^>]*rel=["\']icon["\'][^>]*>/', $html)) {
$html = preg_replace('/<link[^>]*rel=["\']icon["\'][^>]*>\s*/', '', $html);
}
// Favicon vor LETZTEM </head> einfügen (nicht in JS-Strings injizieren)
$headPos = strrpos($html, '</head>');
if ($headPos !== false) {
$html = substr($html, 0, $headPos) . $favicon . "\n" . substr($html, $headPos);
}
// Session-Kontext-Script
$ctx = '<script>window.__GGS__ = ' . json_encode([
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
], JSON_UNESCAPED_UNICODE) . ';</script>';
// Vor LETZTEM </body> einfügen (nicht in JS-Strings injizieren)
$bodyPos = strrpos($html, '</body>');
if ($bodyPos !== false) {
$html = substr($html, 0, $bodyPos) . $ctx . "\n" . substr($html, $bodyPos);
} else {
$html .= "\n" . $ctx;
}
echo $html;
}