|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Hyde\Framework\Features\XmlGenerators; |
|
8
|
|
|
|
|
9
|
|
|
use Hyde\Hyde; |
|
10
|
|
|
use SimpleXMLElement; |
|
11
|
|
|
use Hyde\Pages\HtmlPage; |
|
12
|
|
|
use Hyde\Pages\BladePage; |
|
13
|
|
|
use Hyde\Pages\MarkdownPage; |
|
14
|
|
|
use Hyde\Pages\MarkdownPost; |
|
15
|
|
|
use Hyde\Facades\Filesystem; |
|
16
|
|
|
use Hyde\Pages\InMemoryPage; |
|
17
|
|
|
use Hyde\Support\Models\Route; |
|
18
|
|
|
use Illuminate\Support\Carbon; |
|
19
|
|
|
use Hyde\Pages\DocumentationPage; |
|
20
|
|
|
use Hyde\Foundation\Facades\Routes; |
|
21
|
|
|
|
|
22
|
|
|
use function in_array; |
|
23
|
|
|
use function date; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @see https://www.sitemaps.org/protocol.html |
|
27
|
|
|
*/ |
|
28
|
|
|
class SitemapGenerator extends BaseXmlGenerator |
|
29
|
|
|
{ |
|
30
|
|
|
public function generate(): static |
|
31
|
|
|
{ |
|
32
|
|
|
Routes::all()->each(function (Route $route): void { |
|
33
|
|
|
$this->addRoute($route); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function constructBaseElement(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'); |
|
42
|
|
|
$this->xmlElement->addAttribute('generator', 'HydePHP v'.Hyde::version()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function addRoute(Route $route): void |
|
46
|
|
|
{ |
|
47
|
|
|
$urlItem = $this->xmlElement->addChild('url'); |
|
48
|
|
|
|
|
49
|
|
|
$this->addChild($urlItem, 'loc', $this->resolveRouteLink($route)); |
|
50
|
|
|
$this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath())); |
|
51
|
|
|
$this->addChild($urlItem, 'changefreq', $this->generateChangeFrequency(...$this->getRouteInformation($route))); |
|
52
|
|
|
$this->addChild($urlItem, 'priority', $this->generatePriority(...$this->getRouteInformation($route))); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function resolveRouteLink(Route $route): string |
|
56
|
|
|
{ |
|
57
|
|
|
return Hyde::url($route->getOutputPath()); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getLastModDate(string $file): string |
|
61
|
|
|
{ |
|
62
|
|
|
return date('c', @Filesystem::lastModified($file) ?: Carbon::now()->timestamp); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
|
|
|
|
|
67
|
|
|
* @return numeric-string |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
protected function generatePriority(string $pageClass, string $identifier): string |
|
70
|
|
|
{ |
|
71
|
|
|
$priority = 0.5; |
|
72
|
|
|
|
|
73
|
|
|
if (in_array($pageClass, [BladePage::class, MarkdownPage::class, DocumentationPage::class])) { |
|
74
|
|
|
$priority = 0.9; |
|
75
|
|
|
|
|
76
|
|
|
if ($identifier === 'index') { |
|
77
|
|
|
$priority = 1; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (in_array($pageClass, [MarkdownPost::class, InMemoryPage::class, HtmlPage::class])) { |
|
82
|
|
|
$priority = 0.75; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($identifier === '404') { |
|
86
|
|
|
$priority = 0.25; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return (string) $priority; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
|
|
|
|
|
94
|
|
|
* @return 'always'|'hourly'|'daily '|'weekly'|'monthly'|'yearly'|'never' |
|
|
|
|
|
|
95
|
|
|
*/ |
|
96
|
|
|
protected function generateChangeFrequency(string $pageClass, string $identifier): string |
|
97
|
|
|
{ |
|
98
|
|
|
$frequency = 'weekly'; |
|
99
|
|
|
|
|
100
|
|
|
if (in_array($pageClass, [BladePage::class, MarkdownPage::class, DocumentationPage::class])) { |
|
101
|
|
|
$frequency = 'daily'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($identifier === '404') { |
|
105
|
|
|
$frequency = 'monthly'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $frequency; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** @return array{class-string<\Hyde\Pages\Concerns\HydePage>, string} */ |
|
|
|
|
|
|
112
|
|
|
protected function getRouteInformation(Route $route): array |
|
113
|
|
|
{ |
|
114
|
|
|
return [$route->getPageClass(), $route->getPage()->getIdentifier()]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|