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