Completed
Push — master ( 23e2ec...d48e38 )
by Nikola
05:48
created

Extension   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 72.84%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 8
dl 0
loc 174
ccs 59
cts 81
cp 0.7284
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 4 1
A getNamespace() 0 4 1
A getXsdValidationBasePath() 0 4 1
A load() 0 26 3
B configureTwigGlobals() 0 21 6
A configureTwigEnvironment() 0 10 1
A configureTwigWarmUpCommand() 0 6 1
A configureTwigResourcePaths() 0 24 4
A configureTwigBundlePaths() 0 35 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection;
6
7
use RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration\Configuration;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
11
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
12
use Symfony\Component\Config\Resource\FileExistenceResource;
13
use Symfony\Component\DependencyInjection\Reference;
14
15
final class Extension extends BaseExtension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 6
    public function getAlias(): string
21
    {
22 6
        return 'runopencode_query_resources_loader';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 6
    public function getNamespace(): string
29
    {
30 6
        return 'http://www.runopencode.com/xsd-schema/query-resources-loader-bundle';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function getXsdValidationBasePath(): string
37
    {
38 2
        return __DIR__ . '/../Resources/config/schema';
39
    }
40
41
    /**
42
     * @param array<string, mixed> $configs
43
     *
44
     * @throws \Exception
45
     */
46 4
    public function load(array $configs, ContainerBuilder $container): void
47
    {
48 4
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
49 4
        $loader->load('services.xml');
50
51 4
        $configuration = new Configuration();
52 4
        $config        = $this->processConfiguration($configuration, $configs);
53
54 4
        $this->configureTwigGlobals($config, $container);
55 4
        $this->configureTwigEnvironment($config, $container);
56 4
        $this->configureTwigWarmUpCommand($config, $container);
57 4
        $this->configureTwigResourcePaths($config, $container);
58 4
        $this->configureTwigBundlePaths($config, $container);
59
60 4
        if (null !== $config['default_executor']) {
61 1
            $container->setParameter('runopencode.query_resources_loader.default_executor', $config['default_executor']);
62
        }
63
64 4
        if (isset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'])) {
65 1
            $config['twig']['autoescape'] = [new Reference($config['twig']['autoescape_service']), $config['twig']['autoescape_service_method']];
66
        }
67
68 4
        unset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'], $config['twig']['globals']);
69
70 4
        $container->getDefinition('runopencode.query_resources_loader.twig')->replaceArgument(1, $config['twig']);
71 4
    }
72
73
    /**
74
     * @param array<string, mixed> $config
75
     */
76 4
    private function configureTwigGlobals(array $config, ContainerBuilder $container): void
77
    {
78 4
        if (!$container->hasDefinition('runopencode.query_resources_loader.twig')) {
79
            return;
80
        }
81
82 4
        if (empty($config['twig']['globals'])) {
83 2
            return;
84
        }
85
86 2
        $definition = $container->getDefinition('runopencode.query_resources_loader.twig');
87
88 2
        foreach ($config['twig']['globals'] as $key => $global) {
89 2
            if (isset($global['type']) && 'service' === $global['type']) {
90 1
                $definition->addMethodCall('addGlobal', [$key, new Reference($global['id'])]);
91 1
                continue;
92
            }
93
94 1
            $definition->addMethodCall('addGlobal', [$key, $global['value']]);
95
        }
96 2
    }
97
98
    /**
99
     * @param array<string, mixed> $config
100
     */
101 4
    private function configureTwigEnvironment(array $config, ContainerBuilder $container): void
102
    {
103 4
        $configurator = $container->getDefinition('runopencode.query_resources_loader.twig.configurator.environment');
104 4
        $configurator->replaceArgument(0, $config['twig']['date']['format']);
105 4
        $configurator->replaceArgument(1, $config['twig']['date']['interval_format']);
106 4
        $configurator->replaceArgument(2, $config['twig']['date']['timezone']);
107 4
        $configurator->replaceArgument(3, $config['twig']['number_format']['decimals']);
108 4
        $configurator->replaceArgument(4, $config['twig']['number_format']['decimal_point']);
109 4
        $configurator->replaceArgument(5, $config['twig']['number_format']['thousands_separator']);
110 4
    }
111
112
    /**
113
     * @param array<string, mixed> $config
114
     */
115 4
    private function configureTwigWarmUpCommand(array $config, ContainerBuilder $container): void
116
    {
117
        $container
118 4
            ->getDefinition('runopencode.query_resources_loader.twig.query_sources_iterator')
119 4
            ->replaceArgument(2, $config['twig']['paths']);
120 4
    }
121
122
    /**
123
     * @param array<string, mixed> $config
124
     */
125 4
    private function configureTwigResourcePaths(array $config, ContainerBuilder $container): void
126
    {
127 4
        $loader     = $container->getDefinition('runopencode.query_resources_loader.twig.loader.filesystem');
128 4
        $projectDir = $container->getParameter('kernel.project_dir');
129
130
        // add "query" directory within project directory as default path, if exists
131 4
        $defaultPath = \sprintf('%s/query', $projectDir);
132
133 4
        if (\is_dir($defaultPath)) {
134
            $loader->addMethodCall('addPath', [$defaultPath]);
135
            $container->addResource(new FileExistenceResource($defaultPath));
136
            $container->setParameter('runopencode.query_resources_loader.default_path', $defaultPath);
137
        }
138
139
        // register user-configured paths
140 4
        foreach ($config['twig']['paths'] as $path => $namespace) {
141
            if (!$namespace) {
142
                $loader->addMethodCall('addPath', [$path]);
143
                continue;
144
            }
145
146
            $loader->addMethodCall('addPath', [$path, $namespace]);
147
        }
148 4
    }
149
150
    /**
151
     * @param array<string, mixed> $config
152
     */
153 4
    private function configureTwigBundlePaths(array $config, ContainerBuilder $container): void
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...
154
    {
155 4
        $loader      = $container->getDefinition('runopencode.query_resources_loader.twig.loader.filesystem');
156
        $addTwigPath = static function ($dir, $bundle) use ($loader) {
157
            $name = $bundle;
158
159
            if ('Bundle' === \substr($name, -6)) {
160
                $name = \substr($name, 0, -6);
161
            }
162
163
            $loader->addMethodCall('addPath', [$dir, $name]);
164 4
        };
165
166
        // register bundles as Twig namespaces
167 4
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
168
            $dir = $container->getParameter('kernel.project_dir') . '/query/bundles/' . $bundle;
169
170
            if (\is_dir($dir)) {
171
                $addTwigPath($dir, $bundle);
172
            }
173
174
            $container->addResource(new FileExistenceResource($dir));
175
176
            $reflection = new \ReflectionClass($class);
177
            /** @var string $filename */
178
            $filename = $reflection->getFileName();
179
            $dir      = \dirname($filename) . '/Resources/query';
180
181
            if (\is_dir($dir)) {
182
                $addTwigPath($dir, $bundle);
183
            }
184
185
            $container->addResource(new FileExistenceResource($dir));
186
        }
187 4
    }
188
}
189