summaryrefslogtreecommitdiff
path: root/src/Renderer/Renderer.php
blob: de31ca741be2288ae8b89787f45e1a48738867c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace Engelsystem\Renderer;

use Psr\Log\LoggerAwareTrait;

class Renderer
{
    use LoggerAwareTrait;

    /** @var EngineInterface[] */
    protected $renderer = [];

    /**
     * Render a template
     *
     * @param string  $template
     * @param mixed[] $data
     * @return string
     */
    public function render($template, $data = [])
    {
        foreach ($this->renderer as $renderer) {
            if (!$renderer->canRender($template)) {
                continue;
            }

            return $renderer->get($template, $data);
        }

        if ($this->logger) {
            $this->logger->error('Unable to find a renderer for template file "{file}"', ['file' => $template]);
        }

        return '';
    }

    /**
     * Add a new renderer engine
     *
     * @param EngineInterface $renderer
     */
    public function addRenderer(EngineInterface $renderer)
    {
        $this->renderer[] = $renderer;
    }
}