1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Matthijs van den Bos <[email protected]> |
4
|
|
|
* @copyright 2013 Matthijs van den Bos |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Example; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
11
|
|
|
use VDB\Uri\UriInterface; |
12
|
|
|
use VDB\Spider\Event\SpiderEvents; |
13
|
|
|
|
14
|
|
|
class LogHandler implements EventSubscriberInterface |
15
|
|
|
{ |
16
|
|
|
private $debug = false; |
17
|
|
|
|
18
|
|
|
public function __construct($debug = false) |
19
|
|
|
{ |
20
|
|
|
$this->debug = $debug; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function getSubscribedEvents() |
24
|
|
|
{ |
25
|
|
|
return array( |
26
|
|
|
SpiderEvents::SPIDER_CRAWL_FILTER_POSTFETCH => 'logFiltered', |
27
|
|
|
SpiderEvents::SPIDER_CRAWL_FILTER_PREFETCH => 'logFiltered', |
28
|
|
|
SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE => 'logQueued', |
29
|
|
|
SpiderEvents::SPIDER_CRAWL_RESOURCE_PERSISTED => 'logPersisted', |
30
|
|
|
SpiderEvents::SPIDER_CRAWL_ERROR_REQUEST => 'logFailed' |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function logEvent($name, GenericEvent $event) |
35
|
|
|
{ |
36
|
|
|
if ($this->debug === true) { |
37
|
|
|
echo "\n[$name]\t:" . $event->getArgument('uri')->toString(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function logQueued(GenericEvent $event) |
42
|
|
|
{ |
43
|
|
|
$this->logEvent('queued', $event); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function logPersisted(GenericEvent $event) |
47
|
|
|
{ |
48
|
|
|
$this->logEvent('persisted', $event); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function logFiltered(GenericEvent $event) |
52
|
|
|
{ |
53
|
|
|
$this->logEvent('filtered', $event); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function logFailed(GenericEvent $event) |
57
|
|
|
{ |
58
|
|
|
$this->logEvent('failed', $event); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|