1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* HiPanel core package |
5
|
|
|
* |
6
|
|
|
* @link https://hipanel.com/ |
7
|
|
|
* @package hipanel-core |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\components; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\Component; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class OrientationStorage |
19
|
|
|
* |
20
|
|
|
* @package hipanel\components |
21
|
|
|
*/ |
22
|
|
|
class OrientationStorage extends Component |
23
|
|
|
{ |
24
|
|
|
const ORIENTATION_HORIZONTAL = 'horizontal'; |
25
|
|
|
const ORIENTATION_VERTICAL = 'vertical'; |
26
|
|
|
|
27
|
|
|
public $defaultOrientation = self::ORIENTATION_HORIZONTAL; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string the cache key that will be used to cache storage values |
31
|
|
|
*/ |
32
|
|
|
public $cacheKey; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $settingsStorageKey = 'orientations'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $storage; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function init() |
48
|
|
|
{ |
49
|
|
|
if (!isset($this->cacheKey)) { |
50
|
|
|
$this->cacheKey = ['orientationStorage', Yii::$app->user->id]; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \yii\caching\Cache |
56
|
|
|
*/ |
57
|
|
|
protected function getCache() |
58
|
|
|
{ |
59
|
|
|
return Yii::$app->getCache(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return SettingsStorageInterface |
64
|
|
|
*/ |
65
|
|
|
protected function getSettingsStorage() |
66
|
|
|
{ |
67
|
|
|
return Yii::$app->get('settingsStorage'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Ensures that [[storage]] contains actual orientations storage |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
private function ensureStorage() |
76
|
|
|
{ |
77
|
|
|
if (!isset($this->storage)) { |
78
|
|
|
$this->storage = $this->getCache()->get($this->cacheKey); |
|
|
|
|
79
|
|
|
|
80
|
|
|
if ($this->storage === false) { |
81
|
|
|
$this->storage = $this->getSettingsStorage()->getBounded($this->settingsStorageKey); |
82
|
|
|
$this->cache(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->storage; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Saves current [[storage]] |
91
|
|
|
*/ |
92
|
|
|
protected function saveStorage() |
93
|
|
|
{ |
94
|
|
|
$this->getSettingsStorage()->setBounded($this->settingsStorageKey, $this->storage); |
95
|
|
|
$this->cache(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Caches current [[storage]] |
100
|
|
|
*/ |
101
|
|
|
protected function cache() |
102
|
|
|
{ |
103
|
|
|
$this->getCache()->set($this->cacheKey, $this->storage, 86400); // 1 day |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets orientation for the $route |
108
|
|
|
* |
109
|
|
|
* @param string $route |
110
|
|
|
* @param string $orientation |
111
|
|
|
*/ |
112
|
|
|
public function set($route, $orientation) |
113
|
|
|
{ |
114
|
|
|
$this->ensureStorage(); |
115
|
|
|
|
116
|
|
|
$this->storage[$route] = $orientation; |
117
|
|
|
$this->saveStorage(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets orientation for the $route |
122
|
|
|
* |
123
|
|
|
* @param $route |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function get($route) |
127
|
|
|
{ |
128
|
|
|
$this->ensureStorage(); |
129
|
|
|
|
130
|
|
|
return isset($this->storage[$route]) ? $this->storage[$route] : $this->defaultOrientation; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..