This commit is contained in:
Simon Vieille 2021-03-30 20:40:20 +02:00
parent 412db9bd7f
commit c482b86070
4 changed files with 192 additions and 0 deletions

View file

@ -11,10 +11,12 @@ use App\Entity\Blog\Category;
use App\Entity\Blog\Post;
use App\Factory\Blog\CommentFactory;
use App\Form\Blog\UserCommentType;
use App\Markdown\Parser\Post as PostParser;
use App\Repository\Blog\PostRepositoryQuery;
use App\UrlGenerator\PostGenerator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PostController extends PageController
{
@ -148,4 +150,37 @@ class PostController extends PageController
->published()
;
}
public function rss(PostParser $parser): Response
{
$entities = $this->createQuery()->paginate(1, 20);
$items = [];
foreach ($entities as $entity) {
$items[] = [
'title' => $entity->getTitle(),
'guid' => $entity->getId(),
'date' => $entity->getPublishedAt(),
'description' => $parser->transformMarkdown($entity->getContent()),
'categories' => call_user_func(function () use ($entity) {
foreach ($entity->getCategories() as $category) {
if ($category->getIsActive()) {
yield $category->getTitle();
}
}
}),
'link' => $this->generateUrl('blog_menu_post', [
'post' => $entity->getId(),
'slug' => $entity->getSlug(),
], UrlGeneratorInterface::ABSOLUTE_URL),
];
}
$response = new Response();
$response->headers->set('Content-Type', 'text/xml');
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
'items' => $items,
], $response);
}
}

View file

@ -4,6 +4,7 @@ namespace App\Controller;
use App\Api\TTRssClient;
use App\Core\Controller\Site\PageController;
use App\Markdown\Parser\Post as PostParser;
use Symfony\Component\HttpFoundation\Response;
class LinkController extends PageController
@ -14,4 +15,28 @@ class LinkController extends PageController
'pager' => $client->getPager($page),
]);
}
public function rss(PostParser $parser, TTRssClient $client): Response
{
$entities = $client->getPager(1);
$items = [];
foreach ($entities['items'] as $entity) {
$items[] = [
'title' => $entity['title'],
'guid' => md5($entity['link']),
'date' => $entity['timestamp'],
'link' => $entity['link'],
'description' => null,
'categories' => [],
];
}
$response = new Response();
$response->headers->set('Content-Type', 'text/xml');
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
'items' => $items,
], $response);
}
}

110
src/Entity/Page/RssPage.php Normal file
View file

@ -0,0 +1,110 @@
<?php
namespace App\Entity\Page;
use App\Core\Entity\Site\Page\Block;
use App\Core\Entity\Site\Page\Page;
use App\Core\Form\Site\Page\TextBlockType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @ORM\Entity
*/
class RssPage extends Page
{
public function buildForm(FormBuilderInterface $builder)
{
$builder->add(
'title',
TextBlockType::class,
[
'label' => 'Titre',
'required' => true,
'attr' => [
],
'constraints' => [
],
]
);
$builder->add(
'link',
TextBlockType::class,
[
'label' => 'Lien',
'required' => true,
'attr' => [
],
'constraints' => [
],
]
);
$builder->add(
'description',
TextBlockType::class,
[
'label' => 'Description',
'required' => true,
'attr' => [
],
'constraints' => [
],
]
);
$builder->add(
'language',
TextBlockType::class,
[
'label' => 'Langue',
'required' => true,
'attr' => [
],
'constraints' => [
],
]
);
}
public function setTitle(Block $block)
{
return $this->setBlock($block);
}
public function getTitle()
{
return $this->getBlock('title');
}
public function setLink(Block $block)
{
return $this->setBlock($block);
}
public function getLink()
{
return $this->getBlock('link');
}
public function setDescription(Block $block)
{
return $this->setBlock($block);
}
public function getDescription()
{
return $this->getBlock('description');
}
public function setLanguage(Block $block)
{
return $this->setBlock($block);
}
public function getLanguage()
{
return $this->getBlock('language');
}
}

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>{{ _page.title.value }}</title>
<link>{{ _page.link.value }}</link>
<description>{{ _page.description.value }}</description>
<language>{{ _page.language.value }}</language>
{% for item in items %}
<item>
<title><![CDATA[{{ item.title|raw }}]]></title>
<link>{{ item.link }}</link>
<description><![CDATA[{{ item.description|raw }}]]></description>
<guid isPermaLink="false">{{ item.guid }}</guid>
<pubDate>{{ item.date|date('r') }}</pubDate>
{% for category in item.categories %}
<category><![CDATA[{{ category }}]]></category>
{% endfor %}
</item>
{% endfor %}
</channel>
</rss>