Skip to content

Commit 8a785ee

Browse files
Added sitemap receipt generation
1 parent d122758 commit 8a785ee

File tree

12 files changed

+166
-45
lines changed

12 files changed

+166
-45
lines changed

docs/snippets/receipt-sitemap-feed-item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Feeds\Items\Sitemaps;
5+
namespace App\Feeds\Sitemaps\Items;
66

77
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
88

docs/snippets/receipt-sitemap-feed.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace App\Feeds\Sitemaps;
66

7-
use App\Feeds\Items\Sitemaps\ProductFeedItem;
8-
use App\Models\Product;
97
use DragonCode\LaravelFeed\Data\ElementData;
108
use DragonCode\LaravelFeed\Feeds\Feed;
119
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
1210
use Illuminate\Database\Eloquent\Builder;
1311
use Illuminate\Database\Eloquent\Model;
12+
use App\Feeds\Sitemaps\Items\ProductFeedItem;
13+
use App\Models\Product;
1414

1515
class ProductFeed extends Feed
1616
{
@@ -32,18 +32,18 @@ public function builder(): Builder
3232
public function root(): ElementData
3333
{
3434
return new ElementData(
35-
$this->name,
36-
$this->attributes
35+
name : $this->name,
36+
attributes: $this->attributes,
3737
);
3838
}
3939

40-
public function filename(): string
40+
public function item(Model $model): FeedItem
4141
{
42-
return 'sitemaps/products.xml';
42+
return new ProductFeedItem($model);
4343
}
4444

45-
public function item(Model $model): FeedItem
45+
public function filename(): string
4646
{
47-
return new ProductFeedItem($model);
47+
return 'sitemaps/products.xml';
4848
}
4949
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
3+
4+
<url>
5+
<loc>https://example.com/products/porro-odit-molestiae-illo-nemo-et</loc>
6+
<lastmod>2025-09-04T04:08:12+00:00</lastmod>
7+
<priority>0.9</priority>
8+
</url>
9+
<url>
10+
<loc>https://example.com/products/magni-similique-voluptas-qui-doloribus</loc>
11+
<lastmod>2025-09-04T04:08:12+00:00</lastmod>
12+
<priority>0.9</priority>
13+
</url>
14+
15+
</urlset>

docs/snippets/receipt-sitemap-result.xml

Lines changed: 0 additions & 32 deletions
This file was deleted.

docs/topics/receipt-sitemap.topic

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</chapter>
5757

5858
<chapter title="Result" id="result">
59-
<code-block lang="xml" src="receipt-sitemap-result.xml" />
59+
<code-block lang="xml" src="receipt-sitemap-feed.xml" />
6060
</chapter>
6161

6262
</topic>

tests/Feature/Docs/ReceiptTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Commands\FeedGenerateCommand;
6+
use DragonCode\LaravelFeed\Models\Feed;
7+
use Workbench\App\Feeds\Docs\ReceiptSitemapFeed;
8+
use Workbench\App\Models\Product;
9+
10+
use function Pest\Laravel\artisan;
11+
12+
it('generate stub', function (string $feed, array $files, array $replaces = []): void {
13+
Product::factory()->count(2)->create();
14+
15+
$model = Feed::create([
16+
'class' => $feed,
17+
'title' => $feed,
18+
]);
19+
20+
artisan(FeedGenerateCommand::class, ['feed' => $model->id])
21+
->assertSuccessful()
22+
->run();
23+
24+
foreach ($files as $from => $to) {
25+
copyFeedFileToDoc($from, $to, $replaces, false);
26+
}
27+
})->with([
28+
'sitemap' => [
29+
'feed' => ReceiptSitemapFeed::class,
30+
31+
'files' => [
32+
'ReceiptSitemapFeed' => 'receipt-sitemap-feed.php',
33+
'Items/ReceiptSitemapFeedItem' => 'receipt-sitemap-feed-item.php',
34+
],
35+
36+
'replaces' => [
37+
'ReceiptSitemapFeed' => 'ProductFeed',
38+
'Workbench\App\Feeds\Docs' => 'App\Feeds\Sitemaps',
39+
40+
'../../../../../../../../../docs/snippets/receipt-sitemap-feed.xml' => 'sitemaps/products.xml',
41+
],
42+
],
43+
]);

tests/Helpers/docs.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@
44

55
use function Orchestra\Testbench\workbench_path;
66

7-
function copyFeedFileToDoc(string $source, string $target): void
7+
function copyFeedFileToDoc(string $source, string $target, array $replaces = [], bool $cutFilename = true): void
88
{
99
$sourceFile = workbench_path('app/Feeds/Docs/' . $source . '.php');
1010
$targetFile = __DIR__ . '/../../docs/snippets/' . $target;
1111

1212
$content = file_get_contents($sourceFile);
1313

14+
if (! empty($replaces)) {
15+
$content = str_replace(
16+
array_keys($replaces),
17+
array_values($replaces),
18+
$content
19+
);
20+
}
21+
1422
$content = str_replace([
15-
'Workbench\App\Models\User',
1623
'Workbench\App\Feeds\Docs',
24+
'Workbench\App\\',
1725
], [
18-
'App\Models\User',
1926
'App\Feeds',
27+
'App\\',
2028
], $content);
2129

30+
if ($cutFilename) {
31+
$content = preg_replace('/(\n\s+public\sfunction\sfilename\(\):\sstring\n\s+{\n\s+.*\n\s+})/', '', $content);
32+
}
33+
2234
file_put_contents($targetFile, $content);
2335
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Feeds\Docs\Items;
6+
7+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
8+
9+
use function route;
10+
11+
/** @property-read \Workbench\App\Models\Product $model */
12+
class ReceiptSitemapFeedItem extends FeedItem
13+
{
14+
public function name(): string
15+
{
16+
return 'url';
17+
}
18+
19+
public function toArray(): array
20+
{
21+
return [
22+
'loc' => route('products.show', $this->model->slug),
23+
24+
'lastmod' => $this->model->updated_at->toIso8601String(),
25+
26+
'priority' => 0.9,
27+
];
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Feeds\Docs;
6+
7+
use DragonCode\LaravelFeed\Data\ElementData;
8+
use DragonCode\LaravelFeed\Feeds\Feed;
9+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
10+
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Database\Eloquent\Model;
12+
use Workbench\App\Feeds\Docs\Items\ReceiptSitemapFeedItem;
13+
use Workbench\App\Models\Product;
14+
15+
class ReceiptSitemapFeed extends Feed
16+
{
17+
protected string $name = 'urlset';
18+
19+
protected array $attributes = [
20+
'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
21+
'xmlns:xhtml' => 'http://www.w3.org/1999/xhtml',
22+
'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1',
23+
'xmlns:video' => 'http://www.google.com/schemas/sitemap-video/1.1',
24+
'xmlns:news' => 'http://www.google.com/schemas/sitemap-news/0.9',
25+
];
26+
27+
public function builder(): Builder
28+
{
29+
return Product::query();
30+
}
31+
32+
public function root(): ElementData
33+
{
34+
return new ElementData(
35+
name : $this->name,
36+
attributes: $this->attributes,
37+
);
38+
}
39+
40+
public function item(Model $model): FeedItem
41+
{
42+
return new ReceiptSitemapFeedItem($model);
43+
}
44+
45+
public function filename(): string
46+
{
47+
return '../../../../../../../../../docs/snippets/receipt-sitemap-feed.xml';
48+
}
49+
}

workbench/app/Models/Product.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Product extends Model
1515
use HasFactory;
1616

1717
protected $fillable = [
18+
'slug',
1819
'article',
1920
'title',
2021
'description',

0 commit comments

Comments
 (0)