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 RuntimeException; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
20
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
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
|
|
|
final class SonataSeoExtension extends Extension |
28
|
|
|
{ |
29
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
30
|
|
|
{ |
31
|
|
|
$configuration = new Configuration(); |
32
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
33
|
|
|
$config = $this->fixConfiguration($config); |
34
|
|
|
|
35
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
36
|
|
|
|
37
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
38
|
|
|
|
39
|
|
|
if (isset($bundles['SonataBlockBundle'], $bundles['KnpMenuBundle'])) { |
40
|
|
|
$loader->load('blocks.xml'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$loader->load('event.xml'); |
44
|
|
|
$loader->load('services.xml'); |
45
|
|
|
$loader->load('commands.xml'); |
46
|
|
|
|
47
|
|
|
$this->configureSeoPage($config['page'], $container); |
48
|
|
|
$this->configureSitemap($config['sitemap'], $container); |
49
|
|
|
$this->configureHttpClient($container, $config['http']); |
50
|
|
|
|
51
|
|
|
$container->getDefinition('sonata.seo.twig.extension') |
52
|
|
|
->replaceArgument(1, $config['encoding']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Configure the default seo page. |
57
|
|
|
*/ |
58
|
|
|
protected function configureSeoPage(array $config, ContainerBuilder $container): void |
59
|
|
|
{ |
60
|
|
|
$container->setParameter('sonata.seo.config', $config); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Configure the sitemap source manager. |
65
|
|
|
*/ |
66
|
|
|
protected function configureSitemap(array $config, ContainerBuilder $container): void |
67
|
|
|
{ |
68
|
|
|
$source = $container->getDefinition('sonata.seo.sitemap.manager'); |
69
|
|
|
|
70
|
|
|
$source->setShared(false); |
71
|
|
|
|
72
|
|
|
foreach ($config['doctrine_orm'] as $pos => $sitemap) { |
73
|
|
|
// define the connectionIterator |
74
|
|
|
$connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos; |
75
|
|
|
|
76
|
|
|
$connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', [ |
77
|
|
|
new Reference($sitemap['connection']), |
78
|
|
|
$sitemap['query'], |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$connectionIterator->setPublic(false); |
82
|
|
|
$container->setDefinition($connectionIteratorId, $connectionIterator); |
83
|
|
|
|
84
|
|
|
// define the sitemap proxy iterator |
85
|
|
|
$sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos; |
86
|
|
|
|
87
|
|
|
$sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', [ |
88
|
|
|
new Reference($connectionIteratorId), |
89
|
|
|
new Reference('router'), |
90
|
|
|
$sitemap['route'], |
91
|
|
|
$sitemap['parameters'], |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$sitemapIterator->setPublic(false); |
95
|
|
|
|
96
|
|
|
$container->setDefinition($sitemapIteratorId, $sitemapIterator); |
97
|
|
|
|
98
|
|
|
$source->addMethodCall('addSource', [$sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($config['services'] as $service) { |
102
|
|
|
$source->addMethodCall('addSource', [$service['group'], new Reference($service['id']), $service['types']]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Fix the sitemap configuration. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function fixConfiguration(array $config) |
112
|
|
|
{ |
113
|
|
|
foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) { |
114
|
|
|
$sitemap['group'] = $sitemap['group'] ?? false; |
115
|
|
|
$sitemap['types'] = $sitemap['types'] ?? []; |
116
|
|
|
$sitemap['connection'] = $sitemap['connection'] ?? 'doctrine.dbal.default_connection'; |
117
|
|
|
$sitemap['route'] = $sitemap['route'] ?? false; |
118
|
|
|
$sitemap['parameters'] = $sitemap['parameters'] ?? false; |
119
|
|
|
$sitemap['query'] = $sitemap['query'] ?? false; |
120
|
|
|
|
121
|
|
|
if (false === $sitemap['route']) { |
122
|
|
|
throw new RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (false === $sitemap['query']) { |
126
|
|
|
throw new RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (false === $sitemap['parameters']) { |
130
|
|
|
throw new RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$config['sitemap']['doctrine_orm'][$pos] = $sitemap; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($config['sitemap']['services'] as $pos => $sitemap) { |
137
|
|
|
if (!\is_array($sitemap)) { |
138
|
|
|
$sitemap = [ |
139
|
|
|
'group' => false, |
140
|
|
|
'types' => [], |
141
|
|
|
'id' => $sitemap, |
142
|
|
|
]; |
143
|
|
|
} else { |
144
|
|
|
$sitemap['group'] = $sitemap['group'] ?? false; |
145
|
|
|
$sitemap['types'] = $sitemap['types'] ?? []; |
146
|
|
|
|
147
|
|
|
if (!isset($sitemap['id'])) { |
148
|
|
|
throw new RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$config['sitemap']['services'][$pos] = $sitemap; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $config; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function configureHttpClient(ContainerBuilder $container, array $config): void |
159
|
|
|
{ |
160
|
|
|
if (null === $config['client'] || null === $config['message_factory']) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$container->setAlias('sonata.seo.http.client', $config['client']); |
165
|
|
|
$container->setAlias('sonata.seo.http.message_factory', $config['message_factory']); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|