1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\HttpStatusCheck; |
4
|
|
|
|
5
|
|
|
use Spatie\Crawler\Crawler; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
12
|
|
|
|
13
|
|
|
class ScanCommand extends Command |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this->setName('scan') |
18
|
|
|
->setDescription('Check the http status code of all links on a website.') |
19
|
|
|
->addArgument( |
20
|
|
|
'url', |
21
|
|
|
InputArgument::REQUIRED, |
22
|
|
|
'The url to check' |
23
|
|
|
) |
24
|
|
|
->addOption( |
25
|
|
|
'concurrency', |
26
|
|
|
'c', |
27
|
|
|
InputOption::VALUE_REQUIRED, |
28
|
|
|
'The amount of concurrent connections to use', |
29
|
|
|
10 |
30
|
|
|
) |
31
|
|
|
->addOption( |
32
|
|
|
'output', |
33
|
|
|
'o', |
34
|
|
|
InputOption::VALUE_REQUIRED, |
35
|
|
|
'The file to write the scan log for non 2xx responses' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
41
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
42
|
|
|
* |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$baseUrl = $input->getArgument('url'); |
48
|
|
|
|
49
|
|
|
$output->writeln("Start scanning {$baseUrl}"); |
50
|
|
|
$output->writeln(''); |
51
|
|
|
|
52
|
|
|
$crawlLogger = new CrawlLogger($output); |
53
|
|
|
|
54
|
|
|
if ($input->getOption('output')) { |
55
|
|
|
$outputFile = $input->getOption('output'); |
56
|
|
|
|
57
|
|
|
if (file_exists($outputFile)) { |
58
|
|
|
$helper = $this->getHelper('question'); |
59
|
|
|
$question = new ConfirmationQuestion('Overwrite existing file? ', false); |
60
|
|
|
$crawlLogger->setOverwriteOutputFile($helper->ask($input, $output, $question)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$crawlLogger->setOutputFile($input->getOption('output')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
Crawler::create() |
67
|
|
|
->setConcurrency($input->getOption('concurrency')) |
68
|
|
|
->setCrawlObserver($crawlLogger) |
69
|
|
|
->startCrawling($baseUrl); |
70
|
|
|
|
71
|
|
|
return 0; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|