| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Katten\Purge\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Sabberworm\CSS\OutputFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Katten\Purge\Factory\CssDocumentFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class AppController { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $css; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(InputInterface $input, OutputInterface $output) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->input = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->output = $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Start up the PurgeManager and output the summary | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |      */  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     public function run() { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             $this->setUp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             $purgeManager = new PurgeController($this->css, $this->output); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $purgeManager->startPurge($this->html); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $this->outputSummary($purgeManager->getPurgeResults()); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         }  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         catch ( \Exception $e ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $type = get_class($e); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $this->output->writeln(''); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $this->output->writeln("<error> {$type} </error>"); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $this->output->writeln("<error> {$e->getMessage()} </error> \n"); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->output->writeln('');  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Create the CSS Document object and build the html  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * filenames array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * This function is just here in the interim as refactoring | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * occurs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function setUp() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->output->write('Reading in CSS'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $cssFile   = $this->input->getArgument('css'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $mbSupport = $this->input->getOption('mb-support'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->css = CssDocumentFactory::build($cssFile, $mbSupport); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->output->writeln(' [<info>OK</info>]');  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->html = [ $this->input->getArgument('html') ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |        | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Write the summary to the specified file, function will change | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * simply here to get basic functionality.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param array $purgedCSS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      *      An array of DeclarationBlocks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function outputSummary($purgedCSS) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $file = $this->input->getArgument('output-file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->output->write('Outputting results to file');         | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $output = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         foreach ($purgedCSS as $purgedBlock) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $output .= $purgedBlock->render(OutputFormat::createPretty());     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $result = @file_put_contents($file, $output);    | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (!$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             throw new \Exception("Error unable to write to file: $file"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         }  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->output->writeln(' [<info>OK</info>]');    | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |