Passed
Push — master ( e190d9...c38fc4 )
by
unknown
15:05
created

XmlSitemapRenderer::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\TypoScript\TypoScriptService;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\PathUtility;
24
use TYPO3\CMS\Fluid\View\StandaloneView;
25
use TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException;
26
27
/**
28
 * Class to render the XML Sitemap to be used as a UserFunction
29
 * @internal this class is not part of TYPO3's Core API.
30
 */
31
class XmlSitemapRenderer
32
{
33
    /**
34
     * @var array
35
     */
36
    protected $configuration;
37
38
    /**
39
     * @var StandaloneView
40
     */
41
    protected $view;
42
43
    protected TypoScriptService $typoScriptService;
44
45
    public function __construct(TypoScriptService $typoScriptService)
46
    {
47
        $this->typoScriptService = $typoScriptService;
48
    }
49
50
    protected function initialize(array $fullConfiguration)
51
    {
52
        $this->configuration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($fullConfiguration['plugin.']['tx_seo.'] ?? []);
53
        $this->view = $this->getStandaloneView();
54
    }
55
56
    /**
57
     * @param string $_ unused, but needed as this is called via userfunc and passes a string as first parameter
58
     * @param array $typoScriptConfiguration TypoScript configuration specified in USER Content Object
59
     * @param ServerRequestInterface $request
60
     * @return string
61
     * @throws InvalidConfigurationException
62
     */
63
    public function render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request): string
64
    {
65
        $this->initialize($GLOBALS['TSFE']->tmpl->setup);
66
        $this->view->assign('type', $GLOBALS['TSFE']->type);
67
        $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
68
        $this->view->assign('xslFile', $this->getXslFilePath($sitemapType));
69
        if (!empty($sitemap = $request->getQueryParams()['sitemap'])) {
70
            return $this->renderSitemap($request, $sitemap, $sitemapType);
71
        }
72
73
        return $this->renderIndex($request, $sitemapType);
74
    }
75
76
    /**
77
     * @param ServerRequestInterface $request
78
     * @param string $sitemapType
79
     * @return string
80
     */
81
    protected function renderIndex(ServerRequestInterface $request, string $sitemapType): string
82
    {
83
        $sitemaps = [];
84
        foreach ($this->configuration['config'][$sitemapType]['sitemaps'] ?? [] as $sitemap => $config) {
85
            if (!empty($config['provider']) && is_string($config['provider'])
86
                && class_exists($config['provider'])
87
                && is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)
88
            ) {
89
                /** @var XmlSitemapDataProviderInterface $provider */
90
                $provider = GeneralUtility::makeInstance(
91
                    $config['provider'],
92
                    $request,
93
                    $sitemap,
94
                    (array)$config['config']
95
                );
96
97
                $pages = $provider->getNumberOfPages();
98
99
                for ($page = 0; $page < $pages; $page++) {
100
                    $sitemaps[] = [
101
                        'key' => $sitemap,
102
                        'page' => $page,
103
                        'lastMod' => $provider->getLastModified()
104
                    ];
105
                }
106
            }
107
        }
108
109
        $this->view->assign('sitemapType', $sitemapType);
110
        $this->view->assign('sitemaps', $sitemaps);
111
        $this->view->setTemplate('Index');
112
113
        return $this->view->render();
114
    }
115
116
    /**
117
     * @param ServerRequestInterface $request
118
     * @param string $sitemap
119
     * @param string $sitemapType
120
     * @return string
121
     * @throws InvalidConfigurationException
122
     */
123
    protected function renderSitemap(ServerRequestInterface $request, string $sitemap, string $sitemapType): string
124
    {
125
        if (!empty($sitemapConfig = $this->configuration['config'][$sitemapType]['sitemaps'][$sitemap])) {
126
            if (class_exists($sitemapConfig['provider']) &&
127
                is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) {
128
                /** @var XmlSitemapDataProviderInterface $provider */
129
                $provider = GeneralUtility::makeInstance(
130
                    $sitemapConfig['provider'],
131
                    $request,
132
                    $sitemap,
133
                    (array)$sitemapConfig['config']
134
                );
135
136
                $items = $provider->getItems();
137
138
                $template = $sitemapConfig['config']['template'] ?: 'Sitemap';
139
                $this->view->setTemplate($template);
140
                $this->view->assign('xslFile', $this->getXslFilePath($sitemapType, $sitemap));
141
                $this->view->assign('items', $items);
142
                $this->view->assign('sitemapType', $sitemapType);
143
144
                return $this->view->render();
145
            }
146
            throw new InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522);
147
        }
148
149
        throw new InvalidConfigurationException('No valid configuration found for sitemap ' . $sitemap, 1535578569);
150
    }
151
152
    protected function getStandaloneView(): StandaloneView
153
    {
154
        $view = GeneralUtility::makeInstance(StandaloneView::class);
155
        $view->setTemplateRootPaths($this->configuration['view']['templateRootPaths']);
156
        $view->setLayoutRootPaths($this->configuration['view']['layoutRootPaths']);
157
        $view->setPartialRootPaths($this->configuration['view']['partialRootPaths']);
158
        $view->setFormat('xml');
159
160
        return $view;
161
    }
162
163
    /**
164
     * @param string|null $sitemapType
165
     * @param string|null $sitemap
166
     * @return string
167
     */
168
    protected function getXslFilePath(string $sitemapType = null, string $sitemap = null): string
169
    {
170
        $path = $this->configuration['config']['xslFile'] ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
171
        $path = ($sitemapType !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps']['xslFile'] ?? $path) : $path;
172
        $path = ($sitemapType !== null && $sitemap !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps'][$sitemap]['config']['xslFile'] ?? $path) : $path;
173
        return PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
174
    }
175
}
176