|
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; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\yii2\modules\pages\interfaces\PageInterface; |
|
14
|
|
|
use hiqdev\yii2\modules\pages\models\AbstractPage; |
|
15
|
|
|
use hiqdev\yii2\modules\pages\models\PagesList; |
|
16
|
|
|
use hiqdev\yii2\modules\pages\interfaces\StorageInterface; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
|
|
19
|
|
|
class Module extends \yii\base\Module |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array|StorageInterface */ |
|
22
|
|
|
protected $_storage; |
|
23
|
|
|
|
|
24
|
|
|
/** @var int */ |
|
25
|
|
|
private $pageSize = 5; |
|
26
|
|
|
|
|
27
|
|
|
public static function getInstance(): Module |
|
28
|
|
|
{ |
|
29
|
|
|
return Yii::$app->getModule('pages'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* This to use standard app pathes for views and layouts. |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getViewPath() |
|
37
|
|
|
{ |
|
38
|
|
|
return Yii::$app->getViewPath(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $pageName |
|
43
|
|
|
* @return AbstractPage|null |
|
44
|
|
|
* @throws \yii\base\InvalidConfigException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function find(string $pageName): ?PageInterface |
|
47
|
|
|
{ |
|
48
|
|
|
$page = $this->getStorage()->getPage($pageName); |
|
49
|
|
|
|
|
50
|
|
|
return $page; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string|null $id |
|
55
|
|
|
* @return PagesList|null |
|
56
|
|
|
* @throws \yii\base\InvalidConfigException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function findList(string $id = null): ?PagesList |
|
59
|
|
|
{ |
|
60
|
|
|
$list = $this->getStorage()->getList($id); |
|
61
|
|
|
|
|
62
|
|
|
return $list; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param array $storageConfig |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setStorage($storageConfig): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->_storage = $storageConfig; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return StorageInterface |
|
75
|
|
|
* @throws \yii\base\InvalidConfigException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getStorage(): StorageInterface |
|
78
|
|
|
{ |
|
79
|
|
|
if (!is_object($this->_storage)) { |
|
80
|
|
|
$this->_storage = Yii::createObject($this->_storage); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->_storage; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param int $pageSize |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setPageSize(int $pageSize): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->pageSize = $pageSize; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getPageSize(): int |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->pageSize; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|