Passed
Push — main ( 3acca2...274d1b )
by Oscar
03:30
created

OcubomTwigExtraExtension::loadWebpackEncore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-extra-bundle
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\TwigExtraBundle\DependencyInjection;
13
14
use Ocubom\Twig\Extension\HtmlAttributesRuntime;
15
use Ocubom\Twig\Extension\HtmlCompressRuntime;
16
use Ocubom\Twig\Extension\HtmlExtension;
17
use Ocubom\Twig\Extension\Svg\Finder;
18
use Ocubom\Twig\Extension\Svg\FinderInterface;
19
use Ocubom\Twig\Extension\Svg\Library\FontAwesome\Finder as FontAwesomeFinder;
20
use Ocubom\Twig\Extension\Svg\Library\FontAwesomeRuntime;
21
use Ocubom\Twig\Extension\SvgExtension;
22
use Ocubom\Twig\Extension\SvgRuntime;
23
use Ocubom\TwigExtraBundle\Extensions;
24
use Ocubom\TwigExtraBundle\Listener\AddHttpHeadersListener;
25
use Ocubom\TwigExtraBundle\Twig\WebpackEncoreExtension;
26
use Symfony\Component\Config\Definition\ConfigurationInterface;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
1 ignored issue
show
Bug introduced by
The type Symfony\Component\Depend...ection\ContainerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Symfony\Component\DependencyInjection\Extension\Extension;
29
use Symfony\Component\DependencyInjection\Reference;
30
31
class OcubomTwigExtraExtension extends Extension
32
{
33 5
    public function load(array $configs, ContainerBuilder $container): void
34
    {
35 5
        $configuration = $this->getConfiguration($configs, $container);
36
        assert($configuration instanceof ConfigurationInterface);
37 5
        $config = $this->processConfiguration($configuration, $configs);
38
39 5
        foreach (array_keys(Extensions::getClasses()) as $name) {
40 5
            if ($this->isConfigEnabled($container, $config[$name])) {
41 5
                $call = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
42 5
                $this->{'load'.$call}($container, $config[$name]);
43
            }
44
        }
45
46 5
        $this->loadHttpHeaders($container, $config['http_headers']);
47
    }
48
49 5
    private function loadHttpHeaders(ContainerBuilder $container, array $config): void
50
    {
51
        // Filter enabled header rules
52 5
        $headers = array_filter($config, function (array $header): bool {
53 1
            return $header['enabled'] ? true : false;
54 5
        });
55
56
        // Only register listener if some rule is defined
57 5
        if (count($headers) > 0) {
58 1
            $container->register('ocubom_twig_extra.http_headers_listener', AddHttpHeadersListener::class)
59 1
                ->setArguments(array_values($headers))
60 1
                ->addTag('kernel.event_subscriber');
61
        }
62
    }
63
64 4
    private function loadHtml(ContainerBuilder $container, array $config): void
65
    {
66 4
        $container->register('ocubom_twig_extra.twig_html_extension', HtmlExtension::class)
67 4
            ->addTag('twig.extension');
68
69 4
        $container->register('ocubom_twig_extra.twig_html_attributes_runtime', HtmlAttributesRuntime::class)
70 4
            ->addTag('twig.runtime');
71
72 4
        $container->register('ocubom_twig_extra.twig_html_compress_runtime', HtmlCompressRuntime::class)
73 4
            ->setArguments([
74 4
                $config['compression']['force'],
75 4
                $config['compression']['level'],
76 4
            ])
77 4
            ->addTag('twig.runtime');
78
    }
79
80 1
    private function loadSvg(ContainerBuilder $container, array $config): void
81
    {
82 1
        $container
83 1
            ->register('ocubom_twig_extra.twig_svg_extension', SvgExtension::class)
84 1
            ->addTag('twig.extension');
85
86 1
        foreach ($config['finders'] as $name => $paths) {
87 1
            $hash = sha1(serialize($paths));
88 1
            $key = ".ocubom_twig_extra.svg.finder.{$hash}";
89
90
            // Register finder if not exists
91 1
            if (!$container->has($key)) {
92 1
                $container
93 1
                    ->register($key, Finder::class)
94 1
                    ->setArguments($paths)
95 1
                    ->setPublic(false);
96
            }
97
98
            // Create a hidden alias
99 1
            $container->setAlias(".ocubom_twig_extra.svg.{$name}_finder.inner", $key);
100
        }
101
102
        // Register default runtime
103 1
        if ($container->has('.ocubom_twig_extra.svg.default_finder.inner')) {
104
            // Register runtime
105 1
            $container
106 1
                ->register('ocubom_twig_extra.twig_svg_runtime', SvgRuntime::class)
107 1
                ->setArguments([
108 1
                    new Reference('ocubom_twig_extra.svg.default_finder'),
109 1
                    new Reference('logger'),
110 1
                ])
111 1
                ->addTag('twig.runtime');
112
113
            // Create default finder (just an alias)
114 1
            $container->setAlias('ocubom_twig_extra.svg.default_finder', '.ocubom_twig_extra.svg.default_finder.inner');
115
116
            // Create class aliases
117 1
            $container->setAlias(FinderInterface::class, 'ocubom_twig_extra.svg.default_finder');
118
        }
119
120
        // Register fontawesome runtime
121 1
        if ($container->has('.ocubom_twig_extra.svg.fontawesome_finder.inner')) {
122
            // Register runtime
123 1
            $container
124 1
                ->register('ocubom_twig_extra.twig_fontawesome_runtime', FontAwesomeRuntime::class)
125 1
                ->setArguments([
126 1
                    new Reference('ocubom_twig_extra.svg.fontawesome_finder'),
127 1
                    new Reference('logger'),
128 1
                ])
129 1
                ->addTag('twig.runtime');
130
131
            // Create fontawesome finder
132 1
            $container
133 1
                ->register('ocubom_twig_extra.svg.fontawesome_finder', FontAwesomeFinder::class)
134 1
                ->setArguments([
135 1
                    new Reference('.ocubom_twig_extra.svg.fontawesome_finder.inner'),
136 1
                    new Reference('logger'),
137 1
                ]);
138
139
            // Create class aliases
140 1
            $container->setAlias(FontAwesomeFinder::class, 'ocubom_twig_extra.svg.fontawesome_finder');
141
        }
142
    }
143
144 4
    private function loadWebpackEncore(ContainerBuilder $container, array $config): void
145
    {
146 4
        $container->register('ocubom_twig_extra.twig_webpack_encore_extension', WebpackEncoreExtension::class)
147 4
            ->setArguments([
148 4
                new Reference('webpack_encore.entrypoint_lookup_collection'),
149 4
                $config['output_paths'],
150 4
            ])
151 4
            ->addTag('twig.extension');
152
    }
153
}
154