Completed
Push — master ( 4b13e9...ce6bc8 )
by Nikola
03:50
created

TwigNodeDefinition::configureTwigGlobals()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 33
cts 33
cp 1
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 39
nc 1
nop 0
crap 5
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration;
11
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
14
/**
15
 * Class TwigNodeDefinition
16
 *
17
 * Twig environment configuration.
18
 *
19
 * @package RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration
20
 * @internal
21
 */
22
class TwigNodeDefinition extends ArrayNodeDefinition
23
{
24 8
    public function __construct()
25
    {
26 8
        parent::__construct('twig');
27
28
        $this
29 8
            ->configureTwigOptions()
30 8
            ->configureTwigFormatOptions()
31 8
            ->addDefaultsIfNotSet()
32 8
            ->configureTwigGlobals()
33
        ;
34 8
    }
35
36
    /**
37
     * Configure Twig options.
38
     *
39
     * @return TwigNodeDefinition $this
40
     */
41 8
    private function configureTwigOptions()
42
    {
43
        $this
44 8
            ->fixXmlConfig('path')
45 8
            ->children()
46 8
                ->variableNode('autoescape')->defaultValue(false)->end()
47 8
                ->scalarNode('autoescape_service')->defaultNull()->end()
48 8
                ->scalarNode('autoescape_service_method')->defaultNull()->end()
49 8
                ->scalarNode('base_template_class')->example('Twig_Template')->cannotBeEmpty()->end()
50 8
                ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/query_resources/twig')->end()
51 8
                ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
52 8
                ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
53 8
                ->booleanNode('strict_variables')->end()
54 8
                ->scalarNode('auto_reload')->end()
55 8
                ->integerNode('optimizations')->min(-1)->end()
56 8
                ->arrayNode('paths')
57 8
                    ->normalizeKeys(false)
58 8
                    ->useAttributeAsKey('paths')
59 8
                    ->beforeNormalization()
60 8
                    ->always()
61
                    ->then(function ($paths) {
62 3
                        $normalized = array();
63 3
                        foreach ($paths as $path => $namespace) {
64 3
                            if (is_array($namespace)) {
65
                                // xml
66 1
                                $path = $namespace['value'];
67 1
                                $namespace = $namespace['namespace'];
68
                            }
69
70
                            // path within the default namespace
71 3
                            if (ctype_digit((string) $path)) {
72
                                $path = $namespace;
73
                                $namespace = null;
74
                            }
75
76 3
                            $normalized[$path] = $namespace;
77
                        }
78
79 3
                        return $normalized;
80 8
                    })
81 8
                    ->end()
82 8
                    ->prototype('variable')->end()
83 8
                ->end()
84 8
            ->end()
85
        ;
86
87 8
        return $this;
88
    }
89
90
    /**
91
     * Configure Twig format options.
92
     *
93
     * @return TwigNodeDefinition $this
94
     */
95 8
    private function configureTwigFormatOptions()
96
    {
97
        $this
98 8
            ->children()
99 8
                ->arrayNode('date')
100 8
                    ->info('The default format options used by the date filter')
101 8
                    ->addDefaultsIfNotSet()
102 8
                    ->children()
103 8
                        ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
104 8
                        ->scalarNode('interval_format')->defaultValue('%d days')->end()
105 8
                        ->scalarNode('timezone')
106 8
                            ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
107 8
                            ->defaultNull()
108 8
                        ->end()
109 8
                    ->end()
110 8
                ->end()
111 8
                ->arrayNode('number_format')
112 8
                    ->info('The default format options for the number_format filter')
113 8
                    ->addDefaultsIfNotSet()
114 8
                    ->children()
115 8
                        ->integerNode('decimals')->defaultValue(0)->end()
116 8
                        ->scalarNode('decimal_point')->defaultValue('.')->end()
117 8
                        ->scalarNode('thousands_separator')->defaultValue(',')->end()
118 8
                    ->end()
119 8
                ->end()
120 8
            ->end()
121
        ;
122
123 8
        return $this;
124
    }
125
126
    /**
127
     * Configure Twig globals.
128
     *
129
     * @return TwigNodeDefinition $this
130
     */
131 8
    private function configureTwigGlobals()
132
    {
133
        $this
134 8
            ->fixXmlConfig('global')
135 8
            ->children()
136 8
                ->arrayNode('globals')
137 8
                    ->normalizeKeys(false)
138 8
                    ->useAttributeAsKey('key')
139 8
                    ->example(array('foo' => '"@bar"', 'pi' => 3.14))
140 8
                    ->prototype('array')
141 8
                        ->beforeNormalization()
142
                            ->ifTrue(function ($v) { return is_string($v) && 0 === strpos($v, '@'); })
143
                            ->then(function ($v) {
144
                                if (0 === strpos($v, '@@')) {
145
                                    return substr($v, 1);
146
                                }
147
148
                                return array('id' => substr($v, 1), 'type' => 'service');
149 8
                            })
150 8
                        ->end()
151 8
                        ->beforeNormalization()
152
                            ->ifTrue(function ($v) {
153 2
                                if (is_array($v)) {
154 2
                                    $keys = array_keys($v);
155 2
                                    sort($keys);
156
157 2
                                    return $keys !== array('id', 'type') && $keys !== array('value');
158
                                }
159
160 1
                                return true;
161 8
                            })
162
                            ->then(function ($v) { return array('value' => $v); })
163 8
                        ->end()
164 8
                        ->children()
165 8
                            ->scalarNode('id')->end()
166 8
                            ->scalarNode('type')
167 8
                                ->validate()
168 8
                                    ->ifNotInArray(array('service'))
169 8
                                    ->thenInvalid('The %s type is not supported')
170 8
                                ->end()
171 8
                            ->end()
172 8
                            ->variableNode('value')->end()
173 8
                        ->end()
174 8
                    ->end()
175 8
                ->end()
176 8
            ->end();
177
178 8
        return $this;
179
    }
180
}
181