1
|
|
|
<?php namespace Limoncello\Application\Settings; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Application\Contracts\Settings\CacheSettingsProviderInterface; |
20
|
|
|
use Limoncello\Application\CoreSettings\CoreData; |
21
|
|
|
use Limoncello\Application\Exceptions\AmbiguousSettingsException; |
22
|
|
|
use Limoncello\Application\Exceptions\NotRegisteredSettingsException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Limoncello\Application |
26
|
|
|
*/ |
27
|
|
|
class CacheSettingsProvider implements CacheSettingsProviderInterface |
28
|
|
|
{ |
29
|
|
|
/** Internal data index */ |
30
|
|
|
const KEY_CORE_DATA = 0; |
31
|
|
|
|
32
|
|
|
/** Internal data index */ |
33
|
|
|
const KEY_SETTINGS_MAP = self::KEY_CORE_DATA + 1; |
34
|
|
|
|
35
|
|
|
/** Internal data index */ |
36
|
|
|
const KEY_SETTINGS_DATA = self::KEY_SETTINGS_MAP + 1; |
37
|
|
|
|
38
|
|
|
/** Internal data index */ |
39
|
|
|
const KEY_AMBIGUOUS_MAP = self::KEY_SETTINGS_DATA + 1; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $coreData = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $settingsMap = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $settingsData = []; |
55
|
5 |
|
|
56
|
|
|
/** |
57
|
5 |
|
* @var array |
58
|
2 |
|
*/ |
59
|
1 |
|
private $ambiguousMap = []; |
60
|
|
|
|
61
|
1 |
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
3 |
|
public function get(string $className): array |
65
|
|
|
{ |
66
|
3 |
|
if ($this->has($className) === false) { |
67
|
|
|
if (array_key_exists($className, $this->ambiguousMap) === true) { |
68
|
|
|
throw new AmbiguousSettingsException($className); |
69
|
|
|
} |
70
|
|
|
throw new NotRegisteredSettingsException($className); |
71
|
|
|
} |
72
|
5 |
|
|
73
|
|
|
$data = $this->settingsData[$this->settingsMap[$className]]; |
74
|
5 |
|
|
75
|
|
|
return $data; |
76
|
5 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function getCoreData(): array |
82
|
1 |
|
{ |
83
|
|
|
return $this->coreData; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function has(string $className): bool |
90
|
|
|
{ |
91
|
|
|
$result = array_key_exists($className, $this->settingsMap); |
92
|
|
|
|
93
|
|
|
return $result; |
94
|
5 |
|
} |
95
|
|
|
|
96
|
5 |
|
/** |
97
|
5 |
|
* @inheritdoc |
98
|
5 |
|
*/ |
99
|
5 |
|
public function isAmbiguous(string $className): bool |
100
|
|
|
{ |
101
|
|
|
$result = array_key_exists($className, $this->ambiguousMap); |
102
|
5 |
|
|
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param CoreData $coreData |
108
|
2 |
|
* @param InstanceSettingsProvider $provider |
109
|
|
|
* |
110
|
|
|
* @return CacheSettingsProvider |
111
|
2 |
|
*/ |
112
|
2 |
|
public function setInstanceSettings(CoreData $coreData, InstanceSettingsProvider $provider): CacheSettingsProvider |
113
|
2 |
|
{ |
114
|
|
|
$this->unserialize([ |
115
|
|
|
static::KEY_CORE_DATA => $coreData->get(), |
116
|
|
|
static::KEY_SETTINGS_MAP => $provider->getSettingsMap(), |
117
|
|
|
static::KEY_SETTINGS_DATA => $provider->getSettingsData(), |
118
|
|
|
static::KEY_AMBIGUOUS_MAP => $provider->getAmbiguousMap(), |
119
|
|
|
]); |
120
|
5 |
|
|
121
|
|
|
return $this; |
122
|
5 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
|
|
public function serialize(): array |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
static::KEY_CORE_DATA => $this->coreData, |
131
|
|
|
static::KEY_SETTINGS_MAP => $this->settingsMap, |
132
|
|
|
static::KEY_SETTINGS_DATA => $this->settingsData, |
133
|
|
|
static::KEY_AMBIGUOUS_MAP => $this->ambiguousMap, |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function unserialize(array $serialized): void |
141
|
|
|
{ |
142
|
|
|
list ( |
143
|
|
|
static::KEY_CORE_DATA => $this->coreData, |
144
|
|
|
static::KEY_SETTINGS_MAP => $this->settingsMap, |
145
|
|
|
static::KEY_SETTINGS_DATA => $this->settingsData, |
146
|
|
|
static::KEY_AMBIGUOUS_MAP => $this->ambiguousMap, |
147
|
|
|
) = $serialized; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|