| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Highlighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (C) 2016, Some right reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Kacper "Kadet" Donat <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Contact with author: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Xmpp: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * E-mail: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * From Kadet with love. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace Kadet\Highlighter\bin\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Kadet\Highlighter\bin\VerboseOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     use Kadet\Highlighter\KeyLighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Kadet\Highlighter\Language\Language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Console\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\Console\Output\Output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class HighlightCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $_debug = ['time', 'detailed-time', 'count', 'tree-before', 'tree-after', 'density']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->setName('highlight') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File(s) to highlight') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'Source Language to highlight, see <comment>list-languages</comment> command') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format, see <comment>list-languages</comment> command', 'cli') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 'debug', 'd', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 'Debug features (only in verbose): '.implode(', ', array_map(function($f) { return "<info>{$f}</info>"; }, $this->_debug)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ->setDescription('<comment>[DEFAULT]</comment> Highlights given file') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if(!empty($input->getOption('debug')) && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $output->writeln($this->getApplication()->getLongVersion()."\n", Output::VERBOSITY_VERBOSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $formatter = KeyLighter::get()->getFormatter($input->getOption('format')) ?: KeyLighter::get()->getDefaultFormatter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach($input->getArgument('path') as $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $this->process($input, $output, $filename, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     protected function content($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if(!($file = @fopen($path, 'r'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $content = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         while(!feof($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $content .= fgets($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         fclose($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function mergeApplicationDefinition($arguments = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         parent::mergeApplicationDefinition($this->getApplication()->explicit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param InputInterface  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param                 $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param                 $formatter | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     protected function process(InputInterface $input, OutputInterface $output, $filename, $formatter) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         $language = $input->getOption('language') | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             ? Language::byName($input->getOption('language')) | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |             : Language::byFilename($filename); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         if (!($source = $this->content($filename))) { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             throw new InvalidArgumentException(sprintf('Specified file %s doesn\'t exist, check if given path is correct.', | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |                 $filename)); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         if ($output->isVerbose()) { | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             $output->writeln(sprintf( | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |                 "Used file: <path>%s</path>, Language: <language>%s</language>, Formatter: <formatter>%s</formatter>", | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |                 $filename, $language->getFQN(), get_class($formatter) | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |             )); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             $verbose   = new VerboseOutput($output, $input, $language, $formatter, $source); | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |             $formatted = $verbose->process(); | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             $formatted = KeyLighter::get()->highlight($source, $language, $formatter); | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         if (!$input->getOption('no-output')) { | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |             $output->writeln($formatted); | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |