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

Extension::configureTwigWarmUpCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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;
11
12
use RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration\Configuration;
13
use Symfony\Component\Config\FileLocator;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
16
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
17
use Symfony\Component\Config\Resource\FileExistenceResource;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * Class Extension
22
 *
23
 * Bundle extension.
24
 *
25
 * @package RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection
26
 */
27
class Extension extends BaseExtension
28
{
29
    public function getAlias()
30
    {
31
        return 'run_open_code_query_resources_loader';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load(array $configs, ContainerBuilder $container)
38
    {
39
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
40
        $loader->load('services.xml');
41
42
        $configuration = new Configuration();
43
        $config = $this->processConfiguration($configuration, $configs);
44
45
        $this
46
            ->configureTwigGlobals($config, $container)
47
            ->configureTwigEnvironment($config, $container)
48
            ->configureTwigWarmUpCommand($config, $container)
49
            ->configureTwigResourcePaths($config, $container)
50
            ->configureTwigBundlePaths($config, $container)
51
            ;
52
53
        if (null !== $config['default_executor']) {
54
            $container->setParameter('run_open_code.query_resources_loader.default_executor', $config['default_executor']);
55
        }
56
57
        if (isset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'])) {
58
            $config['twig']['autoescape'] = array(new Reference($config['twig']['autoescape_service']), $config['twig']['autoescape_service_method']);
59
        }
60
61
        unset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'], $config['twig']['globals']);
62
63
        $container->getDefinition('run_open_code.query_resources_loader.twig')->replaceArgument(1, $config['twig']);
64
    }
65
66
    /**
67
     * @param array $config
68
     * @param ContainerBuilder $container
69
     * @return Extension $this
70
     */
71
    protected function configureTwigGlobals(array $config, ContainerBuilder $container)
72
    {
73
        if (false !== $container->hasDefinition('run_open_code.query_resources_loader.twig') && !empty($config['twig']['globals'])) {
74
75
            $definition = $container->getDefinition('run_open_code.query_resources_loader.twig');
76
77
            foreach ($config['globals'] as $key => $global) {
78
79
                if (isset($global['type']) && 'service' === $global['type']) {
80
                    $definition->addMethodCall('addGlobal', array($key, new Reference($global['id'])));
81
                } else {
82
                    $definition->addMethodCall('addGlobal', array($key, $global['value']));
83
                }
84
            }
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param array $config
92
     * @param ContainerBuilder $container
93
     * @return Extension $this
94
     */
95
    protected function configureTwigEnvironment(array $config, ContainerBuilder $container)
96
    {
97
        $envConfiguratorDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.configurator.environment');
98
        $envConfiguratorDefinition->replaceArgument(0, $config['twig']['date']['format']);
99
        $envConfiguratorDefinition->replaceArgument(1, $config['twig']['date']['interval_format']);
100
        $envConfiguratorDefinition->replaceArgument(2, $config['twig']['date']['timezone']);
101
        $envConfiguratorDefinition->replaceArgument(3, $config['twig']['number_format']['decimals']);
102
        $envConfiguratorDefinition->replaceArgument(4, $config['twig']['number_format']['decimal_point']);
103
        $envConfiguratorDefinition->replaceArgument(5, $config['twig']['number_format']['thousands_separator']);
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param array $config
110
     * @param ContainerBuilder $container
111
     * @return Extension $this
112
     */
113
    protected function configureTwigWarmUpCommand(array $config, ContainerBuilder $container)
114
    {
115
        $container->getDefinition('run_open_code.query_resources_loader.twig.query_sources_iterator')->replaceArgument(2, $config['twig']['paths']);
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param array $config
122
     * @param ContainerBuilder $container
123
     * @return Extension $this
124
     */
125
    protected function configureTwigResourcePaths(array $config, ContainerBuilder $container)
126
    {
127
        $twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem');
128
129
        // register user-configured paths
130
        foreach ($config['twig']['paths'] as $path => $namespace) {
131
            if (!$namespace) {
132
                $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
133
            } else {
134
                $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
135
            }
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param array $config
143
     * @param ContainerBuilder $container
144
     * @return Extension $this
145
     */
146
    protected function configureTwigBundlePaths(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148
        $twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem');
149
150
        $addTwigPath = function($dir, $bundle) use ($twigFilesystemLoaderDefinition) {
151
152
            $name = $bundle;
153
154 View Code Duplication
            if ('Bundle' === substr($name, -6)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
                $name = substr($name, 0, -6);
156
            }
157
158
            $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
159
        };
160
161
        // register bundles as Twig namespaces
162
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
163
164
            $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/query';
165
166
            if (is_dir($dir)) {
167
                $addTwigPath($dir, $bundle);
168
            }
169
170
            $container->addResource(new FileExistenceResource($dir));
171
172
            $reflection = new \ReflectionClass($class);
173
            $dir = dirname($reflection->getFileName()).'/Resources/query';
174
175
            if (is_dir($dir)) {
176
                $addTwigPath($dir, $bundle);
177
            }
178
179
            $container->addResource(new FileExistenceResource($dir));
180
        }
181
182
        return $this;
183
    }
184
}
185