Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
TwigPositionExtension
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
3
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
 getFunctions
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 renderPositionHtml
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Layout;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use Twig\Extension\AbstractExtension;
12use Twig\Markup;
13use Twig\TwigFunction;
14
15/**
16 * Twig Extension for Layout Position Engine.
17 *
18 * Exposes {{ render_position('position_code', 'theme_name') }} function in Twig templates.
19 *
20 * @package App\Core\Layout
21 */
22final class TwigPositionExtension extends AbstractExtension
23{
24    /**
25     * TwigPositionExtension constructor.
26     *
27     * @param PositionManager $positionManager Layout position manager instance.
28     */
29    public function __construct(
30        private readonly PositionManager $positionManager
31    ) {
32    }
33
34    /**
35     * Returns registered Twig functions array.
36     *
37     * @return array<int, TwigFunction> Twig functions array.
38     */
39    public function getFunctions(): array
40    {
41        return [
42            new TwigFunction(
43                'render_position',
44                [$this, 'renderPositionHtml'],
45                ['is_safe' => ['html']]
46            ),
47        ];
48    }
49
50    /**
51     * Renders position blocks and wraps output in Twig Markup object to prevent auto-escaping.
52     *
53     * @param string $positionCode Layout position code identifier.
54     * @param string $themeName Active theme name string.
55     * @return Markup Safe Twig Markup instance.
56     */
57    public function renderPositionHtml(string $positionCode, string $themeName): Markup
58    {
59        $html = $this->positionManager->renderPosition($positionCode, $themeName);
60        return new Markup($html, 'UTF-8');
61    }
62}