Skip to content

Commit

Permalink
Implement feed provider to allow decoration of feed generation and it…
Browse files Browse the repository at this point in the history
…em transformation
  • Loading branch information
bezin committed Jun 1, 2022
1 parent 96965d7 commit 19aad3b
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 256 deletions.
45 changes: 12 additions & 33 deletions news-bundle/src/Controller/NewsFeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@

namespace Contao\NewsBundle\Controller;

use Contao\CoreBundle\Asset\ContaoContext;
use Contao\CoreBundle\Controller\AbstractController;
use Contao\CoreBundle\Routing\Page\DynamicRouteInterface;
use Contao\CoreBundle\Routing\Page\PageRoute;
use Contao\CoreBundle\ServiceAnnotation\Page;
use Contao\NewsBundle\Event\TransformFeedItemEvent;
use Contao\NewsBundle\Feed\FeedProviderInterface;
use Contao\NewsModel;
use Contao\PageModel;
use Contao\StringUtil;
use FeedIo\Feed;
use FeedIo\Feed\Item;
use FeedIo\Specification;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -45,48 +46,26 @@ class NewsFeedController extends AbstractController implements DynamicRouteInter
'rss' => '.xml',
];

public function __construct(private Specification $specification)
public function __construct(private readonly ContaoContext $contaoContext, private readonly FeedProviderInterface $feedProvider, private readonly Specification $specification)
{
}

public function __invoke(Request $request, PageModel $pageModel): Response
{
$this->initializeContaoFramework();

// TODO: Make $feed extendable for third-party code
$feed = new Feed();
$feed->setTitle($pageModel->title);
$feed->setDescription($pageModel->description);
$feed->setLanguage($pageModel->language);
$staticUrl = $this->contaoContext->getStaticUrl();
$baseUrl = $staticUrl ?: $request->getSchemeAndHttpHost();

$lastModified = null;
$this->feedProvider->setContext($pageModel, $request, $baseUrl);

$archives = StringUtil::deserialize($pageModel->newsArchives);
$featured = match ($pageModel->feedFeatured) {
'featured' => true,
'unfeatured' => false,
default => null,
};
$newsModel = $this->getContaoAdapter(NewsModel::class);

// TODO: Make this extendable for third-party code
if ($pageModel->maxFeedItems > 0) {
$articles = $newsModel->findPublishedByPids($archives, $featured, $pageModel->maxFeedItems);
} else {
$articles = $newsModel->findPublishedByPids($archives, $featured);
}
$feed = $this->feedProvider->getFeed();
$articles = $this->feedProvider->getItems();
$lastModified = null;

if (null !== $articles) {
foreach ($articles as $article) {
$event = new TransformFeedItemEvent($article, $pageModel, $request);
$this->container->get('event_dispatcher')->dispatch($event, TransformFeedItemEvent::NAME);

$item = $event->getItem();

if (null === $item) {
continue;
}

$item = $this->feedProvider->getItemFromModel($article);
$lastModified = null === $lastModified ? $item->getLastModified() : min($lastModified, $item->getLastModified());
$feed->add($item);
$this->tagResponse($article);
Expand All @@ -101,7 +80,7 @@ public function __invoke(Request $request, PageModel $pageModel): Response

$this->setCacheHeaders($response, $pageModel);

foreach ($archives as $archive) {
foreach (StringUtil::deserialize($pageModel->newsArchives, true) as $archive) {
$this->tagResponse('contao.db.tl_news_archive.'.$archive);
}

Expand Down
61 changes: 0 additions & 61 deletions news-bundle/src/Event/TransformFeedItemEvent.php

This file was deleted.

150 changes: 0 additions & 150 deletions news-bundle/src/EventListener/TransformFeedItemListener.php

This file was deleted.

Loading

0 comments on commit 19aad3b

Please sign in to comment.