]>
Commit | Line | Data |
---|---|---|
7b2ba6ef A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
7b2ba6ef A |
6 | |
7 | use Shaarli\Feed\FeedBuilder; | |
8 | use Slim\Http\Request; | |
9 | use Slim\Http\Response; | |
10 | ||
11 | /** | |
12 | * Class FeedController | |
13 | * | |
14 | * Slim controller handling ATOM and RSS feed. | |
7b2ba6ef | 15 | */ |
2899ebb5 | 16 | class FeedController extends ShaarliVisitorController |
7b2ba6ef A |
17 | { |
18 | public function atom(Request $request, Response $response): Response | |
19 | { | |
20 | return $this->processRequest(FeedBuilder::$FEED_ATOM, $request, $response); | |
21 | } | |
22 | ||
23 | public function rss(Request $request, Response $response): Response | |
24 | { | |
25 | return $this->processRequest(FeedBuilder::$FEED_RSS, $request, $response); | |
26 | } | |
27 | ||
28 | protected function processRequest(string $feedType, Request $request, Response $response): Response | |
29 | { | |
30 | $response = $response->withHeader('Content-Type', 'application/'. $feedType .'+xml; charset=utf-8'); | |
31 | ||
32 | $pageUrl = page_url($this->container->environment); | |
33 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl); | |
34 | ||
35 | $cached = $cache->cachedVersion(); | |
36 | if (!empty($cached)) { | |
37 | return $response->write($cached); | |
38 | } | |
39 | ||
40 | // Generate data. | |
41 | $this->container->feedBuilder->setLocale(strtolower(setlocale(LC_COLLATE, 0))); | |
42 | $this->container->feedBuilder->setHideDates($this->container->conf->get('privacy.hide_timestamps', false)); | |
43 | $this->container->feedBuilder->setUsePermalinks( | |
44 | null !== $request->getParam('permalinks') || !$this->container->conf->get('feed.rss_permalinks') | |
45 | ); | |
46 | ||
47 | $data = $this->container->feedBuilder->buildData($feedType, $request->getParams()); | |
48 | ||
c22fa57a | 49 | $data = $this->executeHooks($data, $feedType); |
7b2ba6ef A |
50 | $this->assignAllView($data); |
51 | ||
52 | $content = $this->render('feed.'. $feedType); | |
53 | ||
54 | $cache->cache($content); | |
55 | ||
56 | return $response->write($content); | |
57 | } | |
58 | ||
59 | /** | |
60 | * @param mixed[] $data Template data | |
61 | * | |
62 | * @return mixed[] Template data after active plugins hook execution. | |
63 | */ | |
64 | protected function executeHooks(array $data, string $feedType): array | |
65 | { | |
66 | $this->container->pluginManager->executeHooks( | |
67 | 'render_feed', | |
68 | $data, | |
69 | [ | |
70 | 'loggedin' => $this->container->loginManager->isLoggedIn(), | |
71 | 'target' => $feedType, | |
72 | ] | |
73 | ); | |
74 | ||
75 | return $data; | |
76 | } | |
77 | } |