Completed
Push — 2.0 ( d3085d...14a181 )
by Alex
04:58
created

TwigResponderConfiguration::envTwig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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 Equip\Responder\FormattedResponder;
13
use Twig_Environment;
14
15
final class TwigResponderConfiguration implements ConfigurationInterface
16
{
17
    use EnvTrait;
18
19
    const PREFIX = 'TWIG_';
20
21 1
    public function apply(Injector $injector)
22
    {
23 1
        $env = $this->env;
24
25 1
        $path = $env->getValue('TWIG_TEMPLATES');
26
27 1
        $injector->define(FilesystemLoader::class, [
28 1
            ':path'           => $path,
29 1
            ':fileExtensions' => $this->fileExtensions($env),
30
        ]);
31
32 1
        $injector->define(Twig_Environment::class, [
33 1
            'loader'   => FilesystemLoader::class,
34 1
            ':options' => $this->options($env),
35
        ]);
36
37
        $injector->prepare(FormattedResponder::class, function(FormattedResponder $responder) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
38
            return $responder->withValue(TwigFormatter::class, 1.0);
39 1
        });
40 1
    }
41
42 1
    private function fileExtensions(Env $env): array
43
    {
44 1
        $fileExtensions = $env->getValue('TWIG_FILE_EXTENSIONS', 'html.twig,twig');
45
46 1
        return explode(',', $fileExtensions);
47
    }
48
49 1
    private function options(Env $env): array
50
    {
51 1
       static $options = [
52
            'debug'               => false,
53
            'strict_variables'    => false,
54
            'cache'               => false,
55
            'auto_reload'         => false,
56
            'charset'             => 'UTF-8',
57
            'autoescape'          => 'html',
58
            'base_template_class' => 'Twig_Template',
59
            'optimizations'       => -1,
60
        ];
61
62 1
        foreach ($this->envTwig($env) as $option => $value) {
63 1
            list(, $option) = explode('_', strtolower($option), 2);
64 1
            if (isset($options[$option])) {
65 1
                $options[$option] = $value;
66
            }
67
        }
68
69 1
        return $options;
70
    }
71
72
    private function envTwig(Env $env): array
73
    {
74 1
        $twigFilter = static function ($value) {
75 1
            return stristr($value, self::PREFIX);
76 1
        };
77
78 1
        return array_filter($env->toArray(), $twigFilter, ARRAY_FILTER_USE_KEY);
79
    }
80
}
81