|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\components; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\thememanager\storage\SettingsStorageInterface; |
|
|
|
|
|
|
14
|
|
|
use Yii; |
|
15
|
|
|
use yii\base\Component; |
|
16
|
|
|
use yii\base\Model; |
|
17
|
|
|
use yii\caching\Cache; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class ThemeSettingsStorage extends Component implements SettingsStorageInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @return SettingsStorage |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function getStorage() |
|
25
|
|
|
{ |
|
26
|
|
|
return Yii::$app->get('settingsStorage'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
public $cacheKey; |
|
33
|
|
|
|
|
34
|
|
|
public function init() |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->cacheKey === null && !Yii::$app->user->getIsGuest()) { |
|
37
|
|
|
$this->cacheKey = ['themeSettingsStorage', Yii::$app->user->id]; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return Cache |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function getCache() |
|
45
|
|
|
{ |
|
46
|
|
|
return Yii::$app->get('cache'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function set(Model $model) |
|
53
|
|
|
{ |
|
54
|
|
|
if (Yii::$app->user->getIsGuest()) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$data = $model->toArray(); |
|
59
|
|
|
|
|
60
|
|
|
$this->getStorage()->setBounded('theme', $data); |
|
61
|
|
|
$this->setToCache($data); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
public function get() |
|
68
|
|
|
{ |
|
69
|
|
|
if (Yii::$app->user->getIsGuest()) { |
|
70
|
|
|
return []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (($cached = $this->getFromCache()) !== false) { |
|
74
|
|
|
return $cached; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$storage = $this->getStorage()->getBounded('theme'); |
|
78
|
|
|
$this->setToCache($storage); |
|
79
|
|
|
|
|
80
|
|
|
return $storage; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function getFromCache() |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->cacheKey === null) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->getCache()->get($this->cacheKey); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function setToCache($data) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->cacheKey === null) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->getCache()->set($this->cacheKey, $data, 86400); // 1 day |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: