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