| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Cecil. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Cecil\Step\Optimize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cecil\Assets\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Cecil\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cecil\Step\AbstractStep; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cecil\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Post Processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | abstract class AbstractOptimize extends AbstractStep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var string File type (ie: 'css') */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var mixed File processor */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |     public function init(array $options): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         if ($options['dry-run']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         if (false === $this->config->get(sprintf('optimize.%s.enabled', $this->type))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         if (true === $this->config->get('optimize.enabled')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |             $this->canProcess = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |     public function process(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |         $this->setProcessor(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |         $extensions = (array) $this->config->get(sprintf('optimize.%s.ext', $this->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |         if (empty($extensions)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             throw new RuntimeException(sprintf('The config key "optimize.%s.ext" is empty', $this->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |         $files = Finder::create() | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |             ->files() | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |             ->in($this->config->getOutputPath()) | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 1 |  |             ->name('/\.(' . implode('|', $extensions) . ')$/') | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |             ->notName('/\.min\.(' . implode('|', $extensions) . ')$/') | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |             ->sortByName(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |         $max = \count($files); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |         if ($max <= 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $this->builder->getLogger()->info('No files'); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |         $count = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |         $optimized = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |         $cache = new Cache($this->builder, 'optimized'); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         /** @var \Symfony\Component\Finder\SplFileInfo $file */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 1 |  |         foreach ($files as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |             $count++; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |             $sizeBefore = $file->getSize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |             $message = sprintf('File "%s" processed', $this->builder->isDebug() ? $file->getPathname() : $file->getRelativePathname()); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             $cacheKey = $cache->createKeyFromPath($file->getPathname(), $file->getRelativePathname()); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |             if (!$cache->has($cacheKey)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |                 $processed = $this->processFile($file); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |                 $sizeAfter = \strlen($processed); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |                 if ($sizeAfter < $sizeBefore) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |                     $message = sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |                         'File "%s" optimized (%s Ko -> %s Ko)', | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |                         $this->builder->isDebug() ? $file->getPathname() : $file->getRelativePathname(), | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |                         ceil($sizeBefore / 1000), | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |                         ceil($sizeAfter / 1000) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |                     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 1 |  |                 $cache->set($cacheKey, $this->encode($processed)); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 1 |  |                 $optimized++; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 1 |  |                 $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 1 |  |             $processed = $this->decode($cache->get($cacheKey)); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 1 |  |             Util\File::getFS()->dumpFile($file->getPathname(), $processed); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 1 |  |         if ($optimized == 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             $this->builder->getLogger()->info('Nothing to do'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Set file processor object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     abstract public function setProcessor(): void; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Process a file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Encode file content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |     public function encode(string $content = null): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Decode file content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |     public function decode(string $content = null): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  |