src/Hitso/Bundle/SeoBundle/Controller/Front/SitemapController.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\SeoBundle\Controller\Front;
  4. use Hitso\Bundle\SeoBundle\Service\SitemapGenerator;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Contracts\Cache\ItemInterface;
  10. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  11. /**
  12.  * Class SitemapController
  13.  *
  14.  * @package Hitso\Bundle\SeoBundle\Controller\Front
  15.  */
  16. class SitemapController extends Controller
  17. {
  18.     protected $generator;
  19.     protected $cache;
  20.     protected $cacheNamePrefix '';
  21.     protected $page;
  22.     protected $section;
  23.     protected $sharedMaxAge 3600;
  24.     public function __construct(SitemapGenerator $generator)
  25.     {
  26.         $this->generator $generator;
  27.         $this->cache     = new FilesystemAdapter();
  28.         $cacheNamePrefix = isset($_ENV['APP_NAME'])?$_ENV['APP_NAME'].'_':'';
  29.         $cacheNamePrefix .= isset($_ENV['APP_ENV'])?$_ENV['APP_ENV'].'_':'';
  30.         $this->cacheNamePrefix $cacheNamePrefix;
  31.     }
  32.     /**
  33.      * @Route("/sitemap.xml", name="hitso_sitemap_front_index")
  34.      */
  35.     public function indexAction(ParameterBagInterface $params): Response
  36.     {
  37.         $this->sharedMaxAge $params->get('hitso.sitemap.cache.shared_max_age');
  38.         $index $this->cache->get($this->cacheNamePrefix 'sitemap_index', function (ItemInterface $item) {
  39.             $item->expiresAfter($this->sharedMaxAge);
  40.             return $this->generator->generateIndex();
  41.         });
  42.         return $this->render('HitsoSeoBundle::Sitemap/index.xml.twig', [
  43.             'sitemap_index' => $index,
  44.         ], $this->getEmptyXmlResponse($params));
  45.     }
  46.     /**
  47.      * @Route("/sitemap.{section}.{page}.xml", name="hitso_sitemap_front_section")
  48.      */
  49.     public function sectionAction(SitemapGenerator $generatorstring $sectionint $pageParameterBagInterface $params): Response
  50.     {
  51.         $this->sharedMaxAge $params->get('hitso.sitemap.cache.shared_max_age');
  52.         $this->section      $section;
  53.         $this->page         $page;
  54.         $sitemapSectionPage $this->cache->get($this->cacheNamePrefix 'sitemap_section_' $section '_' $page, function (ItemInterface $item) {
  55.             $item->expiresAfter($this->sharedMaxAge);
  56.             return $this->generator->generateSectionPage($this->section$this->page);
  57.         });
  58.         if ($sitemapSectionPage->getCount() === 0) {
  59.             return new Response('Requested page is out of range'Response::HTTP_NOT_FOUND);
  60.         }
  61.         return $this->render('HitsoSeoBundle::Sitemap/section.xml.twig', [
  62.             'sitemap_section' => $sitemapSectionPage,
  63.         ], $this->getEmptyXmlResponse($params));
  64.     }
  65.     private function getEmptyXmlResponse(ParameterBagInterface $params): Response
  66.     {
  67.         $response     = new Response(nullResponse::HTTP_OK, [
  68.             'Content-type' => 'text/xml',
  69.             'X-Robots-Tag' => 'noindex',
  70.         ]);
  71.         $sharedMaxAge $params->get('hitso.sitemap.cache.shared_max_age');
  72.         $response->setSharedMaxAge($sharedMaxAge);
  73.         return $response;
  74.     }
  75. }