Skip to content

Commit d2399d7

Browse files
Gummibeerfreekmurze
authored andcommitted
RSS feed type (spatie#110)
* fix the link type to use atom * add RSS feed type
1 parent 70bbfa3 commit d2399d7

10 files changed

+132
-42
lines changed

config/feed.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
/*
2626
* The view that will render the feed.
2727
*/
28-
'view' => 'feed::feed',
28+
'view' => 'feed::atom',
29+
30+
/*
31+
* The type to be used in the <link> tag
32+
*/
33+
'type' => 'application/atom+xml',
2934
],
3035
],
3136
];

resources/views/atom.blade.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?=
2+
/* Using an echo tag here so the `<? ... ?>` won't get parsed as short tags */
3+
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
4+
?>
5+
<feed xmlns="http://www.w3.org/2005/Atom">
6+
@foreach($meta as $key => $metaItem)
7+
@if($key === 'link')
8+
<{{ $key }} href="{{ url($metaItem) }}"></{{ $key }}>
9+
@elseif($key === 'title')
10+
<{{ $key }}><![CDATA[{{ $metaItem }}]]></{{ $key }}>
11+
@else
12+
<{{ $key }}>{{ $metaItem }}</{{ $key }}>
13+
@endif
14+
@endforeach
15+
@foreach($items as $item)
16+
<entry>
17+
<title><![CDATA[{{ $item->title }}]]></title>
18+
<link rel="alternate" href="{{ url($item->link) }}" />
19+
<id>{{ url($item->id) }}</id>
20+
<author>
21+
<name> <![CDATA[{{ $item->author }}]]></name>
22+
</author>
23+
<summary type="html">
24+
<![CDATA[{!! $item->summary !!}]]>
25+
</summary>
26+
@if($item->__isset('enclosure'))
27+
<enclosure url="{{ url($item->enclosure) }}" length="{{ $item->enclosureLength }}" type="{{ $item->enclosureType }}" />
28+
@endif
29+
<category type="html">
30+
<![CDATA[{!! $item->category ?? '' !!}]]>
31+
</category>
32+
<updated>{{ $item->updated->toRssString() }}</updated>
33+
</entry>
34+
@endforeach
35+
</feed>

resources/views/feed.blade.php

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1 @@
1-
<?=
2-
/* Using an echo tag here so the `<? ... ?>` won't get parsed as short tags */
3-
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
4-
?>
5-
<feed xmlns="http://www.w3.org/2005/Atom">
6-
@foreach($meta as $key => $metaItem)
7-
@if($key === 'link')
8-
<{{ $key }} href="{{ url($metaItem) }}"></{{ $key }}>
9-
@elseif($key === 'title')
10-
<{{ $key }}><![CDATA[{{ $metaItem }}]]></{{ $key }}>
11-
@else
12-
<{{ $key }}>{{ $metaItem }}</{{ $key }}>
13-
@endif
14-
@endforeach
15-
@foreach($items as $item)
16-
<entry>
17-
<title><![CDATA[{{ $item->title }}]]></title>
18-
<link rel="alternate" href="{{ url($item->link) }}" />
19-
<id>{{ url($item->id) }}</id>
20-
<author>
21-
<name> <![CDATA[{{ $item->author }}]]></name>
22-
</author>
23-
<summary type="html">
24-
<![CDATA[{!! $item->summary !!}]]>
25-
</summary>
26-
@if($item->__isset('enclosure'))
27-
<enclosure url="{{ url($item->enclosure) }}" length="{{ $item->enclosureLength }}" type="{{ $item->enclosureType }}" />
28-
@endif
29-
<category type="html">
30-
<![CDATA[{!! $item->category ?? '' !!}]]>
31-
</category>
32-
<updated>{{ $item->updated->toRssString() }}</updated>
33-
</entry>
34-
@endforeach
35-
</feed>
1+
@include('feed::atom')

resources/views/links.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
@foreach($feeds as $name => $title)
2-
<link rel="alternate" type="application/atom+xml" href="{{ route("feeds.{$name}") }}" title="{{ $title }}">
1+
@foreach($feeds as $name => $feed)
2+
<link rel="alternate" type="{{ $feed['type'] ?? 'application/atom+xml' }}" href="{{ route("feeds.{$name}") }}" title="{{ $feed['title'] }}">
33
@endforeach

resources/views/rss.blade.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?=
2+
/* Using an echo tag here so the `<? ... ?>` won't get parsed as short tags */
3+
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
4+
?>
5+
<rss version="2.0">
6+
<channel>
7+
<title><![CDATA[{{ $meta['title'] }}]]></title>
8+
<link><![CDATA[{{ url($meta['link']) }}]]></link>
9+
<description><![CDATA[{{ $meta['description'] }}]]></description>
10+
<language>{{ $meta['language'] }}</language>
11+
<pubDate>{{ $meta['updated'] }}</pubDate>
12+
13+
@foreach($items as $item)
14+
<item>
15+
<title><![CDATA[{{ $item->title }}]]></title>
16+
<link>{{ url($item->link) }}</link>
17+
<description><![CDATA[{!! $item->summary !!}]]></description>
18+
<author><![CDATA[{{ $item->author }}]]></author>
19+
<guid>{{ url($item->id) }}</guid>
20+
<pubDate>{{ $item->updated->toRssString() }}</pubDate>
21+
</item>
22+
@endforeach
23+
</channel>
24+
</rss>

src/FeedServiceProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public function registerLinksComposer()
5353

5454
protected function feeds()
5555
{
56-
return collect(config('feed.feeds'))->mapWithKeys(function ($feed, $name) {
57-
return [$name => $feed['title']];
58-
});
56+
return collect(config('feed.feeds'));
5957
}
6058
}

tests/FeedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class FeedTest extends TestCase
66
{
77
/** @var array */
8-
protected $feedNames = ['feed1', 'feed2', 'feed3'];
8+
protected $feedNames = ['feed1', 'feed2', 'feed3', 'feed1.rss'];
99

1010
/** @test */
1111
public function all_feeds_are_available_on_their_registered_routes()

tests/TestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ protected function getEnvironmentSetUp($app)
6161
'language' => 'en-US',
6262
'view' => 'feed::links',
6363
],
64+
[
65+
'items' => 'Spatie\Feed\Test\DummyRepository@getAll',
66+
'url' => '/feed1.rss',
67+
'title' => 'Feed 1 RSS',
68+
'description' => 'This is feed 1 as RSS from the unit tests',
69+
'language' => 'en-US',
70+
'view' => 'feed::rss',
71+
'type' => 'application/rss+xml',
72+
],
6473
];
6574

6675
$app['config']->set('feed.feeds', $feed);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php return '<?xml version="1.0" encoding="UTF-8"?>
2+
<rss version="2.0">
3+
<channel>
4+
<title><![CDATA[Feed 1 RSS]]></title>
5+
<link><![CDATA[http://localhost/feedBaseUrl/feed1.rss]]></link>
6+
<description><![CDATA[This is feed 1 as RSS from the unit tests]]></description>
7+
<language>en-US</language>
8+
<pubDate>Fri, 01 Jan 2016 00:00:00 +0100</pubDate>
9+
10+
<item>
11+
<title><![CDATA[feedItemTitle]]></title>
12+
<link>https://localhost/news/testItem1</link>
13+
<description><![CDATA[feedItemSummary]]></description>
14+
<author><![CDATA[feedItemAuthor]]></author>
15+
<guid>http://localhost/1</guid>
16+
<pubDate>Fri, 01 Jan 2016 00:00:00 +0100</pubDate>
17+
</item>
18+
<item>
19+
<title><![CDATA[feedItemTitle]]></title>
20+
<link>https://localhost/news/testItem1</link>
21+
<description><![CDATA[feedItemSummary]]></description>
22+
<author><![CDATA[feedItemAuthor]]></author>
23+
<guid>http://localhost/1</guid>
24+
<pubDate>Fri, 01 Jan 2016 00:00:00 +0100</pubDate>
25+
</item>
26+
<item>
27+
<title><![CDATA[feedItemTitle]]></title>
28+
<link>https://localhost/news/testItem1</link>
29+
<description><![CDATA[feedItemSummary]]></description>
30+
<author><![CDATA[feedItemAuthor]]></author>
31+
<guid>http://localhost/1</guid>
32+
<pubDate>Fri, 01 Jan 2016 00:00:00 +0100</pubDate>
33+
</item>
34+
<item>
35+
<title><![CDATA[feedItemTitle]]></title>
36+
<link>https://localhost/news/testItem1</link>
37+
<description><![CDATA[feedItemSummary]]></description>
38+
<author><![CDATA[feedItemAuthor]]></author>
39+
<guid>http://localhost/1</guid>
40+
<pubDate>Fri, 01 Jan 2016 00:00:00 +0100</pubDate>
41+
</item>
42+
<item>
43+
<title><![CDATA[feedItemTitle]]></title>
44+
<link>https://localhost/news/testItem1</link>
45+
<description><![CDATA[feedItemSummary]]></description>
46+
<author><![CDATA[feedItemAuthor]]></author>
47+
<guid>http://localhost/1</guid>
48+
<pubDate>Fri, 01 Jan 2016 00:00:00 +0100</pubDate>
49+
</item>
50+
</channel>
51+
</rss>
52+
';

tests/__snapshots__/FeedTest__it_can_render_all_feed_links_via_a_view__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<link rel="alternate" type="application/atom+xml" href="http://localhost/feedBaseUrl/feed2" title="Feed 2">
33
<link rel="alternate" type="application/atom+xml" href="http://localhost/feedBaseUrl/feed3" title="Feed 3">
44
<link rel="alternate" type="application/atom+xml" href="http://localhost/feedBaseUrl/feed-with-custom-view" title="Feed with Custom View">
5+
<link rel="alternate" type="application/rss+xml" href="http://localhost/feedBaseUrl/feed1.rss" title="Feed 1 RSS">
56
';

0 commit comments

Comments
 (0)