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 Twig\Environment;
11use Twig\Loader\FilesystemLoader;
12
13final class TwigRenderer implements ComponentRendererInterface
14{
15    private Environment $twigEnvironment;
16
17    private static ComponentHelperExtension $extension;
18
19    public function __construct(
20        string $path,
21        private AssetManager $assetManager,
22        private string $namespacePrefix
23    ) {
24        $loader = new FilesystemLoader([$path, self::getFallbackFixturesPath()]);
25        $this->twigEnvironment = new Environment($loader, []);
26        if (!isset(self::$extension)) {
27            self::$extension = new ComponentHelperExtension();
28        }
29        $this->twigEnvironment->addExtension(self::$extension);
30    }
31
32    public function getAssetContents(string $filename): string
33    {
34        return $this->assetManager->getAsset($filename)->getContents();
35    }
36
37    public function getAssetUrl(string $filename): string
38    {
39        return $this->assetManager->getAsset($filename)->getBase64Url();
40    }
41
42    public function render(ComponentInterface $component, ApieContext $apieContext): string
43    {
44        $className = get_class($component);
45        if (!str_starts_with($className, $this->namespacePrefix)) {
46            throw new InvalidTypeException($component, 'class in ' . $this->namespacePrefix . ' namespace');
47        }
48        self::$extension->selectComponent($this, $component, $apieContext);
49        try {
50            $templatePath = str_replace('\\', '/', strtolower(substr($className, strlen($this->namespacePrefix)))) . '.html.twig';
51            return $this->twigEnvironment->render($templatePath);
52        } finally {
53            self::$extension->deselectComponent($component);
54        }
55    }
56
57    final public static function getFallbackFixturesPath(): string
58    {
59        return __DIR__ . '/../resources';
60    }
61}