implemented step 15 by Gemini

- Rendering HTML
This commit is contained in:
2026-06-15 04:30:46 +02:00
parent 4f62bb7aa7
commit 7efd6b24a5
5 changed files with 251 additions and 73 deletions

69
src/Services/Renderer.php Normal file
View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
class Renderer
{
private FileStorage $storage;
private string $exportsPath;
private string $templatesPath;
public function __construct()
{
$this->storage = new FileStorage();
$this->exportsPath = realpath(__DIR__ . '/../../exports');
$this->templatesPath = realpath(__DIR__ . '/../Templates');
}
/**
* Renders a complete static website for a project.
*/
public function render(string $projectId): bool
{
$projectData = $this->storage->get("projects/{$projectId}.json");
if (!$projectData || empty($projectData['content']['generated'])) {
return false;
}
$projectExportDir = $this->exportsPath . DIRECTORY_SEPARATOR . $projectId;
$assetsDir = $projectExportDir . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'css';
// 1. Create directory structure
if (!is_dir($assetsDir)) {
mkdir($assetsDir, 0777, true);
}
// 2. Copy static assets
$siteCssSource = $this->templatesPath . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'site.css';
copy($siteCssSource, $assetsDir . DIRECTORY_SEPARATOR . 'site.css');
// 3. Render HTML
$html = $this->renderTemplate('base', [
'project' => $projectData,
'content' => '<!-- Final content sections will go here -->'
]);
// 4. Save to exports
return file_put_contents($projectExportDir . DIRECTORY_SEPARATOR . 'index.html', $html) !== false;
}
/**
* Internal helper to render a PHP template with variables.
*/
private function renderTemplate(string $templateName, array $vars = []): string
{
$templateFile = $this->templatesPath . DIRECTORY_SEPARATOR . $templateName . '.php';
if (!file_exists($templateFile)) {
throw new Exception("Template not found: $templateName");
}
extract($vars);
ob_start();
include $templateFile;
return ob_get_clean();
}
}