vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Sitemap/XmlSitemapRenderer.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\WebsiteBundle\Sitemap;
  11. use Twig\Environment;
  12. /**
  13. * Render sitemap in xml-format.
  14. */
  15. class XmlSitemapRenderer implements XmlSitemapRendererInterface
  16. {
  17. /**
  18. * @var SitemapProviderPoolInterface
  19. */
  20. private $sitemapProviderPool;
  21. /**
  22. * @var Environment
  23. */
  24. private $engine;
  25. public function __construct(
  26. SitemapProviderPoolInterface $sitemapProviderPool,
  27. Environment $engine
  28. ) {
  29. $this->sitemapProviderPool = $sitemapProviderPool;
  30. $this->engine = $engine;
  31. }
  32. public function renderIndex($scheme, $host)
  33. {
  34. if (!$this->needsIndex($scheme, $host)) {
  35. return null;
  36. }
  37. return $this->render(
  38. '@SuluWebsite/Sitemap/sitemap-index.xml.twig',
  39. ['sitemaps' => $this->sitemapProviderPool->getIndex($scheme, $host), 'domain' => $host, 'scheme' => $scheme]
  40. );
  41. }
  42. public function renderSitemap($alias, $page, $scheme, $host)
  43. {
  44. if (!$this->sitemapProviderPool->hasProvider($alias)) {
  45. return null;
  46. }
  47. $provider = $this->sitemapProviderPool->getProvider($alias);
  48. if ($provider->getMaxPage($scheme, $host) < $page) {
  49. return null;
  50. }
  51. $entries = $provider->build($page, $scheme, $host);
  52. return $this->render(
  53. '@SuluWebsite/Sitemap/sitemap.xml.twig',
  54. [
  55. 'domain' => $host,
  56. 'scheme' => $scheme,
  57. 'entries' => $entries,
  58. ]
  59. );
  60. }
  61. /**
  62. * Renders a view.
  63. *
  64. * @param string $view The view name
  65. * @param array $parameters An array of parameters to pass to the view
  66. *
  67. * @return string
  68. */
  69. private function render($view, array $parameters = [])
  70. {
  71. return $this->engine->render($view, $parameters);
  72. }
  73. /**
  74. * Returns true if a index exists.
  75. *
  76. * @return bool
  77. */
  78. private function needsIndex($scheme, $host)
  79. {
  80. return 1 < \array_reduce(
  81. $this->sitemapProviderPool->getIndex($scheme, $host),
  82. function($v1, Sitemap $v2) {
  83. return $v1 + $v2->getMaxPage();
  84. }
  85. );
  86. }
  87. }