First Commit

This commit is contained in:
Michel Fedde 2024-06-30 14:41:33 +02:00
commit 923d6ca242
35 changed files with 4933 additions and 0 deletions

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
final class ResourceEntry
{
/**
* @param string[] $js
* @param string[] $css
*/
public function __construct(
public readonly array $js,
public readonly array $css,
) { }
}

View file

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use Exception;
use GamesShop\Paths;
final class ResourceIndex
{
private const string PATH = Paths::SOURCE_PATH . '/file-index.json';
/**
* @var ResourceEntry[]
*/
private array $resources = [];
public function __construct()
{
$fileContents = file_get_contents(self::PATH);
$index = json_decode($fileContents, true);
foreach ($index as $entryKey => $resource) {
$js = $resource['js'];
if (is_string($js)) {
$js = [$js];
}
$css = $resource['css'];
if (is_string($css)) {
$css = [$css];
}
$this->resources[$entryKey] = new ResourceEntry(
$js, $css
);
}
}
/**
* @throws Exception
*/
public function getResource(string $entry): ResourceEntry
{
if (!array_key_exists($entry, $this->resources)) {
throw new Exception("Entry '$entry' not found");
}
return $this->resources[$entry];
}
}

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace GamesShop\Templates;
use GamesShop\Paths;
use League\Plates\Engine;
final class TemplateEngine extends Engine
{
private const string TEMPLATES_PATH = Paths::SOURCE_PATH . '/templates';
public function __construct(
private ResourceIndex $resourceIndex,
)
{
parent::__construct(self::TEMPLATES_PATH, 'php');
$this->addData([
'resources' => $this->resourceIndex,
]);
}
public function renderPage(string $page, array $data = array())
{
return parent::render("pages/$page", $data);
}
}