Files
WebWizard/src/Services/Renderer.php

189 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
require_once __DIR__ . '/../Utils/Helpers.php';
class Renderer
{
private FileStorage $storage;
private string $exportsPath;
private string $templatesPath;
private array $palettes = [
'blue-gray' => [
'primary' => '#2563eb',
'secondary' => '#64748b',
'bg' => '#f8fafc',
'text' => '#1e293b'
],
'nature-green' => [
'primary' => '#059669',
'secondary' => '#fbbf24',
'bg' => '#fdfdfd',
'text' => '#064e3b'
],
'elegant-gold' => [
'primary' => '#d4af37',
'secondary' => '#111827',
'bg' => '#ffffff',
'text' => '#111827'
]
];
private array $styles = [
'minimal' => [
'radius' => '0px',
'shadow' => 'none',
'padding' => '4rem 1rem'
],
'modern' => [
'radius' => '0.5rem',
'shadow' => '0 4px 6px -1px rgb(0 0 0 / 0.1)',
'padding' => '6rem 1.5rem'
],
'premium' => [
'radius' => '1.5rem',
'shadow' => '0 10px 15px -3px rgb(0 0 0 / 0.1)',
'padding' => '8rem 2rem'
]
];
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';
$cssDir = $assetsDir . DIRECTORY_SEPARATOR . 'css';
$jsDir = $assetsDir . DIRECTORY_SEPARATOR . 'js';
// 1. Create directory structure
if (!is_dir($cssDir)) mkdir($cssDir, 0777, true);
if (!is_dir($jsDir)) mkdir($jsDir, 0777, true);
// 2. Copy static assets
copy($this->templatesPath . '/css/site.css', $cssDir . '/site.css');
copy($this->templatesPath . '/js/site.js', $jsDir . '/site.js');
// 3. Handle Contact Form Logic
$formConfig = $projectData['wizard_data']['modules']['contact_form'] ?? [];
if (!empty($formConfig['enabled'])) {
// Copy handler
copy($this->templatesPath . '/emailer.php', $projectExportDir . '/ajax.php');
// Create config for handler (PHP file is not web-accessible)
$siteConfig = [
'site_name' => $projectData['wizard_data']['identity']['business_name'],
'form_mode' => $formConfig['mode'] ?? 'local',
'smtp' => $formConfig['smtp'] ?? null,
'local_password_hash' => $formConfig['local_viewer']['password_hash'] ?? null
];
$configContent = "<?php\nreturn " . var_export($siteConfig, true) . ";\n";
file_put_contents($projectExportDir . '/config.php', $configContent);
if (($siteConfig['form_mode'] ?? 'local') === 'local') {
// Copy viewer
copy($this->templatesPath . '/form-viewer.php', $projectExportDir . '/form-viewer.php');
// Create messages dir
$msgDir = $projectExportDir . '/messages';
if (!is_dir($msgDir)) mkdir($msgDir, 0777, true);
file_put_contents($msgDir . '/.htaccess', "Deny from all");
}
}
// 4. Prepare relative paths for assets
if (!empty($projectData['wizard_data']['assets']['logo'])) {
$projectData['wizard_data']['assets']['logo'] = $this->makePathRelative($projectData['wizard_data']['assets']['logo'], $projectId);
}
if (!empty($projectData['wizard_data']['assets']['gallery'])) {
foreach ($projectData['wizard_data']['assets']['gallery'] as $key => $path) {
$projectData['wizard_data']['assets']['gallery'][$key] = $this->makePathRelative($path, $projectId);
}
}
// 4. Render Content Sections
$sectionsHtml = '';
$selectedSections = $projectData['wizard_data']['modules']['sections'] ?? [];
foreach ($selectedSections as $sectionId) {
try {
$sectionsHtml .= $this->renderTemplate("sections/{$sectionId}", [
'project' => $projectData
]);
} catch (Exception $e) {
error_log("Renderer: Failed to render section $sectionId: " . $e->getMessage());
}
}
// 4. Prepare Visual Variables
$visuals = $projectData['wizard_data']['visuals'] ?? [];
$palette = $this->palettes[$visuals['palette'] ?? 'blue-gray'] ?? $this->palettes['blue-gray'];
$style = $this->styles[$visuals['style'] ?? 'modern'] ?? $this->styles['modern'];
// 5. Render HTML base template
$html = $this->renderTemplate('base', [
'project' => $projectData,
'content' => $sectionsHtml,
'css_vars' => [
'primary' => $palette['primary'],
'secondary' => $palette['secondary'],
'bg' => $palette['bg'],
'text' => $palette['text'],
'radius' => $style['radius'],
'shadow' => $style['shadow'],
'padding' => $style['padding']
]
]);
// 6. Save to exports
return file_put_contents($projectExportDir . DIRECTORY_SEPARATOR . 'index.html', $html) !== false;
}
/**
* Makes an absolute or project-root relative path relative to the project's export directory.
*/
private function makePathRelative(string $path, string $projectId): string
{
$prefix = "exports/{$projectId}/";
if (strpos($path, $prefix) === 0) {
return substr($path, strlen($prefix));
}
return $path;
}
/**
* 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();
}
}