1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Lichtenwallner (https://lichtenwallner.at) |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/jolicht/markdown-cms for the canonical source repository |
6
|
|
|
* @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT |
7
|
|
|
* @copyright Copyright (c) Johannes Lichtenwallner |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types = 1); |
10
|
|
|
namespace Jolicht\MarkdownCms\Parser\Observer; |
11
|
|
|
|
12
|
|
|
use Sabre\Xml\Writer as XmlWriter; |
13
|
|
|
|
14
|
|
|
class SitemapObserver implements ParsedContentObserverInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Config writer |
18
|
|
|
* |
19
|
|
|
* @var XmlWriter |
20
|
|
|
*/ |
21
|
|
|
private $xmlWriter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Sitemap path |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $sitemapPath; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Base url |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $baseUrl; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
* |
40
|
|
|
* @param XmlWriter $xmlWriter |
41
|
|
|
* @param string $sitemapPath |
42
|
|
|
* @param string $baseUrl |
43
|
|
|
*/ |
44
|
|
|
public function __construct(XmlWriter $xmlWriter, string $sitemapPath, string $baseUrl) |
45
|
|
|
{ |
46
|
|
|
$this->xmlWriter = $xmlWriter; |
47
|
|
|
$this->sitemapPath = $sitemapPath; |
48
|
|
|
$this->baseUrl = rtrim($baseUrl, '/'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Handle parsed config |
53
|
|
|
* @param array $config |
54
|
|
|
*/ |
55
|
|
|
public function __invoke(array $config) |
56
|
|
|
{ |
57
|
|
|
$namespace = 'http://www.sitemaps.org/schemas/sitemap/0.9'; |
58
|
|
|
|
59
|
|
|
$baseUrl = $this->getBaseUrl(); |
60
|
|
|
|
61
|
|
|
$writer = $this->getXmlWriter(); |
62
|
|
|
|
63
|
|
|
$writer->openMemory(); |
64
|
|
|
$writer->startDocument('1.0', 'UTF-8'); |
65
|
|
|
$writer->namespaceMap[$namespace] = ''; |
66
|
|
|
$writer->setIndent(true); |
67
|
|
|
$writer->startElement('{' . $namespace . '}' . 'urlset'); |
68
|
|
View Code Duplication |
if (isset($config['content_types']['blogEntry'])) { |
|
|
|
|
69
|
|
|
foreach ($config['content_types']['blogEntry']['all'] as $id) { |
70
|
|
|
$writer->startElement('url'); |
71
|
|
|
$writer->writeElement('loc', $baseUrl . '/blog.' . $id . '.html'); |
72
|
|
|
$writer->endElement(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
View Code Duplication |
if (isset($config['content_types']['page'])) { |
|
|
|
|
76
|
|
|
foreach ($config['content_types']['page']['all'] as $id) { |
77
|
|
|
$writer->startElement('url'); |
78
|
|
|
$writer->writeElement('loc', $baseUrl . '/' . $id . '.html'); |
79
|
|
|
$writer->endElement(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
$writer->endElement(); |
83
|
|
|
$content = $writer->outputMemory(); |
84
|
|
|
|
85
|
|
|
file_put_contents($this->getSitemapPath(), $content); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get xml config writer |
90
|
|
|
* |
91
|
|
|
* @return XmlXmlWriter |
92
|
|
|
*/ |
93
|
|
|
public function getXmlWriter() : XmlWriter |
94
|
|
|
{ |
95
|
|
|
return $this->xmlWriter; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get sitemap path |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getSitemapPath() : string |
104
|
|
|
{ |
105
|
|
|
return $this->sitemapPath; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get base url |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getBaseUrl() : string |
114
|
|
|
{ |
115
|
|
|
return $this->baseUrl; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.