| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Yii2 Pages Module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link      https://github.com/hiqdev/yii2-module-pages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package   yii2-module-pages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace hiqdev\yii2\modules\pages\models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use hiqdev\yii2\modules\pages\storage\FileSystemStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yii\data\ArrayDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use yii\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class PagesList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var AbstractPage[]  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected array $pages = []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * PagesList constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param AbstractPage[] $pages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(array $pages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->pages = $pages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param FileSystemStorage $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return PagesList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @throws \yii\base\InvalidConfigException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public static function createFromDir(string $path, FileSystemStorage $storage): PagesList | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $list = $storage->getFileSystem()->listContents($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         ArrayHelper::multisort($list, 'basename', SORT_DESC); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $pages = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         foreach ($list as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             if ($file['type'] !== 'file' || $file['basename'][0] === '.') { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $pages[] = AbstractPage::createFromFile($file['path'], $storage); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $index = new static($pages, $storage); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         return $index; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return ArrayDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getDataProvider(): ArrayDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return new ArrayDataProvider([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'allModels' => $this->pages, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'pagination' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'pageSize' => Yii::$app->getModule('pages')->getPageSize(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  |