src/Controller/SitemapController.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Repository\ContentRepository;
  5. use App\Repository\Menu\ItemRepository;
  6. use App\Router\Generator\DomainUrlGenerator;
  7. use App\Service\CacheHelper;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Cache\CacheItem;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  15. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  16. class SitemapController extends AbstractController
  17. {
  18.     protected ItemRepository $menuRepo;
  19.     protected ContentRepository $contentRepo;
  20.     protected DomainUrlGenerator $urlGenerator;
  21.     protected Request $request;
  22.     protected TagAwareCacheInterface $cache;
  23.     public function __construct(
  24.         ItemRepository $menuRepo,
  25.         ContentRepository $contentRepo,
  26.         DomainUrlGenerator $urlGenerator,
  27.         RequestStack $requestStack,
  28.         TagAwareCacheInterface $appCache
  29.     ) {
  30.         $this->menuRepo = $menuRepo;
  31.         $this->contentRepo = $contentRepo;
  32.         $this->urlGenerator = $urlGenerator;
  33.         $this->request = $requestStack->getCurrentRequest() ?? new Request();
  34.         $this->cache = $appCache;
  35.     }
  36.     public function sitemap(string $format, CacheHelper $cacheHelper): Response
  37.     {
  38.         if ($cacheHelper->cacheEnabled()) {
  39.             return $this->cache->get("sitemap.$format", function (CacheItem $item) use ($format) {
  40.                 $item->expiresAfter(new \DateInterval('P1D'));
  41.                 return $this->getResponseByFormat($format);
  42.             });
  43.         } else {
  44.             return $this->getResponseByFormat($format);
  45.         }
  46.     }
  47.     protected function getResponseByFormat(string $format): Response
  48.     {
  49.         $links = $this->menuRepo->findAllItemsStaticPaths();
  50.         $this->prependUrlsWithSchemeAndHost($links);
  51.         $links += $this->urlGenerator->matchContentArray(
  52.             $this->contentRepo->findAllContentWithRoutesForGenerator(),
  53.             true,
  54.             ['full_url' => true]
  55.         );
  56.         switch ($format) {
  57.             case 'xml':
  58.                 return $this->generateXml($links);
  59.             default:
  60.                 throw new NotFoundHttpException('Unsupported format');
  61.         }
  62.     }
  63.     protected function generateXml(array $links): Response
  64.     {
  65.         $encoder = new XmlEncoder();
  66.         $data = [
  67.             '@xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
  68.             'url' => [],
  69.         ];
  70.         foreach ($links as $l)
  71.             $data['url'][] = ['loc' => $l];
  72.         $response = $encoder->encode($data, 'xml' ,[
  73.             'xml_encoding' => 'utf-8',
  74.             'xml_root_node_name' => 'urlset',
  75.         ]);
  76.         return new Response($response, 200, ['Content-type' => 'application/xml']);
  77.     }
  78.     protected function prependUrlsWithSchemeAndHost(array &$urls): void
  79.     {
  80.         $host = $this->request->getSchemeAndHttpHost();
  81.         foreach ($urls as &$url)
  82.             $url = $host . $url;
  83.     }
  84. }