Implement snip loader for twig, allowing extends and includes
This commit is contained in:
parent
ca7a093e55
commit
31cfeca93a
56
src/Service/SnipParser/Twig/SnipLoader.php
Normal file
56
src/Service/SnipParser/Twig/SnipLoader.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service\SnipParser\Twig;
|
||||||
|
|
||||||
|
use App\Entity\Snip;
|
||||||
|
use App\Repository\SnipRepository;
|
||||||
|
use App\Service\SnipContent\SnipContentService;
|
||||||
|
use Twig\Error\LoaderError;
|
||||||
|
use Twig\Loader\LoaderInterface;
|
||||||
|
use Twig\Source;
|
||||||
|
|
||||||
|
class SnipLoader implements LoaderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly SnipRepository $repository,
|
||||||
|
private readonly SnipContentService $contentService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function getSourceContext(string $name): Source
|
||||||
|
{
|
||||||
|
return new Source($this->contentService->getActiveText($this->getFromKey($name)), $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCacheKey(string $name): string
|
||||||
|
{
|
||||||
|
return $this->getFromKey($name)->getActiveVersion()->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isFresh(string $name, int $time): bool
|
||||||
|
{
|
||||||
|
$this->getFromKey($name);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists(string $name)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->getFromKey($name);
|
||||||
|
} catch (LoaderError) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getFromKey(string $key): Snip
|
||||||
|
{
|
||||||
|
$snip = $this->repository->find($key);
|
||||||
|
if (!$snip) {
|
||||||
|
throw new LoaderError(\sprintf('Template "%s" is not defined.', $key));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $snip;
|
||||||
|
}
|
||||||
|
}
|
@ -5,9 +5,14 @@ namespace App\Service\SnipParser\Twig;
|
|||||||
use App\Service\SnipParser\AbstractParser;
|
use App\Service\SnipParser\AbstractParser;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
use Twig\Loader\ArrayLoader;
|
use Twig\Loader\ArrayLoader;
|
||||||
|
use Twig\Loader\ChainLoader;
|
||||||
|
|
||||||
class TwigParser extends AbstractParser
|
class TwigParser extends AbstractParser
|
||||||
{
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly SnipLoader $snipLoader,
|
||||||
|
) {}
|
||||||
|
|
||||||
public static function getName(): string
|
public static function getName(): string
|
||||||
{
|
{
|
||||||
return 'twig';
|
return 'twig';
|
||||||
@ -16,12 +21,15 @@ class TwigParser extends AbstractParser
|
|||||||
public function parseView(string $content): string
|
public function parseView(string $content): string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$arrayLoader = new ArrayLoader([
|
$loader = new ChainLoader([
|
||||||
'index' => $content,
|
new ArrayLoader([
|
||||||
|
'index' => $content,
|
||||||
|
]),
|
||||||
|
$this->snipLoader,
|
||||||
]);
|
]);
|
||||||
$twig = new Environment($arrayLoader);
|
$twig = new Environment($loader);
|
||||||
return $twig->render('index');
|
return $twig->render('index');
|
||||||
}catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
return sprintf('<pre><code class="hljs">%s</code></pre>', htmlspecialchars($exception->getMessage()));
|
return sprintf('<pre><code class="hljs">%s</code></pre>', htmlspecialchars($exception->getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user