This repository has been archived by the owner on May 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathroutes.php
90 lines (74 loc) · 2.97 KB
/
routes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
use Adrenth\RssFetcher\Models\Feed as FeedModel;
use Adrenth\RssFetcher\Models\Item;
use Adrenth\RssFetcher\Models\Source;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Response;
use Zend\Feed\Exception\InvalidArgumentException;
use Zend\Feed\Writer\Entry;
use Zend\Feed\Writer\Feed;
Route::get('/feeds/{path}', function ($path) {
/** @var FeedModel $model */
$model = FeedModel::where('path', '=', $path)->first();
if ($model === null) {
return Response::make('Not Found', 404);
}
$feed = new Feed();
$feed->setTitle($model->getAttribute('title'))
->setDescription($model->getAttribute('description'))
->setBaseUrl(Url::to('/'))
->setGenerator('OctoberCMS/Adrenth.RssFetcher')
->setId('Adrenth.RssFecther.' . $model->getAttribute('id'))
->setLink(Url::to('/feeds/' . $path))
->setFeedLink(Url::to('/feeds/' . $path), $model->getAttribute('type'))
->setDateModified()
->addAuthor(['name' => 'OctoberCMS']);
/** @var Collection $sources */
$sources = $model->sources;
$ids = Arr::pluck($sources->toArray(), 'id');
$items = [];
Source::with(['items' => function ($builder) use (&$items, $model) {
$items = $builder->where('is_published', '=', 1)
->whereDate('pub_date', '<=', date('Y-m-d'))
->orderBy('pub_date', 'desc')
->limit($model->getAttribute('max_items'))
->get();
}])->whereIn('id', $ids)
->where('is_enabled', '=', 1)
->get();
/** @var Item $item */
foreach ($items as $item) {
try {
$entry = new Entry();
$entry->setId((string) $item->getAttribute('id'))
->setTitle($item->getAttribute('title'))
->setDescription($item->getAttribute('description'))
->setLink($item->getAttribute('link'))
->setDateModified($item->getAttribute('pub_date'));
$comments = $item->getAttribute('comments');
if (!empty($comments)) {
$entry->setCommentLink($comments);
}
$category = $item->getAttribute('category');
if (!empty($category)) {
$entry->addCategory(['term' => $category]);
}
$enclosureUrl = $item->getAttribute('enclosure_url');
if (!empty($enclosureUrl)) {
$entry->setEnclosure([
'uri' => $enclosureUrl,
'type' => $item->getAttribute('enclosure_type'),
'length' => $item->getAttribute('enclosure_length'),
]);
}
$feed->addEntry($entry);
} catch (InvalidArgumentException $e) {
continue;
}
}
return Response::make($feed->export($model->getAttribute('type')), 200, [
'Content-Type' => sprintf('application/%s+xml', $model->getAttribute('type')),
]);
});