Completed
Push — master ( 8b9114...86ab3d )
by Alex
07:01 queued 04:44
created

TwigConfiguration::envTwig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
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
    public function apply(Injector $injector)
21
    {
22
        $env = $this->env;
23
24
        $path = $env->getValue('TWIG_TEMPLATES');
25
26
        $injector->define(FilesystemLoader::class, [
27
            ':path'           => $path,
28
            ':fileExtensions' => $this->fileExtensions($env),
29
        ]);
30
31
        $injector->define(Twig_Environment::class, [
32
            'loader'   => FilesystemLoader::class,
33
            ':options' => $this->options($env),
34
        ]);
35
    }
36
37
    private function fileExtensions(Env $env): array
38
    {
39
        $fileExtensions = $env->getValue('TWIG_FILE_EXTENSIONS', 'html.twig,twig');
40
41
        return explode(',', $fileExtensions);
42
    }
43
44
    private function options(Env $env): array
45
    {
46
       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
        foreach ($this->envTwig($env) as $option => $value) {
58
            list(, $option) = explode('_', strtolower($option), 2);
59
            if (isset($options[$option])) {
60
                $options[$option] = $value;
61
            }
62
        }
63
64
        return $options;
65
    }
66
67
    private function envTwig(Env $env): array
68
    {
69
        $twigFilter = static function ($value) {
70
            return stristr($value, self::PREFIX);
71
        };
72
73
        return array_filter($env->toArray(), $twigFilter, ARRAY_FILTER_USE_KEY);
74
    }
75
}
76