1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mfonte\Sitemap; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Renderable; |
6
|
|
|
use Illuminate\Contracts\Support\Responsable; |
7
|
|
|
use Illuminate\Support\Facades\Response; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Mfonte\Sitemap\Contracts\Sitemapable; |
10
|
|
|
use Mfonte\Sitemap\Tags\Tag; |
11
|
|
|
use Mfonte\Sitemap\Tags\Url; |
12
|
|
|
|
13
|
|
|
class Sitemap implements Responsable, Renderable |
14
|
|
|
{ |
15
|
|
|
/** @var \Mfonte\Sitemap\Tags\Url[] */ |
16
|
|
|
protected $tags = []; |
17
|
|
|
|
18
|
|
|
public static function create() |
19
|
|
|
{ |
20
|
|
|
return new static(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
final public function __construct() |
24
|
|
|
{ |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $tag string|Url|Sitemapable |
29
|
|
|
*/ |
30
|
|
|
public function add($tag) |
31
|
|
|
{ |
32
|
|
|
if (is_object($tag) && array_key_exists(Sitemapable::class, class_implements($tag))) { |
33
|
|
|
$tag = $tag->toSitemapTag(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (is_iterable($tag)) { |
37
|
|
|
foreach ($tag as $item) { |
38
|
|
|
$this->add($item); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (is_string($tag)) { |
45
|
|
|
$tag = Url::create($tag); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (! in_array($tag, $this->tags)) { |
49
|
|
|
$this->tags[] = $tag; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getTags(): array |
56
|
|
|
{ |
57
|
|
|
return $this->tags; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getUrl(string $url): ?Url |
61
|
|
|
{ |
62
|
|
|
return collect($this->tags)->first(function (Tag $tag) use ($url) { |
|
|
|
|
63
|
|
|
return $tag->getType() === 'url' && $tag->url === $url; |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function hasUrl(string $url): bool |
68
|
|
|
{ |
69
|
|
|
return (bool) $this->getUrl($url); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function hasImages() : bool |
73
|
|
|
{ |
74
|
|
|
return (bool) collect($this->tags)->first(function (Tag $tag) { |
|
|
|
|
75
|
|
|
return $tag->getType() === 'url' && ! empty($tag->images); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function hasNews() : bool |
80
|
|
|
{ |
81
|
|
|
return (bool) collect($this->tags)->first(function (Tag $tag) { |
|
|
|
|
82
|
|
|
return $tag->getType() === 'url' && ! empty($tag->news); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function render(): string |
87
|
|
|
{ |
88
|
|
|
$tags = collect($this->tags)->unique('url')->filter(); |
|
|
|
|
89
|
|
|
$hasImages = $this->hasImages(); |
90
|
|
|
$hasNews = $this->hasNews(); |
91
|
|
|
|
92
|
|
|
return view('sitemap::sitemap') |
93
|
|
|
->with(compact('tags', 'hasImages', 'hasNews')) |
94
|
|
|
->render(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function writeToFile(string $path) |
98
|
|
|
{ |
99
|
|
|
file_put_contents($path, $this->render()); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function writeToDisk(string $disk, string $path) |
105
|
|
|
{ |
106
|
|
|
Storage::disk($disk)->put($path, $this->render()); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create an HTTP response that represents the object. |
113
|
|
|
* |
114
|
|
|
* @param \Illuminate\Http\Request $request |
115
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
116
|
|
|
*/ |
117
|
|
|
public function toResponse($request) |
118
|
|
|
{ |
119
|
|
|
return Response::make($this->render(), 200, [ |
120
|
|
|
'Content-Type' => 'text/xml', |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|