| @@ 8-76 (lines=69) @@ | ||
| 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 | /** |
|
| 22 | * @param string|\Spatie\Sitemap\Tags\Tag $url |
|
| 23 | * |
|
| 24 | * @return $this |
|
| 25 | */ |
|
| 26 | public function add($url) |
|
| 27 | { |
|
| 28 | if (is_string($url)) { |
|
| 29 | $url = Url::create($url); |
|
| 30 | } |
|
| 31 | ||
| 32 | $this->tags[] = $url; |
|
| 33 | ||
| 34 | return $this; |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @param string $url |
|
| 39 | * |
|
| 40 | * @return \Spatie\Sitemap\Tags\Url|null |
|
| 41 | */ |
|
| 42 | public function getUrl(string $url) |
|
| 43 | { |
|
| 44 | return collect($this->tags)->first(function (Tag $tag) use ($url) { |
|
| 45 | return $tag->getType() === 'url' && $tag->url === $url; |
|
| 46 | }); |
|
| 47 | } |
|
| 48 | ||
| 49 | public function hasUrl(string $url): bool |
|
| 50 | { |
|
| 51 | return (bool) $this->getUrl($url); |
|
| 52 | } |
|
| 53 | ||
| 54 | public function render(): string |
|
| 55 | { |
|
| 56 | sort($this->tags); |
|
| 57 | ||
| 58 | $tags = $this->tags; |
|
| 59 | ||
| 60 | return view('laravel-sitemap::sitemap') |
|
| 61 | ->with(compact('tags')) |
|
| 62 | ->render(); |
|
| 63 | } |
|
| 64 | ||
| 65 | /** |
|
| 66 | * @param string $path |
|
| 67 | * |
|
| 68 | * @return $this |
|
| 69 | */ |
|
| 70 | public function writeToFile(string $path) |
|
| 71 | { |
|
| 72 | file_put_contents($path, $this->render()); |
|
| 73 | ||
| 74 | return $this; |
|
| 75 | } |
|
| 76 | } |
|
| 77 | ||
| @@ 8-88 (lines=81) @@ | ||
| 5 | use Spatie\Sitemap\Tags\Tag; |
|
| 6 | use Spatie\Sitemap\Tags\Sitemap; |
|
| 7 | ||
| 8 | class SitemapIndex |
|
| 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 | /** |
|
| 22 | * @param string|\Spatie\Sitemap\Tags\Tag $tag |
|
| 23 | * |
|
| 24 | * @return $this |
|
| 25 | */ |
|
| 26 | public function add($tag) |
|
| 27 | { |
|
| 28 | if (is_string($tag)) { |
|
| 29 | $tag = Sitemap::create($tag); |
|
| 30 | } |
|
| 31 | ||
| 32 | $this->tags[] = $tag; |
|
| 33 | ||
| 34 | return $this; |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Get sitemap tag. |
|
| 39 | * |
|
| 40 | * @param string $url |
|
| 41 | * |
|
| 42 | * @return \Spatie\Sitemap\Tags\Sitemap|null |
|
| 43 | */ |
|
| 44 | public function getSitemap(string $url) |
|
| 45 | { |
|
| 46 | return collect($this->tags)->first(function (Tag $tag) use ($url) { |
|
| 47 | return $tag->getType() === 'sitemap' && $tag->url === $url; |
|
| 48 | }); |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * Check if there is the provided sitemap in the index. |
|
| 53 | * |
|
| 54 | * @param string $url |
|
| 55 | * |
|
| 56 | * @return bool |
|
| 57 | */ |
|
| 58 | public function hasSitemap(string $url): bool |
|
| 59 | { |
|
| 60 | return (bool) $this->getSitemap($url); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Get the inflated template content. |
|
| 65 | * |
|
| 66 | * @return string |
|
| 67 | */ |
|
| 68 | public function render(): string |
|
| 69 | { |
|
| 70 | $tags = $this->tags; |
|
| 71 | ||
| 72 | return view('laravel-sitemap::sitemapIndex/index') |
|
| 73 | ->with(compact('tags')) |
|
| 74 | ->render(); |
|
| 75 | } |
|
| 76 | ||
| 77 | /** |
|
| 78 | * @param string $path |
|
| 79 | * |
|
| 80 | * @return $this |
|
| 81 | */ |
|
| 82 | public function writeToFile(string $path) |
|
| 83 | { |
|
| 84 | file_put_contents($path, $this->render()); |
|
| 85 | ||
| 86 | return $this; |
|
| 87 | } |
|
| 88 | } |
|
| 89 | ||