|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[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 Core23\SitemapBundle\Sitemap; |
|
13
|
|
|
|
|
14
|
|
|
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface; |
|
15
|
|
|
use Core23\SitemapBundle\Exception\SitemapNotFoundException; |
|
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
17
|
|
|
|
|
18
|
|
|
final class SitemapServiceManager implements SitemapServiceManagerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var SitemapServiceInterface[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $services; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param SitemapServiceInterface[] $services |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(array $services = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->services = []; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($services as $id => $service) { |
|
33
|
|
|
$this->addSitemap($id, $service); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function get(SitemapDefinitionInterface $definition): ?SitemapServiceInterface |
|
41
|
|
|
{ |
|
42
|
|
|
$service = $this->getService($definition->getType()); |
|
43
|
|
|
|
|
44
|
|
|
$optionsResolver = new OptionsResolver(); |
|
45
|
|
|
$this->configureSettings($optionsResolver, $service); |
|
46
|
|
|
|
|
47
|
|
|
$settings = $optionsResolver->resolve($definition->getSettings()); |
|
48
|
|
|
$definition->setSettings($settings); |
|
49
|
|
|
|
|
50
|
|
|
return $service; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function addSitemap(string $id, SitemapServiceInterface $service): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->services[$id] = $service; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $id |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
private function has(string $id): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return isset($this->services[$id]) ? true : false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $id |
|
73
|
|
|
* |
|
74
|
|
|
* @return SitemapServiceInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getService(string $id): SitemapServiceInterface |
|
77
|
|
|
{ |
|
78
|
|
|
if (!$this->has($id)) { |
|
79
|
|
|
throw new SitemapNotFoundException(sprintf('The sitemap service `%s` does not exist', $id)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->services[$id]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param OptionsResolver $resolver |
|
87
|
|
|
* @param SitemapServiceInterface $sitemap |
|
88
|
|
|
*/ |
|
89
|
|
|
private function configureSettings(OptionsResolver $resolver, SitemapServiceInterface $sitemap): void |
|
90
|
|
|
{ |
|
91
|
|
|
$resolver |
|
92
|
|
|
->setDefaults([ |
|
93
|
|
|
'use_cache' => true, |
|
94
|
|
|
'extra_cache_keys' => [], |
|
95
|
|
|
'ttl' => 86400, |
|
96
|
|
|
]) |
|
97
|
|
|
->setAllowedTypes('use_cache', 'bool') |
|
98
|
|
|
->setAllowedTypes('extra_cache_keys', 'array') |
|
99
|
|
|
->setAllowedTypes('ttl', 'int') |
|
100
|
|
|
; |
|
101
|
|
|
|
|
102
|
|
|
$sitemap->configureSettings($resolver); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|