Completed
Push — master ( bbe0e7...1d2424 )
by jelmer
32:52 queued 27:17
created

IncludeOnceExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 1
rs 9.9
c 3
b 0
f 0
1
<?php
2
3
namespace Common\Core\Twig\Extensions;
4
5
use Twig\Environment;
6
use Twig\Error\LoaderError;
7
use Twig\Extension\AbstractExtension;
8
use Twig\Extension\SandboxExtension;
9
use Twig\TwigFunction;
10
11
final class IncludeOnceExtension extends AbstractExtension
12
{
13
    /** @var array */
14
    private $includedTemplates = [];
15
16 28
    public function getFunctions(): array
17
    {
18
        return [
19 28
            new TwigFunction(
20 28
                'include_once',
21 28
                [$this, 'includeOnce'],
22 28
                ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]
23
            ),
24 28
            new TwigFunction(
25 28
                'is_included',
26 28
                [$this, 'isIncluded']
27
            ),
28 28
            new TwigFunction(
29 28
                'set_included',
30 28
                [$this, 'setIncluded']
31
            ),
32
        ];
33
    }
34
35
    public function includeOnce(
36
        Environment $env,
37
        array $context,
38
        string $template,
39
        array $variables = [],
40
        bool $withContext = true,
41
        bool $ignoreMissing = false,
42
        bool $sandboxed = false
43
    ): string {
44
        if ($this->isIncluded($template)) {
45
            return '';
46
        }
47
48
        $alreadySandboxed = false;
49
        $sandbox = null;
50
        $output = '';
51
        $isSandboxed = $sandboxed && $env->hasExtension(SandboxExtension::class);
52
53
        if ($withContext) {
54
            $variables = array_merge($context, $variables);
55
        }
56
57
        if ($isSandboxed) {
58
            $sandbox = $env->getExtension(SandboxExtension::class);
59
            if (!$alreadySandboxed = $sandbox->isSandboxed()) {
0 ignored issues
show
Bug introduced by
The method isSandboxed() does not exist on Twig\Extension\ExtensionInterface. It seems like you code against a sub-type of Twig\Extension\ExtensionInterface such as Twig\Extension\SandboxExtension or Twig\Tests\TwigTestExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            if (!$alreadySandboxed = $sandbox->/** @scrutinizer ignore-call */ isSandboxed()) {
Loading history...
60
                $sandbox->enableSandbox();
0 ignored issues
show
Bug introduced by
The method enableSandbox() does not exist on Twig\Extension\ExtensionInterface. It seems like you code against a sub-type of Twig\Extension\ExtensionInterface such as Twig\Extension\SandboxExtension or Twig\Tests\TwigTestExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                $sandbox->/** @scrutinizer ignore-call */ 
61
                          enableSandbox();
Loading history...
61
            }
62
        }
63
64
        try {
65
            $loaded = null;
66
            try {
67
                $loaded = $env->resolveTemplate($template);
68
            } catch (LoaderError $e) {
69
                if (!$ignoreMissing) {
70
                    throw $e;
71
                }
72
            }
73
74
            $output = $loaded ? $loaded->render($variables) : '';
75
        } finally {
76
            if ($isSandboxed && !$alreadySandboxed) {
77
                $sandbox->disableSandbox();
0 ignored issues
show
Bug introduced by
The method disableSandbox() does not exist on Twig\Extension\ExtensionInterface. It seems like you code against a sub-type of Twig\Extension\ExtensionInterface such as Twig\Extension\SandboxExtension or Twig\Tests\TwigTestExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                $sandbox->/** @scrutinizer ignore-call */ 
78
                          disableSandbox();
Loading history...
78
            }
79
        }
80
81
        // this needs to happen after we capture the output so we can check if it was previously included
82
        $this->setIncluded($template);
83
84
        return $output;
85
    }
86
87
    public function isIncluded(string $template): bool
88
    {
89
        return $this->includedTemplates[$template] ?? false;
90
    }
91
92
    public function setIncluded(string $template, bool $isIncluded = true): void
93
    {
94
        $this->includedTemplates[$template] = $isIncluded;
95
    }
96
}
97