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
|
|
|
/** @var array */ |
11
|
|
|
protected $tags = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return static |
15
|
|
|
*/ |
16
|
|
|
public static function create() |
17
|
|
|
{ |
18
|
|
|
return new static(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function createFromFile(string $path) |
22
|
|
|
{ |
23
|
|
|
$sitemap = new static(); |
24
|
|
|
|
25
|
|
|
if (! file_exists($path)) { |
26
|
|
|
return $sitemap; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$tags = json_decode(json_encode(simplexml_load_string(file_get_contents($path))), true); |
30
|
|
|
|
31
|
|
|
collect($tags)->each(function($tag) use ($sitemap) { |
32
|
|
|
$url = new Url($tag['loc']); |
33
|
|
|
$url->setPriority($tag['priority']); |
34
|
|
|
$url->setChangeFrequency($tag['changefreq']); |
35
|
|
|
$url->setLastModificationDate(new \DateTime($tag['lastmod'])); |
36
|
|
|
|
37
|
|
|
$sitemap->add($url); |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
return $sitemap; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string|\Spatie\Sitemap\Tags\Tag $tag |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function add($tag) |
49
|
|
|
{ |
50
|
|
|
if (is_string($tag)) { |
51
|
|
|
$tag = Url::create($tag); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (! in_array($tag, $this->tags)) { |
55
|
|
|
$this->tags[] = $tag; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $url |
63
|
|
|
* |
64
|
|
|
* @return \Spatie\Sitemap\Tags\Url|null |
65
|
|
|
*/ |
66
|
|
|
public function getUrl(string $url) |
67
|
|
|
{ |
68
|
|
|
return collect($this->tags)->first(function (Tag $tag) use ($url) { |
69
|
|
|
return $tag->getType() === 'url' && $tag->url === $url; |
|
|
|
|
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function hasUrl(string $url): bool |
74
|
|
|
{ |
75
|
|
|
return (bool) $this->getUrl($url); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function render(): string |
79
|
|
|
{ |
80
|
|
|
sort($this->tags); |
81
|
|
|
|
82
|
|
|
$tags = $this->tags; |
83
|
|
|
|
84
|
|
|
return view('laravel-sitemap::sitemap') |
|
|
|
|
85
|
|
|
->with(compact('tags')) |
86
|
|
|
->render(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $path |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function writeToFile(string $path) |
95
|
|
|
{ |
96
|
|
|
file_put_contents($path, $this->render()); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.