|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Sitemap; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\Sitemap\Tags\Tag; |
|
6
|
|
|
use Spatie\Sitemap\Tags\Url; |
|
7
|
|
|
|
|
8
|
|
|
class Sitemap |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $tags = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $formatDocument = false; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return self |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function create(): self |
|
24
|
|
|
{ |
|
25
|
|
|
return new static(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string|\Spatie\Sitemap\Tags\Tag $tag |
|
30
|
|
|
* |
|
31
|
|
|
* @return self |
|
32
|
|
|
*/ |
|
33
|
|
|
public function add($tag): self |
|
34
|
|
|
{ |
|
35
|
|
|
if (is_string($tag)) { |
|
36
|
|
|
$tag = Url::create($tag); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!in_array($tag, $this->tags)) { |
|
40
|
|
|
$this->tags[] = $tag; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getTags(): array |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->tags; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $url |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getUrl(string $url): ?Url |
|
60
|
|
|
{ |
|
61
|
|
|
return collect($this->tags)->first(function (Tag $tag) use ($url) { |
|
62
|
|
|
return $tag->getType() === 'url' && $tag->url === $url; |
|
|
|
|
|
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $url |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function hasUrl(string $url): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return (bool)$this->getUrl($url); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
* @throws \Throwable |
|
79
|
|
|
*/ |
|
80
|
|
|
public function render(): string |
|
81
|
|
|
{ |
|
82
|
|
|
sort($this->tags); |
|
83
|
|
|
|
|
84
|
|
|
$tags = $this->tags; |
|
85
|
|
|
|
|
86
|
|
|
$header = view('laravel-sitemap::header')->render(); |
|
|
|
|
|
|
87
|
|
|
$document = view('laravel-sitemap::sitemap') |
|
|
|
|
|
|
88
|
|
|
->with(compact('tags')) |
|
89
|
|
|
->render(); |
|
90
|
|
|
|
|
91
|
|
|
if ($this->formatDocument) { |
|
92
|
|
|
$document = self::formatDocument($document); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $header . $document; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $path |
|
100
|
|
|
* |
|
101
|
|
|
* @return self |
|
102
|
|
|
* @throws \Throwable |
|
103
|
|
|
*/ |
|
104
|
|
|
public function writeToFile(string $path): self |
|
105
|
|
|
{ |
|
106
|
|
|
file_put_contents($path, $this->render()); |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param bool $formatDocument |
|
113
|
|
|
* |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setFormatDocument(bool $formatDocument = true) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->formatDocument = $formatDocument; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $xml |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected static function formatDocument(string $xml): string |
|
129
|
|
|
{ |
|
130
|
|
|
if (!extension_loaded('dom')) { |
|
131
|
|
|
return $xml; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$dom = new \DOMDocument(); |
|
135
|
|
|
$dom->preserveWhiteSpace = false; |
|
136
|
|
|
$dom->formatOutput = true; |
|
137
|
|
|
|
|
138
|
|
|
$dom->loadXML($xml); |
|
139
|
|
|
|
|
140
|
|
|
return $dom->saveXML($dom->documentElement) . PHP_EOL; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.