forked from spatie/laravel-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSitemapGenerator.php
202 lines (149 loc) · 5.32 KB
/
SitemapGenerator.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Spatie\Sitemap;
use Closure;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Support\Collection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Spatie\Browsershot\Browsershot;
use Spatie\Crawler\Crawler;
use Spatie\Crawler\CrawlProfiles\CrawlProfile;
use Spatie\Sitemap\Crawler\Observer;
use Spatie\Sitemap\Crawler\Profile;
use Spatie\Sitemap\Tags\Url;
class SitemapGenerator
{
protected Collection $sitemaps;
protected Uri $urlToBeCrawled;
protected Crawler $crawler;
/** @var callable */
protected $shouldCrawl;
/** @var callable */
protected $hasCrawled;
protected int $concurrency = 10;
protected bool | int $maximumTagsPerSitemap = false;
protected ?int $maximumCrawlCount = null;
public static function create(string $urlToBeCrawled): static
{
return app(static::class)->setUrl($urlToBeCrawled);
}
public function __construct(Crawler $crawler)
{
$this->crawler = $crawler;
$this->sitemaps = new Collection([new Sitemap]);
$this->hasCrawled = fn (Url $url, ResponseInterface $response = null) => $url;
}
public function configureCrawler(Closure $closure): static
{
call_user_func_array($closure, [$this->crawler]);
return $this;
}
public function setConcurrency(int $concurrency): static
{
$this->concurrency = $concurrency;
return $this;
}
public function setMaximumCrawlCount(int $maximumCrawlCount): static
{
$this->maximumCrawlCount = $maximumCrawlCount;
return $this;
}
public function maxTagsPerSitemap(int $maximumTagsPerSitemap = 50000): static
{
$this->maximumTagsPerSitemap = $maximumTagsPerSitemap;
return $this;
}
public function setUrl(string $urlToBeCrawled): static
{
$this->urlToBeCrawled = new Uri($urlToBeCrawled);
if ($this->urlToBeCrawled->getPath() === '') {
$this->urlToBeCrawled = $this->urlToBeCrawled->withPath('/');
}
return $this;
}
public function shouldCrawl(callable $shouldCrawl): static
{
$this->shouldCrawl = $shouldCrawl;
return $this;
}
public function hasCrawled(callable $hasCrawled): static
{
$this->hasCrawled = $hasCrawled;
return $this;
}
public function getSitemap(): Sitemap
{
if (config('sitemap.execute_javascript')) {
$this->crawler->executeJavaScript();
}
if (config('sitemap.chrome_binary_path')) {
$this->crawler
->setBrowsershot((new Browsershot)->setChromePath(config('sitemap.chrome_binary_path')))
->acceptNofollowLinks();
}
if (! is_null($this->maximumCrawlCount)) {
$this->crawler->setMaximumCrawlCount($this->maximumCrawlCount);
}
$this->crawler
->setCrawlProfile($this->getCrawlProfile())
->setCrawlObserver($this->getCrawlObserver())
->setConcurrency($this->concurrency)
->startCrawling($this->urlToBeCrawled);
return $this->sitemaps->first();
}
public function writeToFile(string $path): static
{
$sitemap = $this->getSitemap();
if ($this->maximumTagsPerSitemap) {
$sitemap = SitemapIndex::create();
$format = str_replace('.xml', '_%d.xml', $path);
// Parses each sub-sitemaps, writes and push them into the sitemap index
$this->sitemaps->each(function (Sitemap $item, int $key) use ($sitemap, $format) {
$path = sprintf($format, $key);
$item->writeToFile(sprintf($format, $key));
$sitemap->add(last(explode('public', $path)));
});
}
$sitemap->writeToFile($path);
return $this;
}
protected function getCrawlProfile(): CrawlProfile
{
$shouldCrawl = function (UriInterface $url) {
if ($url->getHost() !== $this->urlToBeCrawled->getHost()) {
return false;
}
if (! is_callable($this->shouldCrawl)) {
return true;
}
return ($this->shouldCrawl)($url);
};
$profileClass = config('sitemap.crawl_profile', Profile::class);
$profile = new $profileClass($this->urlToBeCrawled);
if (method_exists($profile, 'shouldCrawlCallback')) {
$profile->shouldCrawlCallback($shouldCrawl);
}
return $profile;
}
protected function getCrawlObserver(): Observer
{
$performAfterUrlHasBeenCrawled = function (UriInterface $crawlerUrl, ResponseInterface $response = null) {
$sitemapUrl = ($this->hasCrawled)(Url::create((string) $crawlerUrl), $response);
if ($this->shouldStartNewSitemapFile()) {
$this->sitemaps->push(new Sitemap);
}
if ($sitemapUrl) {
$this->sitemaps->last()->add($sitemapUrl);
}
};
return new Observer($performAfterUrlHasBeenCrawled);
}
protected function shouldStartNewSitemapFile(): bool
{
if (! $this->maximumTagsPerSitemap) {
return false;
}
$currentNumberOfTags = count($this->sitemaps->last()->getTags());
return $currentNumberOfTags >= $this->maximumTagsPerSitemap;
}
}