|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Benrowe\Laravel\Config\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use PDO as BasePdo; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Benrowe\Laravel\Config\Storage |
|
9
|
|
|
*/ |
|
10
|
|
|
class Pdo implements StorageInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var BasePdo |
|
14
|
|
|
*/ |
|
15
|
|
|
private $pdo; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $tableName; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $sqlQueries = [ |
|
26
|
|
|
'clear' => 'DELETE FROM %tablename%', |
|
27
|
|
|
'delete' => 'DELETE FROM %tablename% WHERE LOWER(LEFT(`key`, ?)) = ?', |
|
28
|
|
|
'load' => 'SELECT `key`, `value` FROM %tablename%', |
|
29
|
|
|
'save' => 'INSERT INTO %tablename% (`key`, `value`) VALUES (?,?)' |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* constructor |
|
34
|
|
|
* |
|
35
|
|
|
* @param BasePdo $pdo instance of pdo to execute the queries against |
|
36
|
|
|
* @param string $tableName the table name to reference |
|
37
|
|
|
* @param array $sqlQueries custom queries |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(BasePdo $pdo, $tableName = 'config', array $sqlQueries = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->pdo = $pdo; |
|
42
|
|
|
$this->tableName = $tableName; |
|
43
|
|
|
$this->setSqlQueries($sqlQueries); |
|
44
|
|
|
} |
|
45
|
|
|
/** |
|
46
|
|
|
* Set the sql queries to be used |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $queries additional queries to be used. Overrides the base |
|
49
|
|
|
* ones provided with this storage adapter |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setSqlQueries(array $queries) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->sqlQueries = array_merge($this->sqlQueries, $queries); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public function save($key, $value) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->delKey($key); |
|
62
|
|
|
|
|
63
|
|
|
if (is_array($value)) { |
|
64
|
|
|
foreach ($value as $i => $arrValue) { |
|
65
|
|
|
$this->saveKey($key.'['.$i.']', $arrValue); |
|
66
|
|
|
} |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->saveKey($key, $value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Save the specific key into the database |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $key |
|
77
|
|
|
* @param string|int|float $value |
|
78
|
|
|
*/ |
|
79
|
|
|
private function saveKey($key, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
$sql = $this->getSqlQuery('save'); |
|
82
|
|
|
$stmt = $this->pdo->prepare($sql); |
|
83
|
|
|
$stmt->execute([$key, $value]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function load() |
|
90
|
|
|
{ |
|
91
|
|
|
$sql = $this->getSqlQuery('load'); |
|
92
|
|
|
$stmt = $this->pdo->prepare($sql); |
|
93
|
|
|
$stmt->execute(); |
|
94
|
|
|
|
|
95
|
|
|
$results = []; |
|
96
|
|
|
foreach ($stmt->fetchAll() as $row) { |
|
97
|
|
|
$results[$row['key']] = $row['value']; |
|
98
|
|
|
} |
|
99
|
|
|
return $results; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function clear() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->pdo->exec($this->getSqlQuery('clear')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Generate the requested sql statement based on the provided name |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $name the query as referenced by it's name |
|
114
|
|
|
* @param array $vars any additional variables needed to generate the sql |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
private function getSqlQuery($name, array $vars = []) |
|
118
|
|
|
{ |
|
119
|
|
|
$sql = $this->sqlQueries[$name]; |
|
120
|
|
|
$vars = array_merge(['tablename' => $this->tableName], $vars); |
|
121
|
|
|
foreach ($vars as $key => $value) { |
|
122
|
|
|
$sql = str_replace("%$key%", $value, $sql); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $sql; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Delete the requested key from the database storage |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $key |
|
132
|
|
|
*/ |
|
133
|
|
|
private function delKey($key) |
|
134
|
|
|
{ |
|
135
|
|
|
$sql = $this->getSqlQuery('delete'); |
|
136
|
|
|
$stmt = $this->pdo->prepare($sql); |
|
137
|
|
|
$params = [strlen($key), $key]; |
|
138
|
|
|
$stmt->execute($params); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|