1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Sitemap; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Responsable; |
6
|
|
|
use Illuminate\Support\Facades\Response; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Spatie\Sitemap\Tags\Tag; |
9
|
|
|
use Spatie\Sitemap\Tags\Url; |
10
|
|
|
|
11
|
|
|
class Sitemap implements Responsable |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
protected $tags = []; |
15
|
|
|
|
16
|
|
|
public static function create(): self |
17
|
|
|
{ |
18
|
|
|
return new static(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string|\Spatie\Sitemap\Tags\Tag $tag |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function add($tag): self |
27
|
|
|
{ |
28
|
|
|
if (is_string($tag)) { |
29
|
|
|
$tag = Url::create($tag); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (! in_array($tag, $this->tags)) { |
33
|
|
|
$this->tags[] = $tag; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getTags(): array |
40
|
|
|
{ |
41
|
|
|
return $this->tags; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getUrl(string $url): ?Url |
45
|
|
|
{ |
46
|
|
|
return collect($this->tags)->first(function (Tag $tag) use ($url) { |
47
|
|
|
return $tag->getType() === 'url' && $tag->url === $url; |
|
|
|
|
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function hasUrl(string $url): bool |
52
|
|
|
{ |
53
|
|
|
return (bool) $this->getUrl($url); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function render(): string |
57
|
|
|
{ |
58
|
|
|
sort($this->tags); |
59
|
|
|
|
60
|
|
|
$tags = collect($this->tags)->unique('url'); |
61
|
|
|
|
62
|
|
|
return view('laravel-sitemap::sitemap') |
63
|
|
|
->with(compact('tags')) |
64
|
|
|
->render(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function writeToFile(string $path): self |
68
|
|
|
{ |
69
|
|
|
file_put_contents($path, $this->render()); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function writeToDisk(string $disk, string $path): self |
75
|
|
|
{ |
76
|
|
|
Storage::disk($disk)->put($path, $this->render()); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create an HTTP response that represents the object. |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Http\Request $request |
85
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
86
|
|
|
*/ |
87
|
|
|
public function toResponse($request) |
88
|
|
|
{ |
89
|
|
|
return Response::make($this->render(), 200, [ |
90
|
|
|
'Content-Type' => 'text/xml', |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.