src/Controller/BlogController.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Services\BlogReader;
  4. use App\Services\SiteConfig;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. class BlogController extends AbstractController {
  10.     private BlogReader $blogReader;
  11.     private SiteConfig $config;
  12.     function __construct(SiteConfig $configBlogReader $blogReader) {
  13.         $this->blogReader $blogReader;
  14.         $this->config $config;
  15.     }
  16.     function index(int $page ): Response {
  17.         $postCount $this->blogReader->postCount();
  18.         
  19.         
  20.         $PAGE_LINKS 5;        
  21.         $pageCount ceil($postCount $this->config->getBlogIndexPageSize() );
  22.         $pages = [];
  23.         if($page 1) {
  24.             $pages[] = ['<<'$this->generateUrl('blog', ['page' => strval(1)])      , ''] ;
  25.             $pages[] = ['<' $this->generateUrl('blog', ['page' => strval($page-1)]), ''];
  26.         }
  27.         /*              V                        
  28.             1  2  3  4  5  6  7  8  9  10
  29.                   1  2  3  4  5
  30.                   |<--- 5 --->|
  31.                   1  2  3  4  5  6  7  8  9  10
  32.                   1  2  3  4  5
  33.                   |<--- 5 --->|
  34.         */
  35.         $pagerMiddle ceil($PAGE_LINKS/2); /*pagerLinks = 5; $pagerMiddle = 3; pageLinks = 6; $pagerMiddle = 3; */
  36.         //                   3
  37.         $p = ($page <= $pagerMiddle) ? : ($page $pagerMiddle 1);
  38.         for($i 0$i $PAGE_LINKS && $p <= $pageCount; ++$i$p++) {
  39.             $classes = ($p == $page) ? 'current' '';
  40.             $pages[] = [ strval($p), $this->generateUrl('blog', ['page' => strval($p) ]), $classes];
  41.         }
  42.         if ($page $pageCount) {
  43.             $pages[] = ['>'$this->generateUrl('blog', ['page' => strval($page+1)])    , ''];
  44.             $pages[] = ['>>'$this->generateUrl('blog', ['page' => strval($pageCount)]), ''];
  45.         }
  46.         $posts $this->blogReader->getExcerpts($page$this->config->getBlogIndexPageSize());
  47.         $contents $this->renderView"default/blog.index.html.twig", [
  48.             'posts' => $posts,
  49.             'pages' => $pages,
  50.             'pageCount' => $pageCount,
  51.         ]);
  52.         return new Response($contents);    
  53.     }
  54.     function post(string $slug ): Response {
  55.         $contents $this->renderView"default/blog.post.html.twig", [
  56.             'post' => $posts $this->blogReader->getPostbySlug($slug)
  57.         ]);
  58.         return new Response($contents);    
  59.     }
  60. }