1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of BlitzPHP Parametres. |
5
|
|
|
* |
6
|
|
|
* (c) 2025 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Parametres\Handlers; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Gestionnaire de paramètres via des tableaux |
16
|
|
|
* |
17
|
|
|
* Utilise le stockage local pour gérer les requêtes de paramètres non persistantes. |
18
|
|
|
* Utile principalement pour les tests ou l'extension par de vrais gestionnaires persistants. |
19
|
|
|
*/ |
20
|
|
|
class ArrayHandler extends BaseHandler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Stockage pour les paramètres généraux. |
24
|
|
|
* Format: ['file' => ['property' => ['value', 'type']]] |
25
|
|
|
* |
26
|
|
|
* @var array<string,array<string,list<mixed>>> |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
private array $general = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Stockage des paramètres contextuels. |
32
|
|
|
* Format: ['context' => ['file' => ['property' => ['value', 'type']]]] |
33
|
|
|
* |
34
|
|
|
* @var array<string,list<mixed>|null> |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
private array $contexts = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
|
|
public function has(string $file, string $property, ?string $context = null): bool |
42
|
|
|
{ |
43
|
2 |
|
return $this->hasStored($file, $property, $context); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function get(string $file, string $property, ?string $context = null): mixed |
50
|
|
|
{ |
51
|
2 |
|
return $this->getStored($file, $property, $context); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function set(string $file, string $property, mixed $value = null, ?string $context = null): void |
58
|
|
|
{ |
59
|
2 |
|
$this->setStored($file, $property, $value, $context); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function forget(string $file, string $property, ?string $context = null): void |
66
|
|
|
{ |
67
|
|
|
$this->forgetStored($file, $property, $context); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function flush(): void |
74
|
|
|
{ |
75
|
|
|
$this->general = []; |
76
|
|
|
$this->contexts = []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Vérifie si cette valeur est stockée. |
81
|
|
|
*/ |
82
|
|
|
protected function hasStored(string $file, string $property, ?string $context = null): bool |
83
|
|
|
{ |
84
|
|
|
if ($context === null) { |
85
|
2 |
|
return isset($this->general[$file]) && array_key_exists($property, $this->general[$file]); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return isset($this->contexts[$context][$file]) && array_key_exists($property, $this->contexts[$context][$file]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Récupère une valeur de la mémoire. |
93
|
|
|
* |
94
|
|
|
* @return mixed|null |
95
|
|
|
*/ |
96
|
|
|
protected function getStored(string $file, string $property, ?string $context = null): mixed |
97
|
|
|
{ |
98
|
|
|
if (! $this->has($file, $property, $context)) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $context === null |
103
|
|
|
? $this->parseValue(...$this->general[$file][$property]) |
|
|
|
|
104
|
2 |
|
: $this->parseValue(...$this->contexts[$context][$file][$property]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Ajoute des valeurs à la mémoire. |
109
|
|
|
*/ |
110
|
|
|
protected function setStored(string $file, string $property, mixed $value, ?string $context = null): void |
111
|
|
|
{ |
112
|
2 |
|
$type = gettype($value); |
113
|
2 |
|
$value = $this->prepareValue($value); |
114
|
|
|
|
115
|
|
|
if ($context === null) { |
116
|
|
|
$this->general[$file][$property] = [ |
117
|
|
|
$value, |
118
|
|
|
$type, |
119
|
2 |
|
]; |
120
|
|
|
} else { |
121
|
|
|
$this->contexts[$context][$file][$property] = [ |
122
|
|
|
$value, |
123
|
|
|
$type, |
124
|
2 |
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Supprime un élément de la mémoire. |
130
|
|
|
*/ |
131
|
|
|
protected function forgetStored(string $file, string $property, ?string $context): void |
132
|
|
|
{ |
133
|
|
|
if ($context === null) { |
134
|
|
|
unset($this->general[$file][$property]); |
135
|
|
|
} else { |
136
|
|
|
unset($this->contexts[$context][$file][$property]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|