| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace GarethEllis\Tldr\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GarethEllis\Tldr\Cache\StashAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GarethEllis\Tldr\Console\Output\PageOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GarethEllis\Tldr\Fetcher\CacheFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GarethEllis\Tldr\Fetcher\Exception\RemoteFetcherException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GarethEllis\Tldr\Fetcher\OperatingSystemTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Stash\Driver\FileSystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Stash\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Command\HelpCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Helper\DescriptorHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Input\ArrayInput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use GarethEllis\Tldr\Fetcher\RemoteFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use GuzzleHttp\Client as Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class TldrCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     use OperatingSystemTrait; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             ->setName('tldr') | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             ->setDescription('Perform a look-up against the TLDR man pages project') | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             ->addArgument( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 'page', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 InputArgument::OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 'The TLDR man page to look-up' | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 'refresh-cache', | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                 'r', | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                 "Fetch command from remote repository and refresh cache", | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                 null | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                 'os', | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                 'o', | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 "Operating system to search for: linux, osx or sunos", | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 null | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 'flush-cache', | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 'f', | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 "Delete all cached pages", | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 null | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $http = new Http(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($input->getOption('os')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $options["operatingSystem"] = $input->getOption('os'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $fetcher = new RemoteFetcher($http, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $driver = new FileSystem([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             "path" => sys_get_temp_dir() . "tldr-cache" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $pool = new Pool($driver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $cache = new StashAdapter($pool); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $fetcher = new CacheFetcher($fetcher, $cache, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($input->getOption('flush-cache')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $cache->flushCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (!$input->getArgument("page")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             return $this->outputHelp($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if ($input->getOption('refresh-cache')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $operatingSystem = $input->getOption('os') ?: $this->getOperatingSystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $cache->deleteFromCache($operatingSystem, $input->getArgument("page")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $page = $fetcher->fetchPage($input->getArgument("page")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $pageOutput = new PageOutput($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $pageOutput->write($page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } catch (PageNotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return $output->writeln("<comment>Page not found</comment>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } catch (RemoteFetcherException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             return $output->writeln("<error>Unable to connect to repository :-(</error>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws \Symfony\Component\Console\Exception\ExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     protected function outputHelp(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $help = new HelpCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $help->setCommand($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return $help->run($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.