1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Crawler; |
4
|
|
|
|
5
|
|
|
use Spatie\Robots\RobotsTxt; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
use Spatie\Browsershot\Browsershot; |
8
|
|
|
use Spatie\Crawler\CrawlQueue\CrawlQueue; |
9
|
|
|
|
10
|
|
|
trait CrawlerProperties |
11
|
|
|
{ |
12
|
|
|
public function setConcurrency(int $concurrency): self |
13
|
|
|
{ |
14
|
|
|
$this->concurrency = $concurrency; |
|
|
|
|
15
|
|
|
|
16
|
|
|
return $this; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function setMaximumResponseSize(int $maximumResponseSizeInBytes): self |
20
|
|
|
{ |
21
|
|
|
$this->maximumResponseSize = $maximumResponseSizeInBytes; |
|
|
|
|
22
|
|
|
|
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getMaximumResponseSize(): ?int |
27
|
|
|
{ |
28
|
|
|
return $this->maximumResponseSize; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setMaximumCrawlCount(int $maximumCrawlCount): self |
32
|
|
|
{ |
33
|
|
|
$this->maximumCrawlCount = $maximumCrawlCount; |
|
|
|
|
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getMaximumCrawlCount(): ?int |
39
|
|
|
{ |
40
|
|
|
return $this->maximumCrawlCount; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getCrawlerUrlCount(): int |
44
|
|
|
{ |
45
|
|
|
return $this->crawledUrlCount; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setMaximumDepth(int $maximumDepth): self |
49
|
|
|
{ |
50
|
|
|
$this->maximumDepth = $maximumDepth; |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getMaximumDepth(): ?int |
56
|
|
|
{ |
57
|
|
|
return $this->maximumDepth; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function ignoreRobots(): self |
61
|
|
|
{ |
62
|
|
|
$this->respectRobots = false; |
|
|
|
|
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function respectRobots(): self |
68
|
|
|
{ |
69
|
|
|
$this->respectRobots = true; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function mustRespectRobots(): bool |
75
|
|
|
{ |
76
|
|
|
return $this->respectRobots; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getRobotsTxt(): RobotsTxt |
80
|
|
|
{ |
81
|
|
|
return $this->robotsTxt; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setCrawlQueue(CrawlQueue $crawlQueue): self |
85
|
|
|
{ |
86
|
|
|
$this->crawlQueue = $crawlQueue; |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getCrawlQueue(): CrawlQueue |
92
|
|
|
{ |
93
|
|
|
return $this->crawlQueue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function executeJavaScript(): self |
97
|
|
|
{ |
98
|
|
|
$this->executeJavaScript = true; |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function doNotExecuteJavaScript(): self |
104
|
|
|
{ |
105
|
|
|
$this->executeJavaScript = false; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function mayExecuteJavascript(): bool |
111
|
|
|
{ |
112
|
|
|
return $this->executeJavaScript; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
|
|
|
|
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setCrawlObserver($crawlObservers): self |
121
|
|
|
{ |
122
|
|
|
if (! is_array($crawlObservers)) { |
123
|
|
|
$crawlObservers = [$crawlObservers]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->setCrawlObservers($crawlObservers); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function setCrawlObservers(array $crawlObservers): self |
130
|
|
|
{ |
131
|
|
|
$this->crawlObservers = new ObserverCollection($crawlObservers); |
|
|
|
|
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function addCrawlObserver(CrawlObserver $crawlObserver): self |
137
|
|
|
{ |
138
|
|
|
$this->crawlObservers->addObserver($crawlObserver); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getCrawlObservers(): ObserverCollection |
144
|
|
|
{ |
145
|
|
|
return $this->crawlObservers; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setCrawlProfile(CrawlProfile $crawlProfile): self |
149
|
|
|
{ |
150
|
|
|
$this->crawlProfile = $crawlProfile; |
|
|
|
|
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getCrawlProfile(): CrawlProfile |
156
|
|
|
{ |
157
|
|
|
return $this->crawlProfile; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setBrowsershot(Browsershot $browsershot) |
161
|
|
|
{ |
162
|
|
|
$this->browsershot = $browsershot; |
|
|
|
|
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getBrowsershot(): Browsershot |
168
|
|
|
{ |
169
|
|
|
if (! $this->browsershot) { |
170
|
|
|
$this->browsershot = new Browsershot(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->browsershot; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getBaseUrl(): UriInterface |
177
|
|
|
{ |
178
|
|
|
return $this->baseUrl; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: