forked from eliashaeussler/typo3-warming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SitemapLocator.php
163 lines (140 loc) · 5.06 KB
/
SitemapLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS extension "warming".
*
* Copyright (C) 2022 Elias Häußler <elias@haeussler.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace EliasHaeussler\Typo3Warming\Sitemap;
use EliasHaeussler\Typo3Warming\Cache\CacheManager;
use EliasHaeussler\Typo3Warming\Exception\UnsupportedConfigurationException;
use EliasHaeussler\Typo3Warming\Exception\UnsupportedSiteException;
use EliasHaeussler\Typo3Warming\Sitemap\Provider\ProviderInterface;
use EliasHaeussler\Typo3Warming\Traits\BackendUserAuthenticationTrait;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
/**
* SitemapLocator
*
* @author Elias Häußler <elias@haeussler.dev>
* @license GPL-2.0-or-later
*/
class SitemapLocator
{
use BackendUserAuthenticationTrait;
/**
* @var RequestFactory
*/
protected $requestFactory;
/**
* @var CacheManager
*/
protected $cacheManager;
/**
* @var ProviderInterface[]
*/
protected $providers = [];
/**
* @param ProviderInterface[] $providers
*/
public function __construct(RequestFactory $requestFactory, CacheManager $cacheManager, array $providers)
{
$this->requestFactory = $requestFactory;
$this->cacheManager = $cacheManager;
$this->providers = $providers;
ksort($this->providers);
$this->validateProviders();
}
/**
* @throws UnsupportedConfigurationException
* @throws UnsupportedSiteException
*/
public function locateBySite(Site $site, SiteLanguage $siteLanguage = null): SiteAwareSitemap
{
// Get sitemap from cache
if (($sitemapUrl = $this->cacheManager->get($site, $siteLanguage)) !== null) {
return new SiteAwareSitemap(new Uri($sitemapUrl), $site, $siteLanguage);
}
// Build and validate base URL
$baseUrl = null !== $siteLanguage ? $siteLanguage->getBase() : $site->getBase();
if ($baseUrl->getHost() === '') {
throw UnsupportedConfigurationException::forBaseUrl((string)$baseUrl);
}
// Resolve and validate sitemap
$sitemap = $this->resolveSitemap($site, $siteLanguage);
if ($sitemap === null) {
throw UnsupportedSiteException::forMissingSitemap($site);
}
// Store resolved sitemap in cache
$this->cacheManager->set($sitemap);
return $sitemap;
}
/**
* @return array<int, SiteAwareSitemap>
* @throws UnsupportedConfigurationException
* @throws UnsupportedSiteException
*/
public function locateAllBySite(Site $site): array
{
$sitemaps = [];
foreach ($site->getAvailableLanguages(static::getBackendUser()) as $siteLanguage) {
$sitemaps[$siteLanguage->getLanguageId()] = $this->locateBySite($site, $siteLanguage);
}
return $sitemaps;
}
public function siteContainsSitemap(Site $site, SiteLanguage $siteLanguage = null): bool
{
try {
$sitemap = $this->locateBySite($site, $siteLanguage);
$response = $this->requestFactory->request((string)$sitemap->getUri(), 'HEAD');
return $response->getStatusCode() < 400;
} catch (\Exception $e) {
return false;
}
}
protected function resolveSitemap(Site $site, SiteLanguage $siteLanguage = null): ?SiteAwareSitemap
{
foreach ($this->providers as $provider) {
if (($sitemap = $provider->get($site, $siteLanguage)) !== null) {
return $sitemap;
}
}
return null;
}
protected function validateProviders(): void
{
foreach ($this->providers as $provider) {
if (!\is_object($provider)) {
throw new \InvalidArgumentException(
sprintf('Providers must be of type object, "%s" given.', \gettype($provider)),
1619525071
);
}
if (!\in_array(ProviderInterface::class, class_implements($provider))) {
throw new \InvalidArgumentException(
sprintf(
'The given provider "%s" does not implement the interface "%s".',
\get_class($provider),
ProviderInterface::class
),
1619524996
);
}
}
}
}