vendor/presta/sitemap-bundle/src/Service/Generator.php line 47

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\Service;
  11. use Presta\SitemapBundle\Sitemap\Urlset;
  12. use Presta\SitemapBundle\Sitemap\XmlConstraint;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. /**
  16. * Sitemap generator.
  17. */
  18. class Generator extends AbstractGenerator implements GeneratorInterface
  19. {
  20. /**
  21. * @var UrlGeneratorInterface
  22. */
  23. protected $router;
  24. /**
  25. * @param EventDispatcherInterface $dispatcher
  26. * @param UrlGeneratorInterface $router
  27. * @param int|null $itemsBySet
  28. */
  29. public function __construct(
  30. EventDispatcherInterface $dispatcher,
  31. UrlGeneratorInterface $router,
  32. int $itemsBySet = null
  33. ) {
  34. parent::__construct($dispatcher, $itemsBySet, $router);
  35. $this->router = $router;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function fetch(string $name): ?XmlConstraint
  41. {
  42. if ('root' === $name) {
  43. $this->populate();
  44. return $this->getRoot();
  45. }
  46. $this->populate($name);
  47. if (array_key_exists($name, $this->urlsets)) {
  48. return $this->urlsets[$name];
  49. }
  50. return null;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. protected function newUrlset(string $name, \DateTimeInterface $lastmod = null): Urlset
  56. {
  57. return new Urlset(
  58. $this->router->generate(
  59. 'PrestaSitemapBundle_section',
  60. ['name' => $name, '_format' => 'xml'],
  61. UrlGeneratorInterface::ABSOLUTE_URL
  62. ),
  63. $lastmod
  64. );
  65. }
  66. }