|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Plugin\Response\Databag; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\App\Databag\Databag; |
|
6
|
|
|
use Jaxon\App\Databag\DatabagContext; |
|
7
|
|
|
use Jaxon\Di\Container; |
|
8
|
|
|
use Jaxon\Plugin\AbstractResponsePlugin; |
|
9
|
|
|
|
|
10
|
|
|
use function is_array; |
|
11
|
|
|
use function is_string; |
|
12
|
|
|
use function json_decode; |
|
13
|
|
|
|
|
14
|
|
|
class DatabagPlugin extends AbstractResponsePlugin |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @const The plugin name |
|
18
|
|
|
*/ |
|
19
|
|
|
public const NAME = 'bags'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Databag |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $xDatabag = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The constructor |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(protected Container $di) |
|
30
|
|
|
{} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
private function initDatabag(): void |
|
36
|
|
|
{ |
|
37
|
|
|
if($this->xDatabag !== null) |
|
38
|
|
|
{ |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Get the databag contents from the HTTP request parameters. |
|
43
|
|
|
$xRequest = $this->di->getRequest(); |
|
44
|
|
|
$aBody = $xRequest->getParsedBody(); |
|
45
|
|
|
$aParams = $xRequest->getQueryParams(); |
|
46
|
|
|
$aData = is_array($aBody) ? |
|
47
|
|
|
$this->readData($aBody['jxnbags'] ?? []) : |
|
48
|
|
|
$this->readData($aParams['jxnbags'] ?? []); |
|
49
|
|
|
$this->xDatabag = new Databag($this, $aData); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getName(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return self::NAME; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param mixed $xData |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
private function readData($xData): array |
|
66
|
|
|
{ |
|
67
|
|
|
// Todo: clean input data. |
|
68
|
|
|
// Todo: verify the checksums. |
|
69
|
|
|
return is_string($xData) ? |
|
70
|
|
|
(json_decode($xData, true) ?: []) : |
|
71
|
|
|
(is_array($xData) ? $xData : []); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritDoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getHash(): string |
|
78
|
|
|
{ |
|
79
|
|
|
// Use the version number as hash |
|
80
|
|
|
return '4.0.0'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function writeCommand(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->initDatabag(); |
|
89
|
|
|
if($this->xDatabag->touched()) |
|
90
|
|
|
{ |
|
91
|
|
|
// Todo: calculate the checksums. |
|
92
|
|
|
$this->addCommand('databag.set', ['values' => $this->xDatabag]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $sName |
|
98
|
|
|
* |
|
99
|
|
|
* @return DatabagContext |
|
100
|
|
|
*/ |
|
101
|
|
|
public function bag(string $sName): DatabagContext |
|
102
|
|
|
{ |
|
103
|
|
|
$this->initDatabag(); |
|
104
|
|
|
return new DatabagContext($this->xDatabag, $sName); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|