website/src/Repository/Page.php

44 lines
1.9 KiB
PHP
Raw Normal View History

2021-08-05 13:51:44 +02:00
<?php
namespace App\Repository;
2021-08-05 13:51:44 +02:00
2021-08-06 12:30:00 +02:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
2021-08-05 13:51:44 +02:00
class Page
{
public function getPage(KernelInterface $kernel, string $page)
2021-08-05 13:51:44 +02:00
{
2023-03-22 11:49:46 +01:00
$parsedown = new \Parsedown();
if(file_exists($kernel->getCacheDir() . '/pages/_header.md')) {
$return['header'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/_header.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/_header.html')) {
$return['header'] = file_get_contents($kernel->getCacheDir() . '/pages/_header.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getCacheDir() . '/pages/_nav.md')) {
$return['nav'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/_nav.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/_nav.html')) {
$return['nav'] = file_get_contents($kernel->getCacheDir() . '/pages/_nav.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getCacheDir() . '/pages/' . $page . '.md')) {
2023-03-22 11:49:46 +01:00
$return['content'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/' . $page . '.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/' . $page . '.html')) {
$return['content'] = file_get_contents($kernel->getCacheDir() . '/pages/' . $page . '.html');
2021-08-05 13:51:44 +02:00
} else {
2023-03-22 11:49:46 +01:00
throw new NotFoundHttpException('Some required files were not found');
2021-08-05 13:51:44 +02:00
}
2023-03-22 11:49:46 +01:00
$titles = json_decode(file_get_contents($kernel->getCacheDir() . '/pages/titles.json'), true);
$return['title'] = $titles[$page] ?? '';
$return['status'] = '200';
2021-08-05 13:51:44 +02:00
return $return;
}
}