add simple console command

This commit is contained in:
James
2020-02-22 13:50:20 +00:00
parent 9623a36013
commit ef4ebf7cd0
5 changed files with 336 additions and 2 deletions

View File

@@ -0,0 +1,22 @@
<?php
namespace JHodges\Sitemap;
use Symfony\Component\Console\Application;
class ConsoleApplication extends Application
{
public function __construct()
{
error_reporting(-1);
parent::__construct('Sitempa', '0.0.1');
$this->add(new CrawlCommand());
}
public function getLongVersion()
{
return parent::getLongVersion().' by <comment>JHodges</comment>';
}
}

54
src/CrawlCommand.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
namespace JHodges\Sitemap;
use GuzzleHttp\RequestOptions;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class CrawlCommand extends Command
{
protected function configure()
{
$this->setName('crawl')
->setDescription('Crawl and generate sitemap for the website.')
->addArgument(
'url',
InputArgument::REQUIRED,
'The url to check'
)->addOption(
'found-on',
'f',
InputOption::VALUE_NONE,
'Display found on URLs'
);
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$baseUrl = $input->getArgument('url');
$crawler=new Crawler($baseUrl);
$crawler->crawl($baseUrl);
foreach($crawler->getResults() as $url=>$result){
$output->writeln("{$result['code']} {$url}");
if($input->getOption('found-on')){
foreach($result['foundOn'] as $url=>$count){
$output->writeln(" -> ($count) $url");
}
}
}
return 0;
}
}