|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace App\Controller; |
| 13 | + |
| 14 | +use App\Service\LiveDemoRepository; |
| 15 | +use App\Service\UxPackageRepository; |
| 16 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\Routing\Attribute\Route; |
| 20 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 21 | + |
| 22 | +final class SitemapController extends AbstractController |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private readonly UxPackageRepository $uxPackageRepository, |
| 26 | + private readonly LiveDemoRepository $liveDemoRepository, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + #[Route(path: '/sitemap.xml', name: 'app_sitemap')] |
| 31 | + public function __invoke(Request $request): Response |
| 32 | + { |
| 33 | + $response = $this->render('sitemap.xml.twig', [ |
| 34 | + 'urls' => $this->getSitemapUrls(), |
| 35 | + ]); |
| 36 | + $response->headers->set('Content-Type', 'application/xml'); |
| 37 | + |
| 38 | + return $response; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return iterable<string> |
| 43 | + */ |
| 44 | + private function getSitemapUrls(): iterable |
| 45 | + { |
| 46 | + // Static pages |
| 47 | + yield $this->generateAbsoluteUrl('app_homepage'); |
| 48 | + yield $this->generateAbsoluteUrl('app_packages'); |
| 49 | + yield $this->generateAbsoluteUrl('app_icons'); |
| 50 | + yield $this->generateAbsoluteUrl('app_demos'); |
| 51 | + yield $this->generateAbsoluteUrl('app_changelog'); |
| 52 | + |
| 53 | + // UX Packages |
| 54 | + foreach ($this->uxPackageRepository->findAll() as $package) { |
| 55 | + yield $this->generateAbsoluteUrl($package->getRoute()); |
| 56 | + } |
| 57 | + |
| 58 | + // Live Demos |
| 59 | + foreach ($this->liveDemoRepository->findAll() as $demo) { |
| 60 | + yield $this->generateAbsoluteUrl($demo->getRoute()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private function generateAbsoluteUrl(string $route, array $parameters = []): string |
| 65 | + { |
| 66 | + return $this->generateUrl($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL); |
| 67 | + } |
| 68 | +} |
0 commit comments