/
RedisSaver.php
74 lines (62 loc) · 1.81 KB
/
RedisSaver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Concrete\Core\Config\Driver\Redis;
use Concrete\Core\Config\SaverInterface;
use Illuminate\Support\Arr;
use Redis;
class RedisSaver implements SaverInterface
{
use RedisPaginatedTrait;
/**
* @var Redis
*/
protected $connection;
public function __construct(Redis $redis)
{
$this->connection = $redis;
}
/**
* Save config item.
*
* @param string $item
* @param string $value
* @param string $environment
* @param string $group
* @param string|null $namespace
*
* @return bool
*/
public function save($item, $value, $environment, $group, $namespace = null)
{
// First we gotta clear the item
$key = "{$namespace}::{$group}" . ($item ? ".{$item}" : '');
$deleteKeys = [$key];
foreach ($this->paginatedScan($this->connection, $key . '.*') as $key) {
$deleteKeys[] = $key;
}
if ($deleteKeys) {
$this->connection->del($deleteKeys);
}
// Now we can convert the value into a flat array and save each key
$valueList = $this->flattenValue($namespace, $group, $item, $value);
return $this->connection->mset($valueList);
}
/**
* Flatten a given value into a list of keys => serialized values
* ['a' => ['b' => 'c']] would become ['a.b' => 's:1:"c";']
*
* @param $namespace
* @param $group
* @param $item
* @param mixed $value
*
* @return mixed[]
*/
protected function flattenValue($namespace, $group, $item, $value)
{
$prefix = "{$namespace}::{$group}" . ($item ? ".{$item}" : '');
if (!is_array($value)) {
return [$prefix => serialize($value)];
}
return array_map('serialize', Arr::dot($value, $prefix . '.'));
}
}