1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Controller
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\Response
;
17 use Rapsys\BlogBundle\Entity\Article
;
18 use Rapsys\BlogBundle\Entity\Keyword
;
23 class ArticleController
extends AbstractController
{
29 * @param Request $request The request instance
30 * @return Response The rendered view
32 public function index(Request
$request): Response
{
34 if ($count = $this->doctrine
->getRepository(Article
::class)->findCountAsInt()) {
35 //Negative page or over page
36 if (($page = (int) $request->get('page', 0)) < 0 || $page > $count / $this->limit
) {
38 throw $this->createNotFoundException($this->translator
->trans('Unable to find articles (page: %page%)', ['%page%' => $page]));
42 if (empty($this->context
['articles'] = $this->doctrine
->getRepository(Article
::class)->findAllAsArray($page, $this->limit
))) {
44 throw $this->createNotFoundException($this->translator
->trans('Unable to find articles'));
50 $this->context
['head']['prev'] = $this->context
['articles_prev'] = $this->generateUrl($request->attributes
->get('_route'), ['page' => $page - 1]+
$request->attributes
->get('_route_params'));
54 if ($count > ($page +
1) * $this->limit
) {
56 $this->context
['head']['next'] = $this->context
['articles_next'] = $this->generateUrl($request->attributes
->get('_route'), ['page' => $page +
1]+
$request->attributes
->get('_route_params'));
60 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['articles']));
64 $this->context
['articles'] = [];
67 $this->modified
= new \
DateTime('-1 year');
71 $response = new Response();
74 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
76 $response->setLastModified(new \
DateTime('-1 year'));
79 $response->setPrivate();
83 //XXX: only for public to force revalidation by last modified
84 $response->setEtag(md5(serialize($this->context
['articles'])));
87 $response->setLastModified($this->modified
);
90 $response->setPublic();
92 //Without role and modification
93 if ($response->isNotModified($request)) {
100 $this->context
['head']['keywords'] = implode(
102 //Use closure to extract each unique article keywords sorted
107 //Iterate on articles
110 if (!empty($a['keywords'])) {
111 //Iterate on keywords
112 foreach($a['keywords'] as $k) {
114 $r[$k['title']] = $k['title'];
124 })($this->context
['articles'])
128 $this->context
['title'] = $this->translator
->trans('Articles list');
131 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary article listing');
134 return $this->render('@RapsysBlog/article/index.html.twig', $this->context
, $response);
140 * Display article and keywords
142 * @param Request $request The request instance
143 * @param integer $id The article id
144 * @param ?string $slug The article slug
145 * @return Response The rendered view
147 public function view(Request
$request, int $id, ?string $slug): Response
{
149 if (empty($this->context
['article'] = $this->doctrine
->getRepository(Article
::class)->findByIdAsArray($id))) {
151 throw $this->createNotFoundException($this->translator
->trans('Unable to find article: %id%', ['%id%' => $id]));
155 if ($slug !== $this->context
['article']['slug']) {
156 //Redirect on correctly spelled article
157 return $this->redirectToRoute('rapsys_blog_article_view', ['id' => $this->context
['article']['id'], 'slug' => $this->context
['article']['slug']], Response
::HTTP_MOVED_PERMANENTLY
);
161 $this->modified
= $this->context
['article']['modified'];
164 $response = new Response();
167 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
169 $response->setLastModified(new \
DateTime('-1 year'));
172 $response->setPrivate();
173 //Without logged user
176 //XXX: only for public to force revalidation by last modified
177 $response->setEtag(md5(serialize($this->context
['article'])));
180 $response->setLastModified($this->modified
);
183 $response->setPublic();
185 //Without role and modification
186 if ($response->isNotModified($request)) {
187 //Return 304 response
193 $this->context
['head']['keywords'] = implode(
199 $this->context
['article']['keywords']
204 $this->context
['head']['keywords'] = implode(
206 //Use closure to extract each unique article keywords sorted
212 if (!empty($a['keywords'])) {
213 //Iterate on keywords
214 foreach($a['keywords'] as $k) {
216 $r[$k['title']] = $k['title'];
225 })($this->context
['article'])
229 $this->context
['title'] = $this->context
['article']['title'];
232 $this->context
['description'] = $this->context
['article']['description'];
235 return $this->render('@RapsysBlog/article/view.html.twig', $this->context
, $response);