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
|
|
|
|
11
|
|
|
class SitemapGenerator |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
private $url; |
15
|
|
|
|
16
|
|
|
/** @var string - The path where we must save all the sitemaps.*/ |
17
|
|
|
private $path; |
18
|
|
|
|
19
|
|
|
/** @var SitemapProviders */ |
20
|
|
|
private $providers; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $path |
24
|
|
|
* @param SitemapProviders $providers |
25
|
|
|
* @throws \Exception |
26
|
|
|
*/ |
27
|
|
|
public function __construct(UrlGenerator $urlGenerator, string $path, SitemapProviders $providers) |
28
|
|
|
{ |
29
|
|
|
$this->url = $urlGenerator->generate('', [], UrlGenerator::ABSOLUTE_URL); |
30
|
|
|
$this->providers = $providers; |
31
|
|
|
$this->setPath($path); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function generate(): void |
35
|
|
|
{ |
36
|
|
|
foreach ($this->providers->getAll() as $provider) { |
37
|
|
|
$this->saveSitemapForProvider($provider); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->saveSitemapIndex(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function generateSitemapForProvider(SitemapProviderInterface $provider): string |
44
|
|
|
{ |
45
|
|
|
$provider->createItems(); |
46
|
|
|
$rootNode = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'); |
47
|
|
|
|
48
|
|
|
/** @var SitemapItem $item */ |
49
|
|
|
foreach ($provider->getItems()->getAll() as $item) { |
|
|
|
|
50
|
|
|
$itemNode = $rootNode->addChild('url'); |
51
|
|
|
$itemNode->addChild('loc', $this->url . $item->getUrl()); |
52
|
|
|
$itemNode->addChild('changefreq', $item->getChangeFrequency()->__toString()); |
53
|
|
|
$itemNode->addChild('lastmod', $item->getLastModifiedOn()->format('Y-m-d')); |
54
|
|
|
$itemNode->addChild('priority', $item->getPriority()/10); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $rootNode->asXML(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function generateSitemapIndex(): string |
61
|
|
|
{ |
62
|
|
|
$rootNode = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>'); |
63
|
|
|
|
64
|
|
|
/** @var string $key */ |
65
|
|
|
foreach ($this->providers->getKeys() as $key) { |
66
|
|
|
$itemNode = $rootNode->addChild('sitemap'); |
67
|
|
|
$itemNode->addChild('loc', $this->url . '/sitemap_' . $key . '.xml'); |
68
|
|
|
// @todo: lastmod |
69
|
|
|
$itemNode->addChild('lastmod', (new \DateTime())->format('Y-m-d')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $rootNode->asXML(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getPath(): string |
76
|
|
|
{ |
77
|
|
|
return $this->path; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function regenerateForSitemapProvider(SitemapProviderInterface $provider): void |
81
|
|
|
{ |
82
|
|
|
$this->saveSitemapForProvider($provider); |
83
|
|
|
$this->saveSitemapIndex(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function saveSitemapForProvider(SitemapProviderInterface $provider): void |
87
|
|
|
{ |
88
|
|
|
file_put_contents( |
89
|
|
|
$this->getPath() . '/sitemap_' . $provider->getKey() . '.xml', |
90
|
|
|
$this->generateSitemapForProvider($provider) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function saveSitemapIndex(): void |
95
|
|
|
{ |
96
|
|
|
file_put_contents( |
97
|
|
|
$this->getPath() . '/sitemap.xml', |
98
|
|
|
$this->generateSitemapIndex() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the path where whe must save all the sitemaps. |
104
|
|
|
* |
105
|
|
|
* @param string $path |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
|
|
public function setPath(string $path): void |
109
|
|
|
{ |
110
|
|
|
if ($path === '') { |
111
|
|
|
throw SitemapException::forEmptyPath(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->path = $path; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.