1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenDesloovere\SitemapBundle\Generator; |
4
|
|
|
|
5
|
|
|
use JeroenDesloovere\SitemapBundle\Exception\SitemapException; |
6
|
|
|
use JeroenDesloovere\SitemapBundle\Item\SitemapItem; |
7
|
|
|
use JeroenDesloovere\SitemapBundle\Provider\SitemapProviderInterface; |
8
|
|
|
use JeroenDesloovere\SitemapBundle\Provider\SitemapProviders; |
9
|
|
|
use Symfony\Component\Routing\Generator\UrlGenerator; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
11
|
|
|
|
12
|
|
|
class SitemapGenerator |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
private $url; |
16
|
|
|
|
17
|
|
|
/** @var string - The path where we must save all the sitemaps.*/ |
18
|
|
|
private $path; |
19
|
|
|
|
20
|
|
|
/** @var SitemapProviders */ |
21
|
|
|
private $providers; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Router $router |
25
|
|
|
* @param string $path |
26
|
|
|
* @param SitemapProviders $providers |
27
|
|
|
* @throws \Exception |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Router $router, string $path, SitemapProviders $providers) |
30
|
|
|
{ |
31
|
|
|
$this->setUrl($router->getContext()->getScheme() . '://' . $router->getContext()->getHost()); |
32
|
|
|
$this->providers = $providers; |
33
|
|
|
$this->setPath($path); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function generate(): void |
37
|
|
|
{ |
38
|
|
|
$providers = $this->providers->getAll(); |
39
|
|
|
|
40
|
|
|
if (empty($providers)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($providers as $provider) { |
45
|
|
|
$this->saveSitemapForProvider($provider); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->saveSitemapIndex(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function generateSitemapForProvider(SitemapProviderInterface $provider): string |
52
|
|
|
{ |
53
|
|
|
$provider->createItems(); |
54
|
|
|
$rootNode = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'); |
55
|
|
|
|
56
|
|
|
/** @var SitemapItem $item */ |
57
|
|
|
foreach ($provider->getItems()->getAll() as $item) { |
58
|
|
|
$itemNode = $rootNode->addChild('url'); |
59
|
|
|
$itemNode->addChild('loc', $this->getUrl() . $item->getUrl()); |
60
|
|
|
$itemNode->addChild('changefreq', $item->getChangeFrequency()->__toString()); |
61
|
|
|
$itemNode->addChild('lastmod', $item->getLastModifiedOn()->format('Y-m-d')); |
62
|
|
|
$itemNode->addChild('priority', $item->getPriority()/10); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $rootNode->asXML(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function generateSitemapIndex(): string |
69
|
|
|
{ |
70
|
|
|
$rootNode = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>'); |
71
|
|
|
|
72
|
|
|
/** @var string $key */ |
73
|
|
|
foreach ($this->providers->getKeys() as $key) { |
74
|
|
|
$itemNode = $rootNode->addChild('sitemap'); |
75
|
|
|
$itemNode->addChild('loc', $this->getUrl() . '/sitemap_' . $key . '.xml'); |
76
|
|
|
// @todo: lastmod |
77
|
|
|
$itemNode->addChild('lastmod', (new \DateTime())->format('Y-m-d')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $rootNode->asXML(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPath(): string |
84
|
|
|
{ |
85
|
|
|
return $this->path; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getUrl(): string |
89
|
|
|
{ |
90
|
|
|
return $this->url; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function regenerateForSitemapProvider(SitemapProviderInterface $provider): void |
94
|
|
|
{ |
95
|
|
|
$this->saveSitemapForProvider($provider); |
96
|
|
|
$this->saveSitemapIndex(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function saveSitemapForProvider(SitemapProviderInterface $provider): void |
100
|
|
|
{ |
101
|
|
|
file_put_contents( |
102
|
|
|
$this->getPath() . '/sitemap_' . $provider->getKey() . '.xml', |
103
|
|
|
$this->generateSitemapForProvider($provider) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function saveSitemapIndex(): void |
108
|
|
|
{ |
109
|
|
|
file_put_contents( |
110
|
|
|
$this->getPath() . '/sitemap.xml', |
111
|
|
|
$this->generateSitemapIndex() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the path where whe must save all the sitemaps. |
117
|
|
|
* |
118
|
|
|
* @param string $path |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
public function setPath(string $path): void |
122
|
|
|
{ |
123
|
|
|
if ($path === '') { |
124
|
|
|
throw SitemapException::forEmptyPath(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->path = $path; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the url |
132
|
|
|
* |
133
|
|
|
* @param string $url |
134
|
|
|
* @throws \Exception |
135
|
|
|
*/ |
136
|
|
|
public function setUrl(string $url): void |
137
|
|
|
{ |
138
|
|
|
if ($url === '') { |
139
|
|
|
throw SitemapException::forEmptyUrl(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->url = $url; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|