123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Service;
- use App\Entity\Article;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use App\Repository\ArticleRepository;
- use Laminas\Feed\Writer\Entry;
- use Laminas\Feed\Writer\Feed;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\Routing\RouterInterface;
- class FeedService
- {
- public const ATOM = 'atom';
- public const RSS = 'rss';
- public function __construct(
- protected ArticleRepository $articleRepository,
- protected UrlGeneratorInterface $router,
- protected MarkdownParser $markdownParser,
- protected ParameterBagInterface $parameterBagInterface
- )
- {
- }
- public function createFeed(array $articles, \DateTimeImmutable $updatedAt, string $type = self::RSS): string
- {
- $feed = new Feed();
- $feed->setTitle($this->parameterBagInterface->get('title'));
- $feed->setLink($this->router->generate('app_index', [], RouterInterface::ABSOLUTE_URL));
- $feed->setFeedLink($this->router->generate('app_feed', ['type' => $type], RouterInterface::ABSOLUTE_URL), $type);
- $feed->setDescription('Derniers articles du blog');
- $feed->setLanguage('fr');
- $feed->setDateModified($updatedAt);
- foreach($this->getItems($articles) as $item) {
- $feed->addEntry($item);
- }
- return $feed->export($type);
- }
- public function getItems($articles): \Iterator
- {
- foreach($articles as $article) {
- /** @var Article $article */
- $item = new Entry();
- $item
- ->setTitle($article->getTitle())
- ->setLink($this->router->generate('app_view', ['slug' => $article->getSlug()], RouterInterface::ABSOLUTE_URL))
- ->setDateCreated($article->getPublicationDate())
- ->setDateModified($item->getDateCreated())
- ->addAuthor(['name' => (string) $article->getAuthor()])
- ;
- $tags = $article->getTags();
- foreach($tags as $tag) {
- $item->addCategory([
- 'term' => (string) $tag->getName(),
- 'scheme' => $this->router->generate('app_search', ['tag' => $tag->getName()], RouterInterface::ABSOLUTE_URL)
- ]);
- }
- # Fix pour supprimer le allowfullscreen récupéré par l'extension embed pour youtube
- $content = $this->markdownParser->convertToHtml($article->getContent());
- $content = str_replace('allowfullscreen', 'allowfullscreen="true"', $content);
- $item->setContent($content);
- ;
- yield $item;
- }
- }
- }
|