Refactor service and controller code, update file paths

- Refactored the `Page` class from the `App\Repository` namespace to the `App\Service` namespace.
- Updated the file path references in the `getPage()` method of the `Page` class to use `$this->kernel` instead of `$kernel`.
- Renamed the file from `src/Repository/Page.php` to `src/Service/Page.php`.
- Updated the import statement in the `DefaultController.php` file to reflect this change.

This commit improves code organization and ensures that file paths are correctly referenced.
This commit is contained in:
Jeroen De Meerleer 2023-10-13 09:22:40 +02:00
parent 529ced6a26
commit 563c97925f
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 124 additions and 113 deletions

View File

@ -22,6 +22,9 @@ services:
App\EventSubscriber\ExceptionListener: App\EventSubscriber\ExceptionListener:
tags: tags:
- { name: kernel.event_listener, event: kernel.exception } - { name: kernel.event_listener, event: kernel.exception }
App\Service\Page:
arguments: [ '@kernel']
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones

View File

@ -2,27 +2,25 @@
namespace App\Controller; namespace App\Controller;
use App\Repository\Page; use App\Service\Page;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController class DefaultController extends AbstractController
{ {
#[Route('/{slug}', name: 'default', requirements: ['slug' => '[a-zA-Z0-9\/]+'])] #[Route('/{slug}', name: 'default', requirements: ['slug' => '[a-zA-Z0-9\/]+'])]
public function DefaultAction(Request $request, Page $page, KernelInterface $kernel, string $slug = 'index') public function DefaultAction(Page $page, string $slug = 'index')
{ {
$return = $page->getPage($kernel, $slug); $return = $page->getPage($slug);
$response = new Response('', (int)$return['status']); $response = new Response('', (int)$return['status']);
return $this->render('/page.html.twig', $return, $response); return $this->render('/page.html.twig', $return, $response);
} }
#[Route('/error/{status}', name: 'error', requirements: ['status' => '[0-9]{3}'], priority: 2)] #[Route('/error/{status}', name: 'error', requirements: ['status' => '[0-9]{3}'], priority: 2)]
public function ErrorAction(Request $request, Page $page, KernelInterface $kernel, string $status = '404') public function ErrorAction(Page $page, string $status = '404')
{ {
$return = $page->getPage($kernel, 'error/' . $status); $return = $page->getPage('error/' . $status);
$response = new Response('', (int)$status); $response = new Response('', (int)$status);
return $this->render('/page.html.twig', $return, $response); return $this->render('/page.html.twig', $return, $response);
} }

View File

@ -1,106 +0,0 @@
<?php
namespace App\Repository;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
class Page
{
public function getPage(KernelInterface $kernel, string $page)
{
$parsedown = new \Parsedown();
$return['js'] = [];
$return['css'] = [];
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->getProjectDir() . '/public/assets/js/_header.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/_header.js'), true)),
'file' => '/assets/js/_header.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/_header.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/_header.css'), true)),
'file' => '/assets/css/_header.css'
];
}
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->getProjectDir() . '/public/assets/js/_nav.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/_nav.js'), true)),
'file' => '/assets/js/_nav.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/_nav.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/_nav.css'), true)),
'file' => '/assets/css/_nav.css'
];
}
if(file_exists($kernel->getCacheDir() . '/pages/_footer.md')) {
$return['footer'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/_footer.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/_footer.html')) {
$return['footer'] = file_get_contents($kernel->getCacheDir() . '/pages/_footer.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getProjectDir() . '/public/assets/js/_footer.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/_footer.js'), true)),
'file' => '/assets/js/_footer.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/_footer.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/_footer.css'), true)),
'file' => '/assets/css/_footer.css'
];
}
if(file_exists($kernel->getCacheDir() . '/pages/' . $page . '.md')) {
$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');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getProjectDir() . '/public/assets/js/' . $page . '.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/' . $page . '.js'), true)),
'file' => '/assets/js/' . $page . '.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/' . $page . '.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/' . $page . '.css'), true)),
'file' => '/assets/css/' . $page . '.css'
];
}
$titles = json_decode(file_get_contents($kernel->getCacheDir() . '/pages/titles.json'), true);
$return['title'] = $titles[$page] ?? '';
$return['status'] = '200';
return $return;
}
}

116
src/Service/Page.php Normal file
View File

@ -0,0 +1,116 @@
<?php
namespace App\Service;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
class Page
{
private KernelInterface $kernel;
/**
* @param KernelInterface $kernel
*/
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
public function getPage(string $page)
{
$parsedown = new \Parsedown();
$return['js'] = [];
$return['css'] = [];
if(file_exists($this->kernel->getCacheDir() . '/pages/_header.md')) {
$return['header'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/_header.md'));
} elseif(file_exists($this->kernel->getCacheDir() . '/pages/_header.html')) {
$return['header'] = file_get_contents($this->kernel->getCacheDir() . '/pages/_header.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/_header.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/_header.js'), true)),
'file' => '/assets/js/_header.js'
];
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/_header.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/_header.css'), true)),
'file' => '/assets/css/_header.css'
];
}
if(file_exists($this->kernel->getCacheDir() . '/pages/_nav.md')) {
$return['nav'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/_nav.md'));
} elseif(file_exists($this->kernel->getCacheDir() . '/pages/_nav.html')) {
$return['nav'] = file_get_contents($this->kernel->getCacheDir() . '/pages/_nav.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/_nav.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/_nav.js'), true)),
'file' => '/assets/js/_nav.js'
];
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/_nav.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/_nav.css'), true)),
'file' => '/assets/css/_nav.css'
];
}
if(file_exists($this->kernel->getCacheDir() . '/pages/_footer.md')) {
$return['footer'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/_footer.md'));
} elseif(file_exists($this->kernel->getCacheDir() . '/pages/_footer.html')) {
$return['footer'] = file_get_contents($this->kernel->getCacheDir() . '/pages/_footer.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/_footer.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/_footer.js'), true)),
'file' => '/assets/js/_footer.js'
];
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/_footer.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/_footer.css'), true)),
'file' => '/assets/css/_footer.css'
];
}
if(file_exists($this->kernel->getCacheDir() . '/pages/' . $page . '.md')) {
$return['content'] = $parsedown->text(file_get_contents($this->kernel->getCacheDir() . '/pages/' . $page . '.md'));
} elseif(file_exists($this->kernel->getCacheDir() . '/pages/' . $page . '.html')) {
$return['content'] = file_get_contents($this->kernel->getCacheDir() . '/pages/' . $page . '.html');
} else {
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/js/' . $page . '.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/js/' . $page . '.js'), true)),
'file' => '/assets/js/' . $page . '.js'
];
}
if(file_exists($this->kernel->getProjectDir() . '/public/assets/css/' . $page . '.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($this->kernel->getProjectDir() . '/public/assets/css/' . $page . '.css'), true)),
'file' => '/assets/css/' . $page . '.css'
];
}
$titles = json_decode(file_get_contents($this->kernel->getCacheDir() . '/pages/titles.json'), true);
$return['title'] = $titles[$page] ?? '';
$return['status'] = '200';
return $return;
}
}