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
|
|
|
public function get(SitemapDefinitionInterface $definition): ?SitemapServiceInterface |
38
|
|
|
{ |
39
|
|
|
$sitemap = $this->getService($definition->getType()); |
40
|
|
|
|
41
|
|
|
$optionsResolver = new OptionsResolver(); |
42
|
|
|
$this->configureSettings($optionsResolver, $sitemap); |
43
|
|
|
|
44
|
|
|
$settings = $optionsResolver->resolve($definition->getSettings()); |
45
|
|
|
$definition->setSettings($settings); |
46
|
|
|
|
47
|
|
|
return $sitemap; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function addSitemap(string $id, SitemapServiceInterface $service): void |
51
|
|
|
{ |
52
|
|
|
$this->services[$id] = $service; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function has(string $id): bool |
56
|
|
|
{ |
57
|
|
|
return isset($this->services[$id]) ? true : false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getService(string $id): SitemapServiceInterface |
61
|
|
|
{ |
62
|
|
|
if (!$this->has($id)) { |
63
|
|
|
throw new SitemapNotFoundException(sprintf('The sitemap service "%s" does not exist', $id)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->services[$id]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function configureSettings(OptionsResolver $resolver, SitemapServiceInterface $sitemap): void |
70
|
|
|
{ |
71
|
|
|
$resolver |
72
|
|
|
->setDefaults([ |
73
|
|
|
'use_cache' => true, |
74
|
|
|
'extra_cache_keys' => [], |
75
|
|
|
'ttl' => 86400, |
76
|
|
|
]) |
77
|
|
|
->setAllowedTypes('use_cache', 'bool') |
78
|
|
|
->setAllowedTypes('extra_cache_keys', 'array') |
79
|
|
|
->setAllowedTypes('ttl', 'int') |
80
|
|
|
; |
81
|
|
|
|
82
|
|
|
$sitemap->configureSettings($resolver); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|