SiteFinder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Opentalent\OtOptimizer\XClass\Core\Site;
  3. use Opentalent\OtCore\Exception\NoSuchWebsiteException;
  4. use Opentalent\OtCore\Website\OtWebsiteRepository;
  5. use TYPO3\CMS\Core\Exception\SiteNotFoundException;
  6. use TYPO3\CMS\Core\Site\Entity\Site;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Extbase\Object\ObjectManager;
  9. class SiteFinder extends \TYPO3\CMS\Core\Site\SiteFinder
  10. {
  11. private $fallback;
  12. /**
  13. * Fetches all existing configurations as Site objects
  14. */
  15. public function __construct($siteConfiguration = null, $fallback = true)
  16. {
  17. $this->fallback = $fallback;
  18. if ($this->fallback) {
  19. parent::__construct($siteConfiguration);
  20. }
  21. }
  22. /**
  23. * Find a site by given root page id
  24. *
  25. * @param int $rootPageId the page ID (default language)
  26. * @return Site
  27. * @throws SiteNotFoundException
  28. * @internal only for usage in some places for managing Site Configuration, might be removed without further notice
  29. */
  30. public function getSiteByRootPageId(int $rootPageId): Site
  31. {
  32. if ($this->fallback) {
  33. return parent::getSiteByRootPageId($rootPageId);
  34. }
  35. $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
  36. try {
  37. $website = $otWebsiteRepository->getWebsiteByPageUid($rootPageId);
  38. return $otWebsiteRepository->generateWebsiteConfiguration($website);
  39. } catch (NoSuchWebsiteException $e) {
  40. throw new SiteNotFoundException('No site found for root page id ' . $rootPageId, 1521668882);
  41. }
  42. }
  43. /**
  44. * Find a site by given identifier
  45. *
  46. * @param string $identifier
  47. * @return Site
  48. * @throws SiteNotFoundException
  49. */
  50. public function getSiteByIdentifier(string $identifier): Site
  51. {
  52. if ($this->fallback) {
  53. return parent::getSiteByIdentifier($identifier);
  54. }
  55. $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
  56. try {
  57. $website = $otWebsiteRepository->getWebsiteByConfigIdentifier($identifier);
  58. return $otWebsiteRepository->generateWebsiteConfiguration($website);
  59. } catch (NoSuchWebsiteException $e) {
  60. throw new SiteNotFoundException('No site found for identifier ' . $identifier, 1521716628);
  61. }
  62. }
  63. /**
  64. *
  65. *
  66. * @param int $pageId
  67. * @param array $rootLine
  68. * @param string|null $mountPointParameter
  69. * @return Site
  70. * @throws SiteNotFoundException
  71. */
  72. public function getSiteByPageId(int $pageId, array $rootLine = null, string $mountPointParameter = null): Site
  73. {
  74. if ($this->fallback) {
  75. return parent::getSiteByPageId($pageId, $rootLine, $mountPointParameter);
  76. }
  77. $otWebsiteRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
  78. try {
  79. $website = $otWebsiteRepository->getWebsiteByPageUid($pageId);
  80. return $otWebsiteRepository->generateWebsiteConfiguration($website);
  81. } catch (NoSuchWebsiteException $e) {
  82. throw new SiteNotFoundException('No site found in root line of page ' . $pageId, 1521716622);
  83. }
  84. }
  85. }