1 | <?php |
||
22 | class Spider |
||
23 | { |
||
24 | /** @var DownloaderInterface */ |
||
25 | private $downloader; |
||
26 | |||
27 | /** @var QueueManagerInterface */ |
||
28 | private $queueManager; |
||
29 | |||
30 | /** @var EventDispatcherInterface */ |
||
31 | private $dispatcher; |
||
32 | |||
33 | /** @var DiscovererSet */ |
||
34 | private $discovererSet; |
||
35 | |||
36 | /** @var DiscoveredUri The URI of the site to spider */ |
||
37 | private $seed = array(); |
||
38 | |||
39 | /** @var string the unique id of this spider instance */ |
||
40 | private $spiderId; |
||
41 | |||
42 | /** |
||
43 | * @param string $seed the URI to start crawling |
||
44 | * @param string|null $spiderId |
||
45 | */ |
||
46 | public function __construct($seed, $spiderId = null) |
||
66 | |||
67 | /** |
||
68 | * Starts crawling the URI provided on instantiation |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | public function crawl() |
||
79 | |||
80 | /** |
||
81 | * param DiscovererSet $discovererSet |
||
82 | */ |
||
83 | public function setDiscovererSet(DiscovererSet $discovererSet) |
||
87 | |||
88 | /** |
||
89 | * @return DiscovererSet |
||
90 | */ |
||
91 | public function getDiscovererSet() |
||
99 | |||
100 | /** |
||
101 | * param QueueManagerInterface $queueManager |
||
102 | */ |
||
103 | public function setQueueManager(QueueManagerInterface $queueManager) |
||
107 | |||
108 | /** |
||
109 | * @return QueueManagerInterface |
||
110 | */ |
||
111 | public function getQueueManager() |
||
119 | |||
120 | /** |
||
121 | * @param DownloaderInterface $downloader |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setDownloader(DownloaderInterface $downloader) |
||
130 | |||
131 | /** |
||
132 | * @return DownloaderInterface |
||
133 | */ |
||
134 | public function getDownloader() |
||
141 | |||
142 | /** |
||
143 | * @param EventDispatcherInterface $eventDispatcher |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function setDispatcher(EventDispatcherInterface $eventDispatcher) |
||
152 | |||
153 | /** |
||
154 | * @return EventDispatcherInterface |
||
155 | */ |
||
156 | public function getDispatcher() |
||
163 | |||
164 | public function handleSignal($signal) |
||
174 | |||
175 | /** |
||
176 | * Function that crawls each provided URI |
||
177 | * It applies all processors and listeners set on the Spider |
||
178 | * |
||
179 | * This is a either depth first algorithm as explained here: |
||
180 | * https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
181 | * Note that because we don't do it recursive, but iteratively, |
||
182 | * results will be in a different order from the example, because |
||
183 | * we always take the right-most child first, whereas a recursive |
||
184 | * variant would always take the left-most child first |
||
185 | * |
||
186 | * or |
||
187 | * |
||
188 | * a breadth first algorithm |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | private function doCrawl() |
||
221 | |||
222 | /** |
||
223 | * A shortcut for EventDispatcher::dispatch() |
||
224 | * |
||
225 | * @param string $eventName |
||
226 | * @param null|Event $event |
||
227 | */ |
||
228 | private function dispatch($eventName, Event $event = null) |
||
232 | |||
233 | /** |
||
234 | * @param string $uri |
||
235 | */ |
||
236 | private function setSeed($uri) |
||
241 | } |
||
242 |