Completed
Pull Request — master (#161)
by Christian
06:42
created

SonataSeoExtension::configureHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\SeoBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
/**
23
 * This is the class that loads and manages your bundle configuration.
24
 */
25
class SonataSeoExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function load(array $configs, ContainerBuilder $container)
31
    {
32
        $configuration = new Configuration();
33
        $config = $this->processConfiguration($configuration, $configs);
34
        $config = $this->fixConfiguration($config);
35
36
        $bundles = $container->getParameter('kernel.bundles');
37
38
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
39
40
        if (isset($bundles['SonataBlockBundle']) && isset($bundles['KnpMenuBundle'])) {
41
            $loader->load('blocks.xml');
42
        }
43
44
        $loader->load('event.xml');
45
        $loader->load('services.xml');
46
47
        $this->configureSeoPage($config['page'], $container);
48
        $this->configureSitemap($config['sitemap'], $container);
49
        $this->configureHttpClient($container, $config['http']);
50
        $this->configureClassesToCompile();
51
52
        $container->getDefinition('sonata.seo.twig.extension')
53
            ->replaceArgument(1, $config['encoding']);
54
    }
55
56
    /**
57
     * Add class to compile.
58
     */
59
    public function configureClassesToCompile()
60
    {
61
        $this->addClassesToCompile(array(
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...::addClassesToCompile() has been deprecated with message: since version 3.3, to be removed in 4.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
            'Sonata\\SeoBundle\\Seo\\SeoPage',
63
            'Sonata\\SeoBundle\\Seo\\SeoPageInterface',
64
            'Sonata\\SeoBundle\\Sitemap\\SourceManager',
65
            'Sonata\\SeoBundle\\Twig\\Extension\\SeoExtension',
66
        ));
67
    }
68
69
    /**
70
     * Configure the default seo page.
71
     *
72
     * @param array            $config
73
     * @param ContainerBuilder $container
74
     */
75
    protected function configureSeoPage(array $config, ContainerBuilder $container)
76
    {
77
        $definition = $container->getDefinition($config['default']);
78
79
        $definition->addMethodCall('setTitle', array($config['title']));
80
        $definition->addMethodCall('setMetas', array($config['metas']));
81
        $definition->addMethodCall('setHtmlAttributes', array($config['head']));
82
        $definition->addMethodCall('setSeparator', array($config['separator']));
83
84
        $container->setAlias('sonata.seo.page', $config['default']);
85
    }
86
87
    /**
88
     * Configure the sitemap source manager.
89
     *
90
     * @param array            $config
91
     * @param ContainerBuilder $container
92
     */
93
    protected function configureSitemap(array $config, ContainerBuilder $container)
94
    {
95
        $source = $container->getDefinition('sonata.seo.sitemap.manager');
96
97
        if (method_exists($source, 'setShared')) { // Symfony 2.8+
98
            $source->setShared(false);
99
        } else {
100
            // For Symfony <2.8 compatibility
101
            $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...
102
        }
103
104
        foreach ($config['doctrine_orm'] as $pos => $sitemap) {
105
            // define the connectionIterator
106
            $connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos;
107
108
            $connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', array(
109
                new Reference($sitemap['connection']),
110
                $sitemap['query'],
111
            ));
112
113
            $connectionIterator->setPublic(false);
114
            $container->setDefinition($connectionIteratorId, $connectionIterator);
115
116
            // define the sitemap proxy iterator
117
            $sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos;
118
119
            $sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', array(
120
                new Reference($connectionIteratorId),
121
                new Reference('router'),
122
                $sitemap['route'],
123
                $sitemap['parameters'],
124
            ));
125
126
            $sitemapIterator->setPublic(false);
127
128
            $container->setDefinition($sitemapIteratorId, $sitemapIterator);
129
130
            $source->addMethodCall('addSource', array($sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']));
131
        }
132
133
        foreach ($config['services'] as $service) {
134
            $source->addMethodCall('addSource', array($service['group'], new Reference($service['id']), $service['types']));
135
        }
136
    }
137
138
    /**
139
     * Fix the sitemap configuration.
140
     *
141
     * @param array $config
142
     *
143
     * @return array
144
     */
145
    protected function fixConfiguration(array $config)
146
    {
147
        foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) {
148
            $sitemap['group'] = isset($sitemap['group']) ? $sitemap['group'] : false;
149
            $sitemap['types'] = isset($sitemap['types']) ? $sitemap['types'] : array();
150
            $sitemap['connection'] = isset($sitemap['connection']) ? $sitemap['connection'] : 'doctrine.dbal.default_connection';
151
            $sitemap['route'] = isset($sitemap['route']) ? $sitemap['route'] : false;
152
            $sitemap['parameters'] = isset($sitemap['parameters']) ? $sitemap['parameters'] : false;
153
            $sitemap['query'] = isset($sitemap['query']) ? $sitemap['query'] : false;
154
155
            if ($sitemap['route'] === false) {
156
                throw new \RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration');
157
            }
158
159
            if ($sitemap['query'] === false) {
160
                throw new \RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration');
161
            }
162
163
            if ($sitemap['parameters'] === false) {
164
                throw new \RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration');
165
            }
166
167
            $config['sitemap']['doctrine_orm'][$pos] = $sitemap;
168
        }
169
170
        foreach ($config['sitemap']['services'] as $pos => $sitemap) {
171
            if (!is_array($sitemap)) {
172
                $sitemap = array(
173
                    'group' => false,
174
                    'types' => array(),
175
                    'id' => $sitemap,
176
                );
177
            } else {
178
                $sitemap['group'] = isset($sitemap['group']) ? $sitemap['group'] : false;
179
                $sitemap['types'] = isset($sitemap['types']) ? $sitemap['types'] : array();
180
181
                if (!isset($sitemap['id'])) {
182
                    throw new \RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration');
183
                }
184
            }
185
186
            $config['sitemap']['services'][$pos] = $sitemap;
187
        }
188
189
        return $config;
190
    }
191
192
    /**
193
     * @param ContainerBuilder $container
194
     * @param array            $config
195
     */
196
    private function configureHttpClient(ContainerBuilder $container, array $config)
197
    {
198
        $container->setAlias('sonata.seo.http.client', $config['client']);
199
        $container->setAlias('sonata.seo.http.message_factory', $config['message_factory']);
200
    }
201
}
202