Skip to content

Commit

Permalink
Add public for ->writeToDisk() as option (#534)
Browse files Browse the repository at this point in the history
* Update SitemapIndex.php For S3 Public Storage Option

Add visibility option for sitemap index.

* Update Sitemap.php for S3 Public Visibility

Add visibility option

* Update SitemapIndex.php for visibility

Add public private visibility option

* convert if statement to ternary

* added tests

* remove idea files from php storm

* remove --snapshots--

* remove idea files

* documentation
  • Loading branch information
bmckay959 authored Jan 24, 2024
1 parent e685eb6 commit 20e0cc1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/SitemapIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion tests/SitemapIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
11 changes: 11 additions & 0 deletions tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 20e0cc1

Please sign in to comment.