Files
landingpage-haus-schleusingen/app/Core/View.php
greggy 57b97b5069
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 28s
Lint / PHP Syntax Check (push) Successful in 36s
Lint / CSS Lint (stylelint) (push) Successful in 1m18s
Lint / HTML Lint (htmlhint) (push) Successful in 1m11s
Lint / PHP Syntax Check (pull_request) Successful in 37s
Lint / CSS Lint (stylelint) (pull_request) Successful in 1m20s
Lint / HTML Lint (htmlhint) (pull_request) Successful in 1m13s
feat: add PHPUnit test infrastructure and Router tests
- Add composer.json with PHPUnit 11 and PSR-4 autoloading
- Add phpunit.xml configuration
- Rename app/core/ → app/Core/ and app/controllers/ → app/Controllers/ (PSR-4)
- Add 18 unit tests for App\Core\Router (31 assertions)
  - addRoute(): default action, custom action, overwrite
  - dispatch(): URL normalization, direct match, legacy redirects
  - dispatch(): 404 handling, controller/action not found exceptions
  - TestableRouter subclass to intercept side-effects
- Update .gitignore (vendor/, .phpunit.cache/)
2026-05-22 19:02:02 +00:00

47 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core;
class View
{
private string $viewsPath;
private array $data = [];
public function __construct(?string $viewsPath = null)
{
$this->viewsPath = $viewsPath ?? dirname(__DIR__) . '/views';
}
public function assign(string $key, mixed $value): void
{
$this->data[$key] = $value;
}
public function render(string $view, string $layout = 'main'): void
{
$viewFile = $this->viewsPath . '/' . $view . '.php';
$layoutFile = $this->viewsPath . '/layouts/' . $layout . '.php';
if (!file_exists($viewFile)) {
throw new \RuntimeException("View {$view} nicht gefunden: {$viewFile}");
}
if (!file_exists($layoutFile)) {
throw new \RuntimeException("Layout {$layout} nicht gefunden: {$layoutFile}");
}
// Extract data to variables for the view
extract($this->data, EXTR_SKIP);
// Capture view content
ob_start();
require $viewFile;
$content = ob_get_clean();
// Render layout with $content
require $layoutFile;
}
}