Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
61.54% covered (warning)
61.54%
8 / 13
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ComponentExtension
61.54% covered (warning)
61.54%
8 / 13
60.00% covered (warning)
60.00%
3 / 5
6.42
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctions
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 renderApieCmsData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 renderStacktrace
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderApieComponent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\ApieBundle\Twig;
3
4use Apie\Core\Context\ApieContext;
5use Apie\HtmlBuilders\ErrorHandler\StacktraceRenderer;
6use Apie\HtmlBuilders\Factories\FieldDisplayComponentFactory;
7use Apie\HtmlBuilders\Interfaces\ComponentInterface;
8use Apie\HtmlBuilders\Interfaces\ComponentRendererInterface;
9use Throwable;
10use Twig\Extension\AbstractExtension;
11use Twig\TwigFunction;
12
13class ComponentExtension extends AbstractExtension
14{
15    public function __construct(
16        private readonly ComponentRendererInterface $renderer,
17        private readonly FieldDisplayComponentFactory $fieldDisplayComponentFactory
18    ) {
19    }
20
21    public function getFunctions(): array
22    {
23        return [
24            new TwigFunction('renderApieComponent', [$this, 'renderApieComponent'], ['is_safe' => ['all']]),
25            new TwigFunction('renderStacktrace', [$this, 'renderStacktrace'], ['is_safe' => ['all']]),
26            new TwigFunction('renderApieCmsData', [$this, 'renderApieCmsData'], ['is_safe' => ['all']])
27        ];
28    }
29
30    public function renderApieCmsData(
31        mixed $input,
32        ApieContext $apieContext = new ApieContext()
33    ): string {
34        return $this->renderApieComponent(
35            $this->fieldDisplayComponentFactory->createDisplayFor($input, $apieContext),
36            $apieContext
37        );
38    }
39
40    public function renderStacktrace(Throwable $throwable): string
41    {
42        $renderer = new StacktraceRenderer($throwable);
43        return (string) $renderer;
44    }
45
46    public function renderApieComponent(
47        ComponentInterface $component,
48        ApieContext $apieContext = new ApieContext()
49    ): string {
50        return $this->renderer->render($component, $apieContext);
51    }
52}