Completed
Push — master ( 2f993a...23bc63 )
by Nikola
02:26
created

Extension   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.67%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 8
dl 0
loc 164
ccs 74
cts 75
cp 0.9867
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 14 3
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 9
    public function getAlias(): string
21
    {
22 9
        return 'runopencode_query_resources_loader';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 9
    public function getNamespace(): string
29
    {
30 9
        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 7
    public function load(array $configs, ContainerBuilder $container): void
47
    {
48 7
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
49 7
        $loader->load('services.xml');
50
51 7
        $configuration = new Configuration();
52 7
        $config        = $this->processConfiguration($configuration, $configs);
53
54 7
        $this->configureTwigGlobals($config, $container);
55 7
        $this->configureTwigEnvironment($config, $container);
56 7
        $this->configureTwigWarmUpCommand($config, $container);
57 7
        $this->configureTwigResourcePaths($config, $container);
58 7
        $this->configureTwigBundlePaths($config, $container);
59
60 7
        if (null !== $config['default_executor']) {
61 1
            $container->setParameter('runopencode.query_resources_loader.default_executor', $config['default_executor']);
62
        }
63
64 7
        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 7
        unset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'], $config['twig']['globals']);
69
70 7
        $container->getDefinition('runopencode.query_resources_loader.twig')->replaceArgument(1, $config['twig']);
71 7
    }
72
73
    /**
74
     * @param array<string, mixed> $config
75
     */
76 7
    private function configureTwigGlobals(array $config, ContainerBuilder $container): void
77
    {
78 7
        if (!$container->hasDefinition('runopencode.query_resources_loader.twig')) {
79
            return;
80
        }
81
82 7
        if (empty($config['twig']['globals'])) {
83 5
            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 7
    private function configureTwigEnvironment(array $config, ContainerBuilder $container): void
102
    {
103 7
        $configurator = $container->getDefinition('runopencode.query_resources_loader.twig.configurator.environment');
104 7
        $configurator->replaceArgument(0, $config['twig']['date']['format']);
105 7
        $configurator->replaceArgument(1, $config['twig']['date']['interval_format']);
106 7
        $configurator->replaceArgument(2, $config['twig']['date']['timezone']);
107 7
        $configurator->replaceArgument(3, $config['twig']['number_format']['decimals']);
108 7
        $configurator->replaceArgument(4, $config['twig']['number_format']['decimal_point']);
109 7
        $configurator->replaceArgument(5, $config['twig']['number_format']['thousands_separator']);
110 7
    }
111
112
    /**
113
     * @param array<string, mixed> $config
114
     */
115 7
    private function configureTwigWarmUpCommand(array $config, ContainerBuilder $container): void
116
    {
117
        $container
118 7
            ->getDefinition('runopencode.query_resources_loader.twig.query_sources_iterator')
119 7
            ->replaceArgument(2, $config['twig']['paths']);
120 7
    }
121
122
    /**
123
     * @param array<string, mixed> $config
124
     */
125 7
    private function configureTwigResourcePaths(array $config, ContainerBuilder $container): void
126
    {
127 7
        $loader = $container->getDefinition('runopencode.query_resources_loader.twig.loader.filesystem');
128
129
        // register user-configured paths
130 7
        foreach ($config['twig']['paths'] as $path => $namespace) {
131 2
            if (!$namespace) {
132 2
                $loader->addMethodCall('addPath', [$path]);
133 2
                continue;
134
            }
135
136 2
            $loader->addMethodCall('addPath', [$path, $namespace]);
137
        }
138 7
    }
139
140
    /**
141
     * @param array<string, mixed> $config
142
     */
143 7
    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...
144
    {
145 7
        $loader      = $container->getDefinition('runopencode.query_resources_loader.twig.loader.filesystem');
146
        $addTwigPath = static function ($dir, $bundle) use ($loader) {
147 1
            $name = $bundle;
148
149 1
            if ('Bundle' === \substr($name, -6)) {
150 1
                $name = \substr($name, 0, -6);
151
            }
152
153 1
            $loader->addMethodCall('addPath', [$dir, $name]);
154 7
        };
155
156
        // register bundles as Twig namespaces
157 7
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
158 1
            $dir = $container->getParameter('kernel.root_dir') . '/Resources/' . $bundle . '/query';
159
160 1
            if (\is_dir($dir)) {
161 1
                $addTwigPath($dir, $bundle);
162
            }
163
164 1
            $container->addResource(new FileExistenceResource($dir));
165
166 1
            $reflection = new \ReflectionClass($class);
167
            /** @var string $filename */
168 1
            $filename = $reflection->getFileName();
169 1
            $dir      = \dirname($filename) . '/Resources/query';
170
171 1
            if (\is_dir($dir)) {
172 1
                $addTwigPath($dir, $bundle);
173
            }
174
175 1
            $container->addResource(new FileExistenceResource($dir));
176
        }
177 7
    }
178
}
179