vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Sitemap/SitemapGenerator.php line 61

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 Sulu\Component\Content\Query\ContentQueryBuilderInterface;
  12. use Sulu\Component\Content\Query\ContentQueryExecutorInterface;
  13. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  14. /**
  15. * Generates a sitemap structure for xml or html.
  16. */
  17. class SitemapGenerator implements SitemapGeneratorInterface
  18. {
  19. /**
  20. * @var ContentQueryExecutorInterface
  21. */
  22. private $contentQuery;
  23. /**
  24. * @var WebspaceManagerInterface
  25. */
  26. private $webspaceManager;
  27. /**
  28. * @var ContentQueryBuilderInterface
  29. */
  30. private $contentQueryBuilder;
  31. public function __construct(
  32. ContentQueryExecutorInterface $contentQuery,
  33. WebspaceManagerInterface $webspaceManager,
  34. ContentQueryBuilderInterface $contentQueryBuilder
  35. ) {
  36. $this->contentQuery = $contentQuery;
  37. $this->webspaceManager = $webspaceManager;
  38. $this->contentQueryBuilder = $contentQueryBuilder;
  39. }
  40. public function generateAllLocals($webspaceKey, $flat = false)
  41. {
  42. $webSpaceSitemap = $this->getWebspaceSitemap($webspaceKey);
  43. $webSpaceSitemap->setSitemap(
  44. $this->generateByLocals($webspaceKey, $webSpaceSitemap->getLocalizations(), $flat)
  45. );
  46. return $webSpaceSitemap;
  47. }
  48. public function generate($webspaceKey, $locale, $flat = false)
  49. {
  50. $webspaceSitemapInformation = $this->getWebspaceSitemap($webspaceKey);
  51. $sitemap = $this->generateByLocals($webspaceKey, [$locale], $flat);
  52. if (1 === \count($sitemap) && !$flat) {
  53. $sitemap = $sitemap[0];
  54. }
  55. $webspaceSitemapInformation->setSitemap(
  56. $sitemap
  57. );
  58. return $webspaceSitemapInformation;
  59. }
  60. /**
  61. * @param string $webspaceKey
  62. *
  63. * @return WebspaceSitemap
  64. */
  65. private function getWebspaceSitemap($webspaceKey)
  66. {
  67. $webspace = $this->webspaceManager->findWebspaceByKey($webspaceKey);
  68. $webspaceSitemap = new WebspaceSitemap();
  69. $webspaceSitemap->setWebspaceKey($webspace->getKey());
  70. $defaultLocalization = $webspace->getDefaultLocalization();
  71. if ($defaultLocalization) {
  72. $webspaceSitemap->setDefaultLocalization($defaultLocalization->getLocale());
  73. }
  74. foreach ($webspace->getAllLocalizations() as $localization) {
  75. $webspaceSitemap->addLocalization($localization->getLocale());
  76. }
  77. return $webspaceSitemap;
  78. }
  79. /**
  80. * @param string $webspaceKey
  81. * @param array $locales
  82. * @param bool $flat
  83. *
  84. * @return array
  85. */
  86. private function generateByLocals($webspaceKey, $locales, $flat = false)
  87. {
  88. return $this->contentQuery->execute(
  89. $webspaceKey,
  90. $locales,
  91. $this->contentQueryBuilder,
  92. $flat,
  93. -1,
  94. null,
  95. null,
  96. true
  97. );
  98. }
  99. }