1 | <?php |
||
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 |