|
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\Tags\Sitemap; |
|
|
|
|
|
|
10
|
|
|
use Mfonte\Sitemap\Tags\Tag; |
|
11
|
|
|
|
|
12
|
|
|
class SitemapIndex implements Responsable, Renderable |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Mfonte\Sitemap\Tags\Sitemap[] */ |
|
15
|
|
|
protected array $tags = []; |
|
16
|
|
|
|
|
17
|
|
|
public static function create() |
|
18
|
|
|
{ |
|
19
|
|
|
return new static(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
final public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $tag string|Sitemap |
|
28
|
|
|
*/ |
|
29
|
|
|
public function add($tag) |
|
30
|
|
|
{ |
|
31
|
|
|
if (is_string($tag)) { |
|
32
|
|
|
$tag = Sitemap::create($tag); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->tags[] = $tag; |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getSitemap(string $url): ?Sitemap |
|
41
|
|
|
{ |
|
42
|
|
|
return collect($this->tags)->first(function (Tag $tag) use ($url) { |
|
|
|
|
|
|
43
|
|
|
return $tag->getType() === 'sitemap' && $tag->url === $url; |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function hasSitemap(string $url): bool |
|
48
|
|
|
{ |
|
49
|
|
|
return (bool) $this->getSitemap($url); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function render(): string |
|
53
|
|
|
{ |
|
54
|
|
|
$tags = $this->tags; |
|
55
|
|
|
|
|
56
|
|
|
return view('sitemap::sitemapIndex/index') |
|
57
|
|
|
->with(compact('tags')) |
|
58
|
|
|
->render(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function writeToFile(string $path) |
|
62
|
|
|
{ |
|
63
|
|
|
file_put_contents($path, $this->render()); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function writeToDisk(string $disk, string $path) |
|
69
|
|
|
{ |
|
70
|
|
|
Storage::disk($disk)->put($path, $this->render()); |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create an HTTP response that represents the object. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \Illuminate\Http\Request $request |
|
79
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
80
|
|
|
*/ |
|
81
|
|
|
public function toResponse($request) |
|
82
|
|
|
{ |
|
83
|
|
|
return Response::make($this->render(), 200, [ |
|
84
|
|
|
'Content-Type' => 'text/xml', |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: