Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TwigRenderer
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getAssetContents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAssetUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getFallbackFixturesPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\TwigTemplateLayoutRenderer;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Exceptions\InvalidTypeException;
6use Apie\HtmlBuilders\Assets\AssetManager;
7use Apie\HtmlBuilders\Interfaces\ComponentInterface;
8use Apie\HtmlBuilders\Interfaces\ComponentRendererInterface;
9use Apie\TwigTemplateLayoutRenderer\Extension\ComponentHelperExtension;
10use Symfony\UX\Icons\Twig\UXIconRuntime;
11use Twig\Environment;
12use Twig\Loader\FilesystemLoader;
13
14final class TwigRenderer implements ComponentRendererInterface
15{
16    private Environment $twigEnvironment;
17
18    private static ComponentHelperExtension $extension;
19
20    public function __construct(
21        string $path,
22        private AssetManager $assetManager,
23        private string $namespacePrefix,
24        ?UXIconRuntime $uxIconRuntime = null,
25    ) {
26        $loader = new FilesystemLoader([$path, self::getFallbackFixturesPath()]);
27        $this->twigEnvironment = new Environment($loader, []);
28        if (!isset(self::$extension)) {
29            self::$extension = new ComponentHelperExtension($uxIconRuntime);
30        }
31        $this->twigEnvironment->addExtension(self::$extension);
32    }
33
34    public function getAssetContents(string $filename): string
35    {
36        return $this->assetManager->getAsset($filename)->getContents();
37    }
38
39    public function getAssetUrl(string $filename): string
40    {
41        return $this->assetManager->getAsset($filename)->getBase64Url();
42    }
43
44    public function render(ComponentInterface $component, ApieContext $apieContext): string
45    {
46        $className = get_class($component);
47        if (!str_starts_with($className, $this->namespacePrefix)) {
48            throw new InvalidTypeException($component, 'class in ' . $this->namespacePrefix . ' namespace');
49        }
50        self::$extension->selectComponent($this, $component, $apieContext);
51        try {
52            $templatePath = str_replace('\\', '/', strtolower(substr($className, strlen($this->namespacePrefix)))) . '.html.twig';
53            return $this->twigEnvironment->render($templatePath);
54        } finally {
55            self::$extension->deselectComponent($component);
56        }
57    }
58
59    final public static function getFallbackFixturesPath(): string
60    {
61        return __DIR__ . '/../resources';
62    }
63}