First Commit
This commit is contained in:
commit
923d6ca242
35 changed files with 4933 additions and 0 deletions
16
src/php/Templates/ResourceEntry.php
Normal file
16
src/php/Templates/ResourceEntry.php
Normal 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,
|
||||
) { }
|
||||
}
|
||||
51
src/php/Templates/ResourceIndex.php
Normal file
51
src/php/Templates/ResourceIndex.php
Normal 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];
|
||||
}
|
||||
}
|
||||
27
src/php/Templates/TemplateEngine.php
Normal file
27
src/php/Templates/TemplateEngine.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue