Update PHP version to 8.4 and add property hooks

This commit is contained in:
Ruben 2025-11-01 23:33:09 +01:00
parent 32449d2edd
commit 673c02d237
14 changed files with 939 additions and 606 deletions

View file

@ -1,9 +1,7 @@
<?php
// Find all content files in a directory (supporting language variants)
function findAllContentFiles(string $dir, string $lang, string $defaultLang): array {
global $availableLangs;
function findAllContentFiles(string $dir, string $lang, string $defaultLang, array $availableLangs): array {
if (!is_dir($dir)) return [];
$files = scandir($dir) ?: [];
@ -67,15 +65,15 @@ function findAllContentFiles(string $dir, string $lang, string $defaultLang): ar
return array_column($contentFiles, 'path');
}
function resolveTranslatedPath(string $requestPath, string $contentDir, string $lang, string $defaultLang): string {
function resolveTranslatedPath(Context $ctx, string $requestPath): string {
// If default language, no translation needed
if ($lang === $defaultLang) {
if ($ctx->currentLang === $ctx->defaultLang) {
return $requestPath;
}
$parts = explode('/', trim($requestPath, '/'));
$resolvedParts = [];
$currentPath = $contentDir;
$currentPath = $ctx->contentDir;
foreach ($parts as $segment) {
if (empty($segment)) continue;
@ -86,7 +84,7 @@ function resolveTranslatedPath(string $requestPath, string $contentDir, string $
$subdirs = getSubdirectories($currentPath);
foreach ($subdirs as $dir) {
$metadata = loadMetadata("$currentPath/$dir", $lang, $defaultLang);
$metadata = loadMetadata("$currentPath/$dir", $ctx->currentLang, $ctx->defaultLang);
if ($metadata && isset($metadata['slug']) && $metadata['slug'] === $segment) {
$resolvedParts[] = $dir;
$currentPath .= "/$dir";
@ -106,17 +104,17 @@ function resolveTranslatedPath(string $requestPath, string $contentDir, string $
return implode('/', $resolvedParts);
}
function parseRequestPath(string $requestPath, string $contentDir, bool $hasTrailingSlash, string $lang, string $defaultLang): array {
function parseRequestPath(Context $ctx): array {
// Resolve translated slugs to actual directory names
$resolvedPath = resolveTranslatedPath($requestPath, $contentDir, $lang, $defaultLang);
$contentPath = rtrim($contentDir, '/') . '/' . ltrim($resolvedPath, '/');
$resolvedPath = resolveTranslatedPath($ctx, $ctx->requestPath);
$contentPath = rtrim($ctx->contentDir, '/') . '/' . ltrim($resolvedPath, '/');
if (is_file($contentPath)) {
return ['type' => 'file', 'path' => realpath($contentPath)];
}
if (is_dir($contentPath)) {
// Check if directory has subdirectories
// Check if directory has subdirectories (PHP 8.4: cleaner with array_any later)
$hasSubdirs = !empty(getSubdirectories($contentPath));
// If directory has subdirectories, it's an article-type folder (list view)
@ -126,10 +124,10 @@ function parseRequestPath(string $requestPath, string $contentDir, bool $hasTrai
// No subdirectories - it's a page-type folder
// Find all content files in this directory
$contentFiles = findAllContentFiles($contentPath, $lang, $defaultLang);
$contentFiles = findAllContentFiles($contentPath, $ctx->currentLang, $ctx->defaultLang, $ctx->availableLangs);
if (!empty($contentFiles)) {
return ['type' => 'page', 'path' => realpath($contentPath), 'files' => $contentFiles, 'needsSlash' => !$hasTrailingSlash];
return ['type' => 'page', 'path' => realpath($contentPath), 'files' => $contentFiles, 'needsSlash' => !$ctx->hasTrailingSlash];
}
// No content files found
@ -165,13 +163,13 @@ function loadTranslations(string $lang): array {
return [];
}
function buildNavigation(string $contentDir, string $currentLang, string $defaultLang): array {
function buildNavigation(Context $ctx): array {
$navItems = [];
$items = getSubdirectories($contentDir);
$items = getSubdirectories($ctx->contentDir);
foreach ($items as $item) {
$itemPath = "$contentDir/$item";
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
$itemPath = "{$ctx->contentDir}/$item";
$metadata = loadMetadata($itemPath, $ctx->currentLang, $ctx->defaultLang);
// Check if this item should be in menu
if (!$metadata || empty($metadata['menu'])) {
@ -179,8 +177,8 @@ function buildNavigation(string $contentDir, string $currentLang, string $defaul
}
// Check if content exists for current language
if ($currentLang !== $defaultLang) {
$contentFiles = findAllContentFiles($itemPath, $currentLang, $defaultLang);
if ($ctx->currentLang !== $ctx->defaultLang) {
$contentFiles = findAllContentFiles($itemPath, $ctx->currentLang, $ctx->defaultLang, $ctx->availableLangs);
// If no content files, check if metadata has title for this language
$hasContent = !empty($contentFiles) || ($metadata && isset($metadata['title']));
@ -189,17 +187,16 @@ function buildNavigation(string $contentDir, string $currentLang, string $defaul
}
// Extract title and build URL
$title = $metadata['title'] ?? extractTitle($itemPath, $currentLang, $defaultLang) ?? ucfirst($item);
$langPrefix = getLangPrefix($currentLang, $defaultLang);
$title = $metadata['title'] ?? extractTitle($itemPath, $ctx->currentLang, $ctx->defaultLang) ?? ucfirst($item);
// Use translated slug if available
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))
$urlSlug = ($ctx->currentLang !== $ctx->defaultLang && $metadata && isset($metadata['slug']))
? $metadata['slug']
: $item;
$navItems[] = [
'title' => $title,
'url' => $langPrefix . '/' . urlencode($urlSlug) . '/',
'url' => $ctx->langPrefix . '/' . urlencode($urlSlug) . '/',
'order' => (int)($metadata['menu_order'] ?? 999)
];
}