1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace safaeean\Sitemap; |
4
|
|
|
|
5
|
|
|
use safaeean\Crawler\Crawler; |
|
|
|
|
6
|
|
|
use safaeean\Sitemap\Tags\Url; |
7
|
|
|
use safaeean\Sitemap\Crawler\Profile; |
8
|
|
|
use safaeean\Sitemap\Crawler\Observer; |
9
|
|
|
use safaeean\Crawler\Url as CrawlerUrl; |
|
|
|
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class SitemapGenerator |
13
|
|
|
{ |
14
|
|
|
/** @var \safaeean\Sitemap\Sitemap */ |
15
|
|
|
protected $sitemap; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $urlToBeCrawled = ''; |
19
|
|
|
|
20
|
|
|
/** @var \safaeean\Crawler\Crawler */ |
21
|
|
|
protected $crawler; |
22
|
|
|
|
23
|
|
|
/** @var callable */ |
24
|
|
|
protected $shouldCrawl; |
25
|
|
|
|
26
|
|
|
/** @var callable */ |
27
|
|
|
protected $hasCrawled; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
protected $concurrency = 10; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $urlToBeCrawled |
34
|
|
|
* |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
|
|
public static function create(string $urlToBeCrawled) |
38
|
|
|
{ |
39
|
|
|
return app(static::class)->setUrl($urlToBeCrawled); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function __construct(Crawler $crawler) |
43
|
|
|
{ |
44
|
|
|
$this->crawler = $crawler; |
45
|
|
|
|
46
|
|
|
$this->sitemap = new Sitemap(); |
47
|
|
|
|
48
|
|
|
$this->hasCrawled = function (Url $url, ResponseInterface $response = null) { |
|
|
|
|
49
|
|
|
return $url; |
50
|
|
|
}; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setConcurrency(int $concurrency) |
54
|
|
|
{ |
55
|
|
|
$this->concurrency = $concurrency; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setUrl(string $urlToBeCrawled) |
59
|
|
|
{ |
60
|
|
|
$this->urlToBeCrawled = $urlToBeCrawled; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function shouldCrawl(callable $shouldCrawl) |
66
|
|
|
{ |
67
|
|
|
$this->shouldCrawl = $shouldCrawl; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function hasCrawled(callable $hasCrawled) |
73
|
|
|
{ |
74
|
|
|
$this->hasCrawled = $hasCrawled; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getSitemap(): Sitemap |
80
|
|
|
{ |
81
|
|
|
$this->crawler |
82
|
|
|
->setCrawlProfile($this->getCrawlProfile()) |
83
|
|
|
->setCrawlObserver($this->getCrawlObserver()) |
84
|
|
|
->setConcurrency($this->concurrency) |
85
|
|
|
->startCrawling($this->urlToBeCrawled); |
86
|
|
|
|
87
|
|
|
return $this->sitemap; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $path |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function writeToFile(string $path) |
96
|
|
|
{ |
97
|
|
|
$this->getSitemap()->writeToFile($path); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getCrawlProfile(): Profile |
103
|
|
|
{ |
104
|
|
|
$shouldCrawl = function (CrawlerUrl $url) { |
105
|
|
|
if ($url->host !== CrawlerUrl::create($this->urlToBeCrawled)->host) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (! is_callable($this->shouldCrawl)) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return ($this->shouldCrawl)($url); |
114
|
|
|
}; |
115
|
|
|
|
116
|
|
|
return new Profile($shouldCrawl); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function getCrawlObserver(): Observer |
120
|
|
|
{ |
121
|
|
|
$performAfterUrlHasBeenCrawled = function (CrawlerUrl $crawlerUrl, ResponseInterface $response = null) { |
122
|
|
|
$sitemapUrl = ($this->hasCrawled)(Url::create($crawlerUrl), $response); |
123
|
|
|
|
124
|
|
|
if ($sitemapUrl) { |
125
|
|
|
$this->sitemap->add($sitemapUrl); |
126
|
|
|
} |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
return new Observer($performAfterUrlHasBeenCrawled); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths