Completed
Push — master ( 475b5e...6316b8 )
by Jordi Sala
01:34
created

SonataSeoExtension::fixConfiguration()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.5555
c 0
b 0
f 0
cc 8
eloc 28
nc 11
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\DependencyInjection;
15
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Loader;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
/**
25
 * This is the class that loads and manages your bundle configuration.
26
 */
27
class SonataSeoExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $configs, ContainerBuilder $container): void
33
    {
34
        $configuration = new Configuration();
35
        $config = $this->processConfiguration($configuration, $configs);
36
        $config = $this->fixConfiguration($config);
37
38
        $bundles = $container->getParameter('kernel.bundles');
39
40
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
41
42
        if (isset($bundles['SonataBlockBundle'], $bundles['KnpMenuBundle'])) {
43
            $loader->load('blocks.xml');
44
        }
45
46
        $loader->load('event.xml');
47
        $loader->load('services.xml');
48
49
        $this->configureSeoPage($config['page'], $container);
50
        $this->configureSitemap($config['sitemap'], $container);
51
52
        $container->getDefinition('sonata.seo.twig.extension')
53
            ->replaceArgument(1, $config['encoding']);
54
    }
55
56
    /**
57
     * Configure the default seo page.
58
     *
59
     * @param array            $config
60
     * @param ContainerBuilder $container
61
     */
62
    protected function configureSeoPage(array $config, ContainerBuilder $container): void
63
    {
64
        $definition = $container->getDefinition($config['default']);
65
66
        $definition->addMethodCall('setTitle', [$config['title']]);
67
        $definition->addMethodCall('setMetas', [$config['metas']]);
68
        $definition->addMethodCall('setHtmlAttributes', [$config['head']]);
69
        $definition->addMethodCall('setSeparator', [$config['separator']]);
70
71
        $container->setAlias('sonata.seo.page', $config['default']);
72
    }
73
74
    /**
75
     * Configure the sitemap source manager.
76
     *
77
     * @param array            $config
78
     * @param ContainerBuilder $container
79
     */
80
    protected function configureSitemap(array $config, ContainerBuilder $container): void
81
    {
82
        $source = $container->getDefinition('sonata.seo.sitemap.manager');
83
84
        if (method_exists($source, 'setShared')) { // Symfony 2.8+
85
            $source->setShared(false);
86
        } else {
87
            // For Symfony <2.8 compatibility
88
            $source->setScope(ContainerInterface::SCOPE_PROTOTYPE);
0 ignored issues
show
Bug introduced by
The method setScope() does not seem to exist on object<Symfony\Component...cyInjection\Definition>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        }
90
91
        foreach ($config['doctrine_orm'] as $pos => $sitemap) {
92
            // define the connectionIterator
93
            $connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos;
94
95
            $connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', [
96
                new Reference($sitemap['connection']),
97
                $sitemap['query'],
98
            ]);
99
100
            $connectionIterator->setPublic(false);
101
            $container->setDefinition($connectionIteratorId, $connectionIterator);
102
103
            // define the sitemap proxy iterator
104
            $sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos;
105
106
            $sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', [
107
                new Reference($connectionIteratorId),
108
                new Reference('router'),
109
                $sitemap['route'],
110
                $sitemap['parameters'],
111
            ]);
112
113
            $sitemapIterator->setPublic(false);
114
115
            $container->setDefinition($sitemapIteratorId, $sitemapIterator);
116
117
            $source->addMethodCall('addSource', [$sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']]);
118
        }
119
120
        foreach ($config['services'] as $service) {
121
            $source->addMethodCall('addSource', [$service['group'], new Reference($service['id']), $service['types']]);
122
        }
123
    }
124
125
    /**
126
     * Fix the sitemap configuration.
127
     *
128
     * @param array $config
129
     *
130
     * @return array
131
     */
132
    protected function fixConfiguration(array $config)
133
    {
134
        foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) {
135
            $sitemap['group'] = $sitemap['group'] ?? false;
136
            $sitemap['types'] = $sitemap['types'] ?? [];
137
            $sitemap['connection'] = $sitemap['connection'] ?? 'doctrine.dbal.default_connection';
138
            $sitemap['route'] = $sitemap['route'] ?? false;
139
            $sitemap['parameters'] = $sitemap['parameters'] ?? false;
140
            $sitemap['query'] = $sitemap['query'] ?? false;
141
142
            if (false === $sitemap['route']) {
143
                throw new \RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration');
144
            }
145
146
            if (false === $sitemap['query']) {
147
                throw new \RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration');
148
            }
149
150
            if (false === $sitemap['parameters']) {
151
                throw new \RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration');
152
            }
153
154
            $config['sitemap']['doctrine_orm'][$pos] = $sitemap;
155
        }
156
157
        foreach ($config['sitemap']['services'] as $pos => $sitemap) {
158
            if (!is_array($sitemap)) {
159
                $sitemap = [
160
                    'group' => false,
161
                    'types' => [],
162
                    'id' => $sitemap,
163
                ];
164
            } else {
165
                $sitemap['group'] = $sitemap['group'] ?? false;
166
                $sitemap['types'] = $sitemap['types'] ?? [];
167
168
                if (!isset($sitemap['id'])) {
169
                    throw new \RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration');
170
                }
171
            }
172
173
            $config['sitemap']['services'][$pos] = $sitemap;
174
        }
175
176
        return $config;
177
    }
178
}
179