Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Renderer
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 addRenderer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Renderer;
6
7use Psr\Log\LoggerAwareTrait;
8
9class Renderer
10{
11    use LoggerAwareTrait;
12
13    /** @var EngineInterface[] */
14    protected array $renderer = [];
15
16    /**
17     * Render a template
18     *
19     * @param mixed[] $data
20     */
21    public function render(string $template, array $data = []): string
22    {
23        foreach ($this->renderer as $renderer) {
24            if (!$renderer->canRender($template)) {
25                continue;
26            }
27
28            return $renderer->get($template, $data);
29        }
30
31        $this->logger?->critical('Unable to find a renderer for template file "{file}"', ['file' => $template]);
32
33        return '';
34    }
35
36    /**
37     * Add a new renderer engine
38     */
39    public function addRenderer(EngineInterface $renderer): void
40    {
41        $this->renderer[] = $renderer;
42    }
43}