1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Crawler; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\Crawler\Exception\UrlNotFoundByIndex; |
7
|
|
|
|
8
|
|
|
class CollectionCrawlQueue implements CrawlQueue |
9
|
|
|
{ |
10
|
|
|
/** @var \Illuminate\Support\Collection */ |
11
|
|
|
public $urls; |
12
|
|
|
|
13
|
|
|
/** @var \Illuminate\Support\Collection */ |
14
|
|
|
public $pendingUrls; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->urls = collect(); |
19
|
|
|
|
20
|
|
|
$this->pendingUrls = collect(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function add(CrawlUrl $url): CrawlQueue |
24
|
|
|
{ |
25
|
|
|
if ($this->has($url)) { |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->urls->push($url); |
30
|
|
|
|
31
|
|
|
$url->setId($this->urls->keys()->last()); |
32
|
|
|
$this->pendingUrls->push($url); |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function hasPendingUrls(): bool |
38
|
|
|
{ |
39
|
|
|
return (bool) $this->pendingUrls->count(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int $id |
44
|
|
|
* |
45
|
|
|
* @return \Spatie\Crawler\CrawlUrl|null |
46
|
|
|
*/ |
47
|
|
|
public function getUrlById(int $id): CrawlUrl |
48
|
|
|
{ |
49
|
|
|
if (! isset($this->urls->values()[$id])) { |
50
|
|
|
throw new UrlNotFoundByIndex(sprintf('#%d crawl url not found in collection', $id)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->urls->values()[$id]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function hasAlreadyBeenProcessed(CrawlUrl $url) |
57
|
|
|
{ |
58
|
|
|
return ! $this->contains($this->pendingUrls, $url) && $this->contains($this->urls, $url); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function markAsProcessed(CrawlUrl $crawlUrl) |
62
|
|
|
{ |
63
|
|
|
$this->pendingUrls = $this->pendingUrls |
64
|
|
|
->reject(function (CrawlUrl $crawlUrlItem) use ($crawlUrl) { |
65
|
|
|
return $crawlUrlItem->url->isEqual($crawlUrl->url); |
|
|
|
|
66
|
|
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param CrawlUrl|Url $crawlUrl |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function has($crawlUrl): bool |
75
|
|
|
{ |
76
|
|
|
if ($crawlUrl instanceof Url) { |
77
|
|
|
$crawlUrl = CrawlUrl::create($crawlUrl); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->contains($this->urls, $crawlUrl)) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function contains(Collection $collection, CrawlUrl $searchCrawlUrl): bool |
88
|
|
|
{ |
89
|
|
|
foreach ($collection as $crawlUrl) { |
90
|
|
|
if ($crawlUrl->url->isEqual($searchCrawlUrl->url)) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @return \Spatie\Crawler\CrawlUrl|null */ |
99
|
|
|
public function getPendingUrl() |
100
|
|
|
{ |
101
|
|
|
$first = $this->pendingUrls->first(); |
102
|
|
|
if (! $first) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $first; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: