<?phpnamespaceDrush\Commands;useDrush\Exec\ExecTrait;useSymfony\Component\Console\Attribute\AsCommand;useSymfony\Component\Console\Command\Command;useSymfony\Component\Console\Input\InputArgument;useSymfony\Component\Console\Input\InputInterface;useSymfony\Component\Console\Input\InputOption;useSymfony\Component\Console\Output\OutputInterface;/** * Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples xkcd` */#[AsCommand(name:'xkcd:fetch',description:'Retrieve and display xkcd cartoons.',aliases:['xkcd'],)]finalclassXkcdFetchCommandextendsCommand{useExecTrait;protectedfunctionconfigure():void{$this->addArgument('search',mode:InputArgument::OPTIONAL,description:'Optional argument to retrieve the cartoons matching an index number, keyword search or "random". If omitted the latest cartoon will be retrieved.')->addOption('image-viewer',null,InputOption::VALUE_REQUIRED,'Command to use to view images (e.g. xv, firefox). Defaults to "display" (from ImageMagick).','open')->addOption('google-custom-search-api-key',null,InputOption::VALUE_REQUIRED,'Google Custom Search API Key, available from https://code.google.com/apis/console/. Default key limited to 100 queries/day globally.','AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek')->addUsage('drush xkcd sandwich')->addUsage('drush xkcd 123 --image-viewer=eog')->addUsage('drush xkcd random --image-viewer=firefox');}publicfunctionexecute(InputInterface$input,OutputInterface$output):int{$this->doFetch($input->getArgument('search'),$input->getOptions());returnself::SUCCESS;}protectedfunctiondoFetch($search,array$options):void{if(empty($search)){$this->startBrowser(uri:'http://xkcd.com',browser:true);}elseif(is_numeric($search)){$this->startBrowser(uri:'http://xkcd.com/'.$search,browser:true);}elseif($search=='random'){$xkcd_response=@json_decode(file_get_contents('http://xkcd.com/info.0.json'));if(!empty($xkcd_response->num)){$this->startBrowser(uri:'http://xkcd.com/'.rand(1,$xkcd_response->num),browser:true);}}else{// This uses an API key with a limited number of searches per.$search_response=@json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key='.$options['google-custom-search-api-key'].'&cx=012652707207066138651:zudjtuwe28q&q='.$search));if(!empty($search_response->items)){foreach($search_response->itemsas$item){$this->startBrowser(uri:$item->link,browser:true);}}else{thrownew\Exception(dt('The search failed or produced no results.'));}}}}