Completed
Push — master ( b95763...2cb7d7 )
by Nikola
29:49
created

TwigNodeDefinition::configureTwigOptions()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 48
rs 8.7396
cc 4
eloc 35
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 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
 */
21
class TwigNodeDefinition extends ArrayNodeDefinition
22
{
23
    public function __construct()
24
    {
25
        parent::__construct('twig');
26
27
        $this
28
            ->configureTwigOptions()
29
            ->configureTwigFormatOptions()
30
            ->addDefaultsIfNotSet();
31
        ;
32
    }
33
34
    /**
35
     * Configure Twig options.
36
     *
37
     * @return TwigNodeDefinition $this
38
     */
39
    private function configureTwigOptions()
40
    {
41
        $this
42
            ->fixXmlConfig('path')
43
            ->children()
44
                ->variableNode('autoescape')->defaultValue(false)->end()
45
                ->scalarNode('autoescape_service')->defaultNull()->end()
46
                ->scalarNode('autoescape_service_method')->defaultNull()->end()
47
                ->scalarNode('base_template_class')->example('Twig_Template')->cannotBeEmpty()->end()
48
                ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/query_resources/twig')->end()
49
                ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
50
                ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
51
                ->booleanNode('strict_variables')->end()
52
                ->scalarNode('auto_reload')->end()
53
                ->integerNode('optimizations')->min(-1)->end()
54
                ->arrayNode('paths')
55
                    ->normalizeKeys(false)
56
                    ->useAttributeAsKey('paths')
57
                    ->beforeNormalization()
58
                    ->always()
59
                    ->then(function ($paths) {
60
                        $normalized = array();
61
                        foreach ($paths as $path => $namespace) {
62
                            if (is_array($namespace)) {
63
                                // xml
64
                                $path = $namespace['value'];
65
                                $namespace = $namespace['namespace'];
66
                            }
67
68
                            // path within the default namespace
69
                            if (ctype_digit((string) $path)) {
70
                                $path = $namespace;
71
                                $namespace = null;
72
                            }
73
74
                            $normalized[$path] = $namespace;
75
                        }
76
77
                        return $normalized;
78
                    })
79
                    ->end()
80
                    ->prototype('variable')->end()
81
                ->end()
82
            ->end()
83
        ;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Configure Twig format options.
90
     *
91
     * @return TwigNodeDefinition $this
92
     */
93
    private function configureTwigFormatOptions()
94
    {
95
        $this
96
            ->children()
97
                ->arrayNode('date')
98
                    ->info('The default format options used by the date filter')
99
                    ->addDefaultsIfNotSet()
100
                    ->children()
101
                        ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
102
                        ->scalarNode('interval_format')->defaultValue('%d days')->end()
103
                        ->scalarNode('timezone')
104
                            ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
105
                            ->defaultNull()
106
                        ->end()
107
                    ->end()
108
                ->end()
109
                ->arrayNode('number_format')
110
                    ->info('The default format options for the number_format filter')
111
                    ->addDefaultsIfNotSet()
112
                    ->children()
113
                        ->integerNode('decimals')->defaultValue(0)->end()
114
                        ->scalarNode('decimal_point')->defaultValue('.')->end()
115
                        ->scalarNode('thousands_separator')->defaultValue(',')->end()
116
                    ->end()
117
                ->end()
118
            ->end()
119
        ;
120
121
        return $this;
122
    }
123
}
124