| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of TwigView. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  ** (c) 2014 Cees-Jan Kiewiet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace WyriHaximus\TwigView\Shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cake\Console\ConsoleIo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cake\Console\Shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cake\Core\Plugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use WyriHaximus\TwigView\Lib\Scanner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use WyriHaximus\TwigView\View\TwigView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class CompileTemplatesShell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @package WyriHaximus\TwigView\Console\Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  | // @codingStandardsIgnoreStart | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class CompileShell extends Shell | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | // @codingStandardsIgnoreEnd | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Instance of TwigView to be used to compile templates. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var TwigView | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     protected $twigView; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * Constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param ConsoleIo $consoleIo An IO instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |     public function __construct(ConsoleIo $consoleIo = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         parent::__construct($consoleIo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         $this->twigView = new TwigView(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * Set TwigView. | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @param TwigView $twigView TwigView instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |     public function setTwigview(TwigView $twigView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $this->twigView = $twigView; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * Compile all templates. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |     public function all() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $this->out('<info>Compiling all templates</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         foreach(Scanner::all() as $section => $templates) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             $this->out('<info>Compiling ' . $section . '\'s templates</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |             $this->walkIterator($templates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     // @codingStandardsIgnoreEnd | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * Compile only this plugin. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $plugin Plugin name. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |     public function plugin($plugin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         $this->out('<info>Compiling one ' . $plugin . '\'s templates</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         $this->walkIterator(Scanner::plugin($plugin)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * Only compile one file. | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $fileName File to compile. | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |     public function file($fileName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         $this->out('<info>Compiling one template</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |         $this->compileTemplate($fileName); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * Walk over $iterator and compile all templates in it. | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param mixed $iterator Iterator to walk over. | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     protected function walkIterator($iterator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         foreach ($iterator as $template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $this->compileTemplate($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * Compile a template. | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @param string $fileName Template to compile. | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     protected function compileTemplate($fileName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this-> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 twigView-> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 getTwig()-> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 loadTemplate($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $this->out('<success>' . $fileName . '</success>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $this->out('<error>' . $fileName . '</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $this->out('<error>' . $exception->getMessage() . '</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * Set options for this console. | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * @return \Cake\Console\ConsoleOptionParser | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |     public function getOptionParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |         return parent::getOptionParser()->addSubcommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |             'all', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 1 |  |                 'short' => 'a', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  |                 'help' => __('Searches and precompiles all twig templates it finds.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |         )->addSubcommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |             'plugin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |                 'short' => 'p', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  |                 'help' => __('Searches and precompiles all twig templates for a specific plugin.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |         )->addSubcommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |             'file', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |                 'short' => 'f', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |                 'help' => __('Precompile a specific file.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 1 |  |         )->setDescription(__('TwigView templates precompiler')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |