diff --git a/README.md b/README.md index f9713a4..1ceea4d 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,11 @@ You can also use one of your available filesystem disks to write the sitemap to. SitemapGenerator::create('https://example.com')->getSitemap()->writeToDisk('public', 'sitemap.xml'); ``` +You may need to set the file visibility on one of your sitemaps. For example, if you are writing a sitemap to S3 that you want to be publicly available. You can set the third parameter to `true` to make it public. Note: This can only be used on the `->writeToDisk()` method. +```php +SitemapGenerator::create('https://example.com')->getSitemap()->writeToDisk('public', 'sitemap.xml', true); +``` + You can also add your models directly by implementing the `\Spatie\Sitemap\Contracts\Sitemapable` interface. ```php diff --git a/src/Sitemap.php b/src/Sitemap.php index c9b9966..70268ec 100644 --- a/src/Sitemap.php +++ b/src/Sitemap.php @@ -78,9 +78,11 @@ public function writeToFile(string $path): static return $this; } - public function writeToDisk(string $disk, string $path): static + public function writeToDisk(string $disk, string $path, bool $public = false): static { - Storage::disk($disk)->put($path, $this->render()); + $visibility = ($public) ? 'public' : 'private'; + + Storage::disk($disk)->put($path, $this->render(), $visibility); return $this; } diff --git a/src/SitemapIndex.php b/src/SitemapIndex.php index 859efb8..59236ab 100644 --- a/src/SitemapIndex.php +++ b/src/SitemapIndex.php @@ -58,9 +58,11 @@ public function writeToFile(string $path): static return $this; } - public function writeToDisk(string $disk, string $path): static + public function writeToDisk(string $disk, string $path, bool $public = false): static { - Storage::disk($disk)->put($path, $this->render()); + $visibility = ($public) ? 'public' : 'private'; + + Storage::disk($disk)->put($path, $this->render(), $visibility); return $this; } diff --git a/tests/SitemapIndexTest.php b/tests/SitemapIndexTest.php index e7a123f..06e4541 100644 --- a/tests/SitemapIndexTest.php +++ b/tests/SitemapIndexTest.php @@ -30,11 +30,22 @@ assertMatchesXmlSnapshot(file_get_contents($path)); }); -it('can write a sitemap to a storage disk', function () { +it('can write a sitemap to a storage disk with private visibility', function () { Storage::fake('sitemap'); $this->index->writeToDisk('sitemap', 'sitemap.xml'); + $visibility = Storage::disk('sitemap')->getVisibility('sitemap.xml'); assertMatchesXmlSnapshot(Storage::disk('sitemap')->get('sitemap.xml')); + expect($visibility)->toBe('private'); +}); + +it('can write a sitemap to a storage disk with public visibility', function () { + Storage::fake('sitemap'); + $this->index->writeToDisk('sitemap', 'sitemap.xml', true); + $visibility = Storage::disk('sitemap')->getVisibility('sitemap.xml'); + + assertMatchesXmlSnapshot(Storage::disk('sitemap')->get('sitemap.xml')); + expect($visibility)->toBe('public'); }); test('an url string can be added to the index', function () { diff --git a/tests/SitemapTest.php b/tests/SitemapTest.php index c39a657..3584e22 100644 --- a/tests/SitemapTest.php +++ b/tests/SitemapTest.php @@ -34,8 +34,19 @@ it('can write a sitemap to a storage disk', function () { Storage::fake('sitemap'); $this->sitemap->writeToDisk('sitemap', 'sitemap.xml'); + $visibility = Storage::disk('sitemap')->getVisibility('sitemap.xml'); assertMatchesXmlSnapshot(Storage::disk('sitemap')->get('sitemap.xml')); + expect($visibility)->toBe('private'); +}); + +it('can write a sitemap to a storage disk with public visibility', function () { + Storage::fake('sitemap'); + $this->sitemap->writeToDisk('sitemap', 'sitemap.xml', true); + $visibility = Storage::disk('sitemap')->getVisibility('sitemap.xml'); + + assertMatchesXmlSnapshot(Storage::disk('sitemap')->get('sitemap.xml')); + expect($visibility)->toBe('public'); }); test('an url string can be added to the sitemap', function () {