1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Matthijs van den Bos <[email protected]> |
4
|
|
|
* @copyright 2013 Matthijs van den Bos |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace VDB\Spider\QueueManager; |
8
|
|
|
|
9
|
|
|
use VDB\Uri\UriInterface; |
10
|
|
|
use VDB\Spider\Exception\QueueException; |
11
|
|
|
use Symfony\Component\EventDispatcher\Event; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
15
|
|
|
use VDB\Spider\Event\SpiderEvents; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TODO: make this an Iterable |
19
|
|
|
*/ |
20
|
|
|
class InMemoryQueueManager implements QueueManager |
21
|
|
|
{ |
22
|
|
|
/** @var int The maximum depth for the crawl */ |
23
|
|
|
public $maxDepth = 3; |
24
|
|
|
|
25
|
|
|
/** @var int The maximum size of the process queue for this spider. 0 means infinite */ |
26
|
|
|
public $maxQueueSize = 0; |
27
|
|
|
|
28
|
|
|
/** @var int the amount of times a Resource was enqueued */ |
29
|
|
|
private $currentQueueSize = 0; |
30
|
|
|
|
31
|
|
|
/** @var UriInterface[] the list of URIs to process */ |
32
|
|
|
private $traversalQueue = array(); |
33
|
|
|
|
34
|
|
|
/** @var int The traversal algorithm to use. Choose from the class constants |
35
|
|
|
*/ |
36
|
|
|
private $traversalAlgorithm = self::ALGORITHM_DEPTH_FIRST; |
37
|
|
|
|
38
|
|
|
/** @var EventDispatcherInterface */ |
39
|
|
|
private $dispatcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int $traversalAlgorithm Choose from the class constants |
43
|
|
|
* TODO: This should be extracted to a Strategy pattern |
44
|
|
|
*/ |
45
|
|
|
public function setTraversalAlgorithm($traversalAlgorithm) |
46
|
|
|
{ |
47
|
|
|
$this->traversalAlgorithm = $traversalAlgorithm; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function getTraversalAlgorithm() |
54
|
|
|
{ |
55
|
|
|
return $this->traversalAlgorithm; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setDispatcher(EventDispatcherInterface $eventDispatcher) |
63
|
|
|
{ |
64
|
|
|
$this->dispatcher = $eventDispatcher; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return EventDispatcherInterface |
71
|
|
|
*/ |
72
|
|
|
public function getDispatcher() |
73
|
|
|
{ |
74
|
|
|
if (!$this->dispatcher) { |
75
|
|
|
$this->dispatcher = new EventDispatcher(); |
76
|
|
|
} |
77
|
|
|
return $this->dispatcher; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param UriInterface |
82
|
|
|
*/ |
83
|
|
|
public function addUri(UriInterface $uri) |
84
|
|
|
{ |
85
|
|
|
if ($this->maxQueueSize != 0 && $this->currentQueueSize >= $this->maxQueueSize) { |
86
|
|
|
throw new QueueException('Maximum Queue Size of ' . $this->maxQueueSize . ' reached'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->currentQueueSize++; |
90
|
|
|
array_push($this->traversalQueue, $uri); |
91
|
|
|
|
92
|
|
|
$this->getDispatcher()->dispatch( |
93
|
|
|
SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE, |
94
|
|
|
new GenericEvent($this, array('uri' => $uri)) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function next() |
99
|
|
|
{ |
100
|
|
|
if ($this->traversalAlgorithm === static::ALGORITHM_DEPTH_FIRST) { |
101
|
|
|
return array_pop($this->traversalQueue); |
102
|
|
|
} elseif ($this->traversalAlgorithm === static::ALGORITHM_BREADTH_FIRST) { |
103
|
|
|
return array_shift($this->traversalQueue); |
104
|
|
|
} else { |
105
|
|
|
throw new \LogicException('No search algorithm set'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|