TwigConfiguration::options()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 7
cts 7
cp 1
rs 9.2
cc 3
eloc 15
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace AlexMasterov\EquipTwig\Configuration;
5
6
use AlexMasterov\EquipTwig\Loader\FilesystemLoader;
7
use AlexMasterov\EquipTwig\TwigFormatter;
8
use Auryn\Injector;
9
use Equip\Configuration\ConfigurationInterface;
10
use Equip\Configuration\EnvTrait;
11
use Equip\Env;
12
use Twig_Environment;
13
14
final class TwigConfiguration implements ConfigurationInterface
15
{
16
    use EnvTrait;
17
18
    const PREFIX = 'TWIG_';
19
20 1
    public function apply(Injector $injector)
21
    {
22 1
        $env = $this->env;
23
24 1
        $path = $env->getValue('TWIG_TEMPLATES');
25
26 1
        $injector->define(FilesystemLoader::class, [
27 1
            ':path'           => $path,
28 1
            ':fileExtensions' => $this->fileExtensions($env),
29
        ]);
30
31 1
        $injector->define(Twig_Environment::class, [
32 1
            'loader'   => FilesystemLoader::class,
33 1
            ':options' => $this->options($env),
34
        ]);
35 1
    }
36
37 1
    private function fileExtensions(Env $env): array
38
    {
39 1
        $fileExtensions = $env->getValue('TWIG_FILE_EXTENSIONS', 'html.twig,twig');
40
41 1
        return explode(',', $fileExtensions);
42
    }
43
44 1
    private function options(Env $env): array
45
    {
46 1
       static $options = [
47
            'debug'               => false,
48
            'strict_variables'    => false,
49
            'cache'               => false,
50
            'auto_reload'         => false,
51
            'charset'             => 'UTF-8',
52
            'autoescape'          => 'html',
53
            'base_template_class' => 'Twig_Template',
54
            'optimizations'       => -1,
55
        ];
56
57 1
        foreach ($this->envTwig($env) as $option => $value) {
58 1
            list(, $option) = explode('_', strtolower($option), 2);
59 1
            if (isset($options[$option])) {
60 1
                $options[$option] = $value;
61
            }
62
        }
63
64 1
        return $options;
65
    }
66
67
    private function envTwig(Env $env): array
68
    {
69 1
        $twigFilter = static function ($value) {
70 1
            return stristr($value, self::PREFIX);
71 1
        };
72
73 1
        return array_filter($env->toArray(), $twigFilter, ARRAY_FILTER_USE_KEY);
74
    }
75
}
76