1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\CrawlerBundle\Command; |
4
|
|
|
|
5
|
|
|
use MediaMonks\Crawler\Page; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class CrawlUrlCommand extends ContainerAwareCommand |
13
|
|
|
{ |
14
|
|
|
const ARGUMENT_URL = 'url'; |
15
|
|
|
const OPTION_LIMIT = 'limit'; |
16
|
|
|
const OPTION_STOP_ON_ERROR = 'stop-on-error'; |
17
|
|
|
const OPTION_EXCEPTION_ON_ERROR = 'exception-on-error'; |
18
|
|
|
|
19
|
5 |
|
protected function configure() |
20
|
|
|
{ |
21
|
5 |
|
$this |
22
|
5 |
|
->setName('mediamonks:crawler:crawl-url') |
23
|
5 |
|
->setDescription('Crawl an url') |
24
|
|
|
; |
25
|
|
|
|
26
|
5 |
|
$this->addArguments(); |
27
|
5 |
|
$this->addOptions(); |
28
|
5 |
|
} |
29
|
|
|
|
30
|
5 |
|
protected function addArguments() |
31
|
|
|
{ |
32
|
5 |
|
$this->addArgument( |
33
|
5 |
|
self::ARGUMENT_URL, |
34
|
5 |
|
InputArgument::REQUIRED, |
35
|
|
|
'Url of the site you want to crawl' |
36
|
5 |
|
); |
37
|
5 |
|
} |
38
|
|
|
|
39
|
5 |
|
protected function addOptions() |
40
|
|
|
{ |
41
|
5 |
|
$this |
42
|
5 |
|
->addOption( |
43
|
5 |
|
self::OPTION_LIMIT, |
44
|
5 |
|
null, |
45
|
5 |
|
InputOption::VALUE_OPTIONAL, |
46
|
5 |
|
'Limit number of pages to crawl', |
47
|
|
|
0 |
48
|
5 |
|
) |
49
|
5 |
|
->addOption( |
50
|
5 |
|
self::OPTION_STOP_ON_ERROR, |
51
|
5 |
|
null, |
52
|
5 |
|
InputOption::VALUE_OPTIONAL, |
53
|
5 |
|
'Stop when a page could not be requested', |
54
|
|
|
false |
55
|
5 |
|
) |
56
|
5 |
|
->addOption( |
57
|
5 |
|
self::OPTION_EXCEPTION_ON_ERROR, |
58
|
5 |
|
null, |
59
|
5 |
|
InputOption::VALUE_OPTIONAL, |
60
|
5 |
|
'Throw exception when a page could not be requested', |
61
|
|
|
false |
62
|
5 |
|
); |
63
|
5 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param InputInterface $input |
67
|
|
|
* @param OutputInterface $output |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
5 |
|
$crawler = $this->getContainer()->get('mediamonks_crawler.crawler'); |
73
|
|
|
|
74
|
5 |
|
$crawler->setLimit((int)$input->getOption(self::OPTION_LIMIT)); |
75
|
5 |
|
$crawler->setStopOnError((bool)$input->getOption(self::OPTION_STOP_ON_ERROR)); |
76
|
5 |
|
$crawler->setExceptionOnError((bool)$input->getOption(self::OPTION_EXCEPTION_ON_ERROR)); |
77
|
|
|
|
78
|
5 |
|
foreach ($crawler->crawl($input->getArgument('url')) as $page) { |
79
|
5 |
|
$this->handlePage($page); |
80
|
5 |
|
} |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Page $page |
85
|
|
|
*/ |
86
|
|
|
protected function handlePage(Page $page) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.