| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Cecil. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Cecil\Step\Assets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cecil\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Cecil\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cecil\Step\AbstractStep; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cecil\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Save assets step. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * This step is responsible for saving assets to the output directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * It copies files from the cache to the output directory, ensuring that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * assets are available for the final build. If the cache is disabled, it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * clears the cache directory before processing assets. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class Save extends AbstractStep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected Cache $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected string $cacheKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |     public function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         return 'Saving assets'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |     public function init(array $options): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // last build step: should clear cache? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $this->clearCacheIfDisabled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         if ($options['dry-run']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         $this->cache = new Cache($this->builder, 'assets'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         $this->cacheKey = \sprintf('_list__%s', $this->builder->getVersion()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         if (empty($this->builder->getAssets()) && $this->cache->has($this->cacheKey)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->builder->setAssets($this->cache->get($this->cacheKey)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $this->canProcess = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Note: a file from `static/` with the same name will NOT be overridden. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |     public function process(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |         $total = \count($this->builder->getAssets()); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         if ($total > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |             $count = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |             foreach ($this->builder->getAssets() as $path) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 // if file deleted from cache... | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |                 if (!Util\File::getFS()->exists($this->cache->getContentFilePathname($path))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                     $this->builder->getLogger()->warning(\sprintf('Asset "%s" not found in cache, skipping. You should clear all cache.', $path)); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                     break; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |                 $count++; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |                 Util\File::getFS()->copy($this->cache->getContentFilePathname($path), Util::joinFile($this->config->getOutputPath(), $path), false); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 1 |  |                 $message = \sprintf('Asset "%s" saved', $path); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |                 $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |             $this->cache->set($this->cacheKey, $this->builder->getAssets()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Deletes cache directory, if cache is disabled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     private function clearCacheIfDisabled(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         if (!$this->config->isEnabled('cache')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 Util\File::getFS()->remove($this->config->getCachePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } catch (\Exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 throw new RuntimeException(\sprintf('Unable to remove cache directory "%s".', $this->config->getCachePath())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |