1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
4
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Services; |
6
|
|
|
|
7
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
8
|
|
|
use Hyde\Framework\Hyde; |
9
|
|
|
use Hyde\Framework\Models\Pages\BladePage; |
10
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
11
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPage; |
12
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
13
|
|
|
use Hyde\Framework\Models\Route; |
14
|
|
|
use SimpleXMLElement; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see \Hyde\Framework\Testing\Feature\Services\SitemapServiceTest |
18
|
|
|
* @see https://www.sitemaps.org/protocol.html |
19
|
|
|
* @phpstan-consistent-constructor |
20
|
|
|
*/ |
21
|
|
|
class SitemapService |
22
|
|
|
{ |
23
|
|
|
public SimpleXMLElement $xmlElement; |
24
|
|
|
protected float $time_start; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
if (! extension_loaded('simplexml') || config('testing.mock_disabled_extensions', false) === true) { |
29
|
|
|
throw new \Exception('The ext-simplexml extension is not installed, but is required to generate RSS feeds.'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->time_start = microtime(true); |
33
|
|
|
|
34
|
|
|
$this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'); |
35
|
|
|
$this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function generate(): static |
39
|
|
|
{ |
40
|
|
|
Route::all()->each(function ($route) { |
41
|
|
|
$this->addRoute($route); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getXML(): string|false |
48
|
|
|
{ |
49
|
|
|
$this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2)); |
50
|
|
|
|
51
|
|
|
return $this->xmlElement->asXML(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function addRoute(RouteContract $route): void |
55
|
|
|
{ |
56
|
|
|
$urlItem = $this->xmlElement->addChild('url'); |
57
|
|
|
$urlItem->addChild('loc', htmlentities($route->getQualifiedUrl())); |
58
|
|
|
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate($route->getSourceFilePath()))); |
59
|
|
|
$urlItem->addChild('changefreq', 'daily'); |
60
|
|
|
if (config('hyde.sitemap.dynamic_priority', true)) { |
61
|
|
|
$urlItem->addChild('priority', $this->getPriority($route->getPageType(), $route->getSourceModel()->slug)); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getLastModDate(string $file): string |
66
|
|
|
{ |
67
|
|
|
return date('c', filemtime( |
68
|
|
|
$file |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getPriority(string $pageClass, string $slug): string |
73
|
|
|
{ |
74
|
|
|
$priority = 0.5; |
75
|
|
|
|
76
|
|
|
if (in_array($pageClass, [BladePage::class, MarkdownPage::class])) { |
77
|
|
|
$priority = 0.9; |
78
|
|
|
if ($slug === 'index') { |
79
|
|
|
$priority = 1; |
80
|
|
|
} |
81
|
|
|
if ($slug === '404') { |
82
|
|
|
$priority = 0.5; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($pageClass === DocumentationPage::class) { |
87
|
|
|
$priority = 0.9; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($pageClass === MarkdownPost::class) { |
91
|
|
|
$priority = 0.75; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return (string) $priority; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function generateSitemap(): string |
98
|
|
|
{ |
99
|
|
|
return (new static)->generate()->getXML(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|