vendor/presta/sitemap-bundle/src/Controller/SitemapController.php line 48

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the PrestaSitemapBundle package.
  4. *
  5. * (c) PrestaConcept <https://prestaconcept.net>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Presta\SitemapBundle\Controller;
  11. use Presta\SitemapBundle\Service\GeneratorInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. /**
  15. * Provides action to render sitemap files
  16. */
  17. class SitemapController
  18. {
  19. /**
  20. * @var GeneratorInterface
  21. */
  22. private $generator;
  23. /**
  24. * Time to live of the response in seconds
  25. *
  26. * @var int
  27. */
  28. private $ttl;
  29. public function __construct(GeneratorInterface $generator, int $ttl)
  30. {
  31. $this->generator = $generator;
  32. $this->ttl = $ttl;
  33. }
  34. /**
  35. * list sitemaps
  36. *
  37. * @return Response
  38. */
  39. public function indexAction(): Response
  40. {
  41. $sitemapindex = $this->generator->fetch('root');
  42. if (!$sitemapindex) {
  43. throw new NotFoundHttpException('Not found');
  44. }
  45. $response = new Response($sitemapindex->toXml());
  46. $response->headers->set('Content-Type', 'text/xml');
  47. $response->setPublic();
  48. $response->setClientTtl($this->ttl);
  49. return $response;
  50. }
  51. /**
  52. * list urls of a section
  53. *
  54. * @param string $name
  55. *
  56. * @return Response
  57. */
  58. public function sectionAction(string $name): Response
  59. {
  60. $section = $this->generator->fetch($name);
  61. if (!$section) {
  62. throw new NotFoundHttpException('Not found');
  63. }
  64. $response = new Response($section->toXml());
  65. $response->headers->set('Content-Type', 'text/xml');
  66. $response->setPublic();
  67. $response->setClientTtl($this->ttl);
  68. return $response;
  69. }
  70. }