Completed
Push — master ( b1c6ca...52cd2c )
by Kamil
77:57 queued 60:05
created

TemplateEventExtension::renderBlocksForEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UiBundle\Twig;
15
16
use Twig\Environment;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFunction;
19
20
final class TemplateEventExtension extends AbstractExtension
21
{
22
    /** @var Environment */
23
    private $twig;
24
25
    /**
26
     * @var array
27
     *
28
     * @psalm-var array<string, list<string>>
29
     */
30
    private $eventsToTemplates;
31
32
    public function __construct(Environment $twig, array $eventsToTemplates)
33
    {
34
        $this->twig = $twig;
35
        $this->eventsToTemplates = $eventsToTemplates;
36
    }
37
38
    public function getFunctions(): array
39
    {
40
        return [
41
            new TwigFunction('sylius_template_event', [$this, 'renderBlocksForEvent'], ['is_safe' => ['html']]),
42
        ];
43
    }
44
45
    public function renderBlocksForEvent(string $event, array $options = []): string
46
    {
47
        $templates = $this->eventsToTemplates[$event] ?? [];
48
49
        $renderedTemplates = [];
50
51
        foreach ($templates as $template) {
52
            $renderedTemplates[] = $this->twig->render($template, $options);
53
        }
54
55
        return implode("\n", $renderedTemplates);
56
    }
57
}
58