| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Cecil/Cecil package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Cecil\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cecil\Collection\Page\Page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cecil\Collection\Page\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cecil\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Class Generator\Homepage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Homepage extends AbstractGenerator implements GeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 | 1 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public function generate(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 1 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 1 |  |         // creates a new index page... | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 1 |  |         $page = (new Page('index'))->setPath('')->setVariable('title', 'Home'); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 1 |  |         // ... clones it if already exists | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 1 |  |         if ($this->builder->getPages()->has('index')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             $page = clone $this->builder->getPages()->get('index'); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         // collects all pages | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $subPages = $this->builder->getPages()->filter(function (Page $page) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |             return $page->getType() == TYPE::PAGE | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 && $page->isVirtual() === false // excludes virtual pages | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 1 |  |                 && $page->getVariable('exclude') !== true; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         /** @var \Cecil\Collection\Page\Page $page */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has($page->getVariable('pagesfrom'))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |             $subPages = $this->builder->getPages()->get($page->getVariable('pagesfrom'))->getVariable('pages'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         // sorts | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         /** @var \Cecil\Collection\Page\Collection $subPages */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         /** @var \Cecil\Collection\Page\Page $page */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $pages = $subPages->sortByDate(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |         if ($page->hasVariable('sortby')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |             $sortMethod = sprintf('sortBy%s', ucfirst((string) $page->getVariable('sortby'))); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |             if (!method_exists($pages, $sortMethod)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 throw new RuntimeException(\sprintf('In "%s" section "%s" is not a valid value for "sortby" variable.', $page->getId(), $page->getVariable('sortby'))); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |             $pages = $pages->$sortMethod(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         /** @var \Cecil\Collection\Page\Page $page */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $page->setType(Type::HOMEPAGE) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             ->setVariable('pages', $pages); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         if ($pages->first()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $page->setVariable('date', $pages->first()->getVariable('date')); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         // default menu | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         if (!$page->getVariable('menu')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $page->setVariable('menu', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 'main' => ['weight' => 0], | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $this->generatedPages->add($page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  |