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 setMaximumCrawlCount(int $maximumCrawlCount): self |
27
|
|
|
{ |
28
|
|
|
$this->maximumCrawlCount = $maximumCrawlCount; |
|
|
|
|
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setMaximumDepth(int $maximumDepth): self |
34
|
|
|
{ |
35
|
|
|
$this->maximumDepth = $maximumDepth; |
|
|
|
|
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function ignoreRobots(): self |
41
|
|
|
{ |
42
|
|
|
$this->respectRobots = false; |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function respectRobots(): self |
48
|
|
|
{ |
49
|
|
|
$this->respectRobots = true; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setCrawlQueue(CrawlQueue $crawlQueue): self |
55
|
|
|
{ |
56
|
|
|
$this->crawlQueue = $crawlQueue; |
|
|
|
|
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function executeJavaScript(): self |
62
|
|
|
{ |
63
|
|
|
$this->executeJavaScript = true; |
|
|
|
|
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function doNotExecuteJavaScript(): self |
69
|
|
|
{ |
70
|
|
|
$this->executeJavaScript = false; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \Spatie\Crawler\CrawlObserver|array[\Spatie\Crawler\CrawlObserver] $crawlObservers |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setCrawlObserver($crawlObservers) |
81
|
|
|
{ |
82
|
|
|
if (! is_array($crawlObservers)) { |
83
|
|
|
$crawlObservers = [$crawlObservers]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->setCrawlObservers($crawlObservers); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setCrawlObservers(array $crawlObservers): self |
90
|
|
|
{ |
91
|
|
|
$this->crawlObservers = $crawlObservers; |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function addCrawlObserver(CrawlObserver $crawlObserver): self |
97
|
|
|
{ |
98
|
|
|
$this->crawlObservers[] = $crawlObserver; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setCrawlProfile(CrawlProfile $crawlProfile): self |
104
|
|
|
{ |
105
|
|
|
$this->crawlProfile = $crawlProfile; |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getBaseUrl(): UriInterface |
111
|
|
|
{ |
112
|
|
|
return $this->baseUrl; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getCrawlQueue(): CrawlQueue |
116
|
|
|
{ |
117
|
|
|
return $this->crawlQueue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getCrawlProfile(): CrawlProfile |
121
|
|
|
{ |
122
|
|
|
return $this->crawlProfile; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \Spatie\Crawler\CrawlObserver[] |
127
|
|
|
*/ |
128
|
|
|
public function getCrawlObservers(): array |
129
|
|
|
{ |
130
|
|
|
return $this->crawlObservers; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getMaximumResponseSize(): ?int |
134
|
|
|
{ |
135
|
|
|
return $this->maximumResponseSize; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function mustRespectRobots(): bool |
139
|
|
|
{ |
140
|
|
|
return $this->respectRobots; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getRobotsTxt(): RobotsTxt |
144
|
|
|
{ |
145
|
|
|
return $this->robotsTxt; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getMaximumDepth(): ?int |
149
|
|
|
{ |
150
|
|
|
return $this->maximumDepth; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getMaximumCrawlCount(): ?int |
154
|
|
|
{ |
155
|
|
|
return $this->maximumCrawlCount; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getCrawlerUrlCount(): int |
159
|
|
|
{ |
160
|
|
|
return $this->crawledUrlCount; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getBrowsershot(): Browsershot |
164
|
|
|
{ |
165
|
|
|
if (! $this->browsershot) { |
166
|
|
|
$this->browsershot = new Browsershot(); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->browsershot; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function setBrowsershot(Browsershot $browsershot) |
173
|
|
|
{ |
174
|
|
|
$this->browsershot = $browsershot; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function mayExecuteJavascript(): bool |
180
|
|
|
{ |
181
|
|
|
return $this->executeJavaScript; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
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: