Completed
Push — 2.x-dev-kit ( c8b106...bd0076 )
by Oskar
01:28
created

SonataSeoExtension::fixConfiguration()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 7.9337
c 0
b 0
f 0
cc 8
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
        $loader->load('commands.xml');
49
50
        $this->configureSeoPage($config['page'], $container);
51
        $this->configureSitemap($config['sitemap'], $container);
52
53
        $container->getDefinition('sonata.seo.twig.extension')
54
            ->replaceArgument(1, $config['encoding']);
55
    }
56
57
    /**
58
     * Configure the default seo page.
59
     *
60
     * @param array            $config
61
     * @param ContainerBuilder $container
62
     */
63
    protected function configureSeoPage(array $config, ContainerBuilder $container): void
64
    {
65
        $container->setParameter('sonata.seo.config', $config);
66
    }
67
68
    /**
69
     * Configure the sitemap source manager.
70
     *
71
     * @param array            $config
72
     * @param ContainerBuilder $container
73
     */
74
    protected function configureSitemap(array $config, ContainerBuilder $container): void
75
    {
76
        $source = $container->getDefinition('sonata.seo.sitemap.manager');
77
78
        if (method_exists($source, 'setShared')) { // Symfony 2.8+
79
            $source->setShared(false);
80
        } else {
81
            // For Symfony <2.8 compatibility
82
            $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...
83
        }
84
85
        foreach ($config['doctrine_orm'] as $pos => $sitemap) {
86
            // define the connectionIterator
87
            $connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos;
88
89
            $connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', [
90
                new Reference($sitemap['connection']),
91
                $sitemap['query'],
92
            ]);
93
94
            $connectionIterator->setPublic(false);
95
            $container->setDefinition($connectionIteratorId, $connectionIterator);
96
97
            // define the sitemap proxy iterator
98
            $sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos;
99
100
            $sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', [
101
                new Reference($connectionIteratorId),
102
                new Reference('router'),
103
                $sitemap['route'],
104
                $sitemap['parameters'],
105
            ]);
106
107
            $sitemapIterator->setPublic(false);
108
109
            $container->setDefinition($sitemapIteratorId, $sitemapIterator);
110
111
            $source->addMethodCall('addSource', [$sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']]);
112
        }
113
114
        foreach ($config['services'] as $service) {
115
            $source->addMethodCall('addSource', [$service['group'], new Reference($service['id']), $service['types']]);
116
        }
117
    }
118
119
    /**
120
     * Fix the sitemap configuration.
121
     *
122
     * @param array $config
123
     *
124
     * @return array
125
     */
126
    protected function fixConfiguration(array $config)
127
    {
128
        foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) {
129
            $sitemap['group'] = $sitemap['group'] ?? false;
130
            $sitemap['types'] = $sitemap['types'] ?? [];
131
            $sitemap['connection'] = $sitemap['connection'] ?? 'doctrine.dbal.default_connection';
132
            $sitemap['route'] = $sitemap['route'] ?? false;
133
            $sitemap['parameters'] = $sitemap['parameters'] ?? false;
134
            $sitemap['query'] = $sitemap['query'] ?? false;
135
136
            if (false === $sitemap['route']) {
137
                throw new \RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration');
138
            }
139
140
            if (false === $sitemap['query']) {
141
                throw new \RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration');
142
            }
143
144
            if (false === $sitemap['parameters']) {
145
                throw new \RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration');
146
            }
147
148
            $config['sitemap']['doctrine_orm'][$pos] = $sitemap;
149
        }
150
151
        foreach ($config['sitemap']['services'] as $pos => $sitemap) {
152
            if (!\is_array($sitemap)) {
153
                $sitemap = [
154
                    'group' => false,
155
                    'types' => [],
156
                    'id' => $sitemap,
157
                ];
158
            } else {
159
                $sitemap['group'] = $sitemap['group'] ?? false;
160
                $sitemap['types'] = $sitemap['types'] ?? [];
161
162
                if (!isset($sitemap['id'])) {
163
                    throw new \RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration');
164
                }
165
            }
166
167
            $config['sitemap']['services'][$pos] = $sitemap;
168
        }
169
170
        return $config;
171
    }
172
}
173