|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
|
5
|
|
|
* @copyright Marwan Al-Soltany 2021 |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace MAKS\Velox\Frontend; |
|
13
|
|
|
|
|
14
|
|
|
use MAKS\Velox\Backend\Event; |
|
15
|
|
|
use MAKS\Velox\Backend\Config; |
|
16
|
|
|
use MAKS\Velox\Helper\Misc; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A class that serves as an abstracted data bag/store that is accessible via dot-notation. |
|
20
|
|
|
* |
|
21
|
|
|
* Example: |
|
22
|
|
|
* ``` |
|
23
|
|
|
* // get all data |
|
24
|
|
|
* $allData = Data::getAll(); |
|
25
|
|
|
* |
|
26
|
|
|
* // check for variable availability |
|
27
|
|
|
* $someVarExists = Data::has('someVar'); |
|
28
|
|
|
* |
|
29
|
|
|
* // get a specific variable or fall back to a default value |
|
30
|
|
|
* $someVar = Data::get('someVar', 'fallbackValue'); |
|
31
|
|
|
* |
|
32
|
|
|
* // set a specific variable value |
|
33
|
|
|
* Data::set('someNewVar.someKey', 'someValue'); |
|
34
|
|
|
* ``` |
|
35
|
|
|
* |
|
36
|
|
|
* @package Velox\Frontend |
|
37
|
|
|
* @since 1.0.0 |
|
38
|
|
|
* @api |
|
39
|
|
|
*/ |
|
40
|
|
|
class Data |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* This event will be dispatched when the data is loaded. |
|
44
|
|
|
* This event will be passed a reference to the data array. |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
public const ON_LOAD = 'data.on.load'; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The currently loaded data. |
|
53
|
|
|
*/ |
|
54
|
|
|
protected static array $bag = []; |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Loads the data from system configuration. |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
8 |
|
protected static function load(): void |
|
63
|
|
|
{ |
|
64
|
8 |
|
if (empty(static::$bag)) { |
|
65
|
1 |
|
static::$bag = (array)Config::get('data', static::$bag); |
|
66
|
|
|
|
|
67
|
|
|
// make `Config::$config['data']` points to `Data::$bag` (the same array in memory) |
|
68
|
1 |
|
$config = &Config::getReference(); |
|
69
|
1 |
|
$config['data'] = &static::$bag; |
|
70
|
|
|
|
|
71
|
1 |
|
Event::dispatch(self::ON_LOAD, [&static::$bag]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks whether a value of a key exists in `self::$bag` via dot-notation. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key The dotted key representation. |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public static function has(string $key): bool |
|
83
|
|
|
{ |
|
84
|
1 |
|
static::load(); |
|
85
|
|
|
|
|
86
|
1 |
|
$value = Misc::getArrayValueByKey(self::$bag, $key, null); |
|
87
|
|
|
|
|
88
|
1 |
|
return isset($value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets a value of a key from `self::$bag` via dot-notation. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $key The dotted key representation. |
|
95
|
|
|
* @param mixed $default [optional] The default fallback value. |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed The requested value or null. |
|
98
|
|
|
*/ |
|
99
|
5 |
|
public static function get(string $key, $default = null) |
|
100
|
|
|
{ |
|
101
|
5 |
|
static::load(); |
|
102
|
|
|
|
|
103
|
5 |
|
return Misc::getArrayValueByKey(self::$bag, $key, $default); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Sets a value of a key in `self::$bag` via dot-notation. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $key The dotted key representation. |
|
110
|
|
|
* @param mixed $value The value to set. |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
8 |
|
public static function set(string $key, $value): void |
|
115
|
|
|
{ |
|
116
|
8 |
|
static::load(); |
|
117
|
|
|
|
|
118
|
8 |
|
Misc::setArrayValueByKey(self::$bag, $key, $value); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the currently loaded data. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public static function getAll(): ?array |
|
127
|
|
|
{ |
|
128
|
1 |
|
static::load(); |
|
129
|
|
|
|
|
130
|
1 |
|
return static::$bag; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|