-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathBlogController.php
More file actions
65 lines (52 loc) · 1.56 KB
/
Copy pathBlogController.php
File metadata and controls
65 lines (52 loc) · 1.56 KB
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
<?php
namespace App\Web\Blog;
use App\Web\Meta\MetaType;
use DateTimeImmutable;
use Tempest\Cache\Cache;
use Tempest\Router\Get;
use Tempest\Router\Response;
use Tempest\Router\Responses\NotFound;
use Tempest\Router\Responses\Ok;
use Tempest\Router\StaticPage;
use Tempest\Support\Arr\ImmutableArray;
use Tempest\View\View;
use function Tempest\view;
final readonly class BlogController
{
#[Get('/blog')]
#[StaticPage]
public function index(BlogRepository $repository): View
{
$posts = $repository->all();
return view('./index.view.php', posts: $posts, meta: MetaType::BLOG);
}
#[Get('/blog/{slug}')]
#[StaticPage(BlogDataProvider::class)]
public function show(string $slug, BlogRepository $repository): Response|View
{
$post = $repository->find($slug);
if (! $post || ! $post->published) {
return new NotFound();
}
return view('./show.view.php', post: $post);
}
#[Get('/rss')]
public function rss(
Cache $cache,
BlogRepository $repository,
): Response {
$xml = $cache->resolve(
key: 'rss',
cache: fn () => $this->renderRssFeed($repository->all(loadContent: true)),
expiresAt: new DateTimeImmutable('+1 hour'),
);
return new Ok($xml)
->addHeader('Content-Type', 'application/xml;charset=UTF-8');
}
private function renderRssFeed(ImmutableArray $posts): string
{
ob_start();
include __DIR__ . '/rss.view.php';
return trim(ob_get_clean());
}
}