Passed
Push — master ( 197fdc...5bd1f6 )
by
unknown
20:00
created

XmlSitemapRenderer::renderIndex()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 33
rs 8.6666
cc 7
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Seo\XmlSitemap;
19
20
use Psr\Http\Message\ServerRequestInterface;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Core\Utility\PathUtility;
23
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
24
use TYPO3\CMS\Extbase\Object\Exception;
25
use TYPO3\CMS\Extbase\Object\ObjectManager;
26
use TYPO3\CMS\Fluid\View\StandaloneView;
27
use TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException;
28
29
/**
30
 * Class to render the XML Sitemap to be used as a UserFunction
31
 * @internal this class is not part of TYPO3's Core API.
32
 */
33
class XmlSitemapRenderer
34
{
35
    /**
36
     * @var array
37
     */
38
    protected $configuration;
39
40
    /**
41
     * @var StandaloneView
42
     */
43
    protected $view;
44
45
    /**
46
     * XmlSitemapRenderer constructor.
47
     * @throws Exception
48
     */
49
    public function __construct()
50
    {
51
        $this->configuration = $this->getConfiguration();
52
        $this->view = $this->getStandaloneView();
53
    }
54
55
    /**
56
     * @param string $_ unused, but needed as this is called via userfunc and passes a string as first parameter
57
     * @param array $typoScriptConfiguration TypoScript configuration specified in USER Content Object
58
     * @return string
59
     * @throws InvalidConfigurationException
60
     */
61
    public function render(string $_, array $typoScriptConfiguration): string
62
    {
63
        // Inject request from globals until request will be available to cObj
64
        $request = $GLOBALS['TYPO3_REQUEST'];
65
        $this->view->assign('type', $GLOBALS['TSFE']->type);
66
        $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
67
        $this->view->assign('xslFile', $this->getXslFilePath($sitemapType));
68
        if (!empty($sitemap = $request->getQueryParams()['sitemap'])) {
69
            return $this->renderSitemap($request, $sitemap, $sitemapType);
70
        }
71
72
        return $this->renderIndex($request, $sitemapType);
73
    }
74
75
    /**
76
     * @param ServerRequestInterface $request
77
     * @param string $sitemapType
78
     * @return string
79
     */
80
    protected function renderIndex(ServerRequestInterface $request, string $sitemapType): string
81
    {
82
        $sitemaps = [];
83
        foreach ($this->configuration['config'][$sitemapType]['sitemaps'] ?? [] as $sitemap => $config) {
84
            if (!empty($config['provider']) && is_string($config['provider'])
85
                && class_exists($config['provider'])
86
                && is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)
87
            ) {
88
                /** @var XmlSitemapDataProviderInterface $provider */
89
                $provider = GeneralUtility::makeInstance(
90
                    $config['provider'],
91
                    $request,
92
                    $sitemap,
93
                    (array)$config['config']
94
                );
95
96
                $pages = $provider->getNumberOfPages();
97
98
                for ($page = 0; $page < $pages; $page++) {
99
                    $sitemaps[] = [
100
                        'key' => $sitemap,
101
                        'page' => $page,
102
                        'lastMod' => $provider->getLastModified()
103
                    ];
104
                }
105
            }
106
        }
107
108
        $this->view->assign('sitemapType', $sitemapType);
109
        $this->view->assign('sitemaps', $sitemaps);
110
        $this->view->setTemplate('Index');
111
112
        return $this->view->render();
113
    }
114
115
    /**
116
     * @param ServerRequestInterface $request
117
     * @param string $sitemap
118
     * @param string $sitemapType
119
     * @return string
120
     * @throws InvalidConfigurationException
121
     */
122
    protected function renderSitemap(ServerRequestInterface $request, string $sitemap, string $sitemapType): string
123
    {
124
        if (!empty($sitemapConfig = $this->configuration['config'][$sitemapType]['sitemaps'][$sitemap])) {
125
            if (class_exists($sitemapConfig['provider']) &&
126
                is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) {
127
                /** @var XmlSitemapDataProviderInterface $provider */
128
                $provider = GeneralUtility::makeInstance(
129
                    $sitemapConfig['provider'],
130
                    $request,
131
                    $sitemap,
132
                    (array)$sitemapConfig['config']
133
                );
134
135
                $items = $provider->getItems();
136
137
                $template = $sitemapConfig['config']['template'] ?: 'Sitemap';
138
                $this->view->setTemplate($template);
139
                $this->view->assign('xslFile', $this->getXslFilePath($sitemapType, $sitemap));
140
                $this->view->assign('items', $items);
141
                $this->view->assign('sitemapType', $sitemapType);
142
143
                return $this->view->render();
144
            }
145
            throw new InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522);
146
        }
147
148
        throw new InvalidConfigurationException('No valid configuration found for sitemap ' . $sitemap, 1535578569);
149
    }
150
151
    /**
152
     * @return StandaloneView
153
     */
154
    protected function getStandaloneView(): StandaloneView
155
    {
156
        $view = GeneralUtility::makeInstance(StandaloneView::class);
157
        $view->setTemplateRootPaths($this->configuration['view']['templateRootPaths']);
158
        $view->setLayoutRootPaths($this->configuration['view']['layoutRootPaths']);
159
        $view->setPartialRootPaths($this->configuration['view']['partialRootPaths']);
160
        $view->setFormat('xml');
161
162
        return $view;
163
    }
164
165
    /**
166
     * @param string|null $sitemapType
167
     * @param string|null $sitemap
168
     * @return string
169
     */
170
    protected function getXslFilePath(string $sitemapType = null, string $sitemap = null): string
171
    {
172
        $path = $this->configuration['config']['xslFile'] ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
173
        $path = ($sitemapType !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps']['xslFile'] ?? $path) : $path;
174
        $path = ($sitemapType !== null && $sitemap !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps'][$sitemap]['config']['xslFile'] ?? $path) : $path;
175
        return PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
176
    }
177
178
    /**
179
     * Get the whole typoscript array
180
     * @return array
181
     * @throws Exception
182
     */
183
    private function getConfiguration(): array
184
    {
185
        $configurationManager = GeneralUtility::makeInstance(ObjectManager::class)
186
            ->get(ConfigurationManagerInterface::class);
187
188
        return $configurationManager->getConfiguration(
189
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
190
            'seo'
191
        );
192
    }
193
}
194