Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Renderer
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 createEngine
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 contentType
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 Cosray\View\Boiler;
6
7use Celemas\Boiler\Engine;
8use Cosray\Renderer as RendererInterface;
9use Override;
10
11/**
12 * @psalm-api
13 *
14 * @psalm-import-type DirsInput from \Celemas\Boiler\Engine
15 */
16class Renderer implements RendererInterface
17{
18    /**
19     * @param DirsInput $dirs
20     * @param list<class-string> $trusted
21     */
22    public function __construct(
23        protected string|array $dirs,
24        protected array $defaults = [],
25        protected array $trusted = [],
26        protected bool $autoescape = true,
27        protected string $contentType = 'text/html',
28    ) {}
29
30    /** @param non-empty-string $id */
31    #[Override]
32    public function render(string $id, array $context): string
33    {
34        $dirs = $this->dirs;
35
36        if (count((array) $dirs) === 0) {
37            throw new RendererException('Provide at least one template directory');
38        }
39
40        $engine = $this->createEngine($dirs);
41
42        return $engine->render($id, $context);
43    }
44
45    /** @param DirsInput $dirs */
46    protected function createEngine(string|array $dirs): Engine
47    {
48        return $this->autoescape
49            ? Engine::create($dirs, $this->defaults, $this->trusted)
50            : Engine::unescaped($dirs, $this->defaults, $this->trusted);
51    }
52
53    #[Override]
54    public function contentType(): string
55    {
56        return $this->contentType;
57    }
58}