‪TYPO3CMS  9.5
XmlSitemapRenderer.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ServerRequestInterface;
27 
33 {
37  protected ‪$configuration;
38 
42  protected ‪$view;
43 
44  public function ‪__construct()
45  {
46  $this->configuration = $this->‪getConfiguration();
47  $this->view = $this->‪getStandaloneView();
48  $this->view->assign(
49  'xslFile',
51  ‪ExtensionManagementUtility::extPath('seo', 'Resources/Public/CSS/Sitemap.xsl')
52  )
53  );
54  }
55 
60  public function ‪render(): string
61  {
62  // Inject request from globals until request will be available to cObj
63  $request = ‪$GLOBALS['TYPO3_REQUEST'];
64  $this->view->assign('type', ‪$GLOBALS['TSFE']->type);
65  if (!empty($sitemap = $request->getQueryParams()['sitemap'])) {
66  return $this->‪renderSitemap($request, $sitemap);
67  }
68 
69  return $this->‪renderIndex($request);
70  }
71 
76  protected function ‪renderIndex(ServerRequestInterface $request): string
77  {
78  $sitemaps = [];
79  foreach ($this->configuration['config']['xmlSitemap']['sitemaps'] ?? [] as $sitemap => $config) {
80  if (class_exists($config['provider']) &&
81  is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)) {
83  $provider = GeneralUtility::makeInstance(
84  $config['provider'],
85  $request,
86  $sitemap,
87  (array)$config['config']
88  );
89 
90  $pages = $provider->getNumberOfPages();
91 
92  for ($page = 0; $page < $pages; $page++) {
93  $sitemaps[] = [
94  'key' => $sitemap,
95  'page' => $page,
96  'lastMod' => $provider->getLastModified()
97  ];
98  }
99  }
100  }
101 
102  $this->view->assign('sitemaps', $sitemaps);
103  $this->view->setTemplate('Index');
104 
105  return $this->view->render();
106  }
107 
114  protected function ‪renderSitemap(ServerRequestInterface $request, string $sitemap): string
115  {
116  if (!empty($sitemapConfig = $this->configuration['config']['xmlSitemap']['sitemaps'][$sitemap])) {
117  if (class_exists($sitemapConfig['provider']) &&
118  is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) {
120  $provider = GeneralUtility::makeInstance(
121  $sitemapConfig['provider'],
122  $request,
123  $sitemap,
124  (array)$sitemapConfig['config']
125  );
126 
127  $items = $provider->getItems();
128 
129  $template = $sitemapConfig['config']['template'] ?: 'Sitemap';
130  $this->view->setTemplate($template);
131  $this->view->assign('items', $items);
132 
133  return $this->view->render();
134  }
135  throw new InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522);
136  }
137 
138  throw new InvalidConfigurationException('No valid configuration found for sitemap ' . $sitemap, 1535578569);
139  }
140 
144  protected function ‪getStandaloneView(): StandaloneView
145  {
146  ‪$view = GeneralUtility::makeInstance(StandaloneView::class);
147  ‪$view->‪setTemplateRootPaths($this->configuration['view']['templateRootPaths']);
148  ‪$view->‪setLayoutRootPaths($this->configuration['view']['layoutRootPaths']);
149  ‪$view->‪setPartialRootPaths($this->configuration['view']['partialRootPaths']);
150  ‪$view->‪setFormat('xml');
151 
152  return ‪$view;
153  }
154 
159  private function ‪getConfiguration(): array
160  {
161  $configurationManager = GeneralUtility::makeInstance(ObjectManager::class)
162  ->get(ConfigurationManagerInterface::class);
163 
164  return $configurationManager->getConfiguration(
166  'seo'
167  );
168  }
169 }
‪TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException
Definition: InvalidConfigurationException.php:19
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderIndex
‪string renderIndex(ServerRequestInterface $request)
Definition: XmlSitemapRenderer.php:74
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getConfiguration
‪array getConfiguration()
Definition: XmlSitemapRenderer.php:157
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:371
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:22
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getStandaloneView
‪TYPO3 CMS Fluid View StandaloneView getStandaloneView()
Definition: XmlSitemapRenderer.php:142
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:23
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:36
‪TYPO3\CMS\Seo\XmlSitemap
Definition: AbstractXmlSitemapDataProvider.php:4
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$configuration
‪array $configuration
Definition: XmlSitemapRenderer.php:36
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$view
‪TYPO3 CMS Fluid View StandaloneView $view
Definition: XmlSitemapRenderer.php:40
‪TYPO3\CMS\Fluid\View\StandaloneView\setFormat
‪setFormat($format)
Definition: StandaloneView.php:78
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\__construct
‪__construct()
Definition: XmlSitemapRenderer.php:42
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer
Definition: XmlSitemapRenderer.php:33
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\extPath
‪static string extPath($key, $script='')
Definition: ExtensionManagementUtility.php:149
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setLayoutRootPaths
‪setLayoutRootPaths(array $layoutRootPaths)
Definition: AbstractTemplateView.php:164
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setPartialRootPaths
‪setPartialRootPaths(array $partialRootPaths)
Definition: AbstractTemplateView.php:131
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderSitemap
‪string renderSitemap(ServerRequestInterface $request, string $sitemap)
Definition: XmlSitemapRenderer.php:112
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\render
‪string render()
Definition: XmlSitemapRenderer.php:58
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setTemplateRootPaths
‪setTemplateRootPaths(array $templateRootPaths)
Definition: AbstractTemplateView.php:111