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