-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathAppOptions.php
113 lines (95 loc) · 2.2 KB
/
AppOptions.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
class AppOptions extends ExtendedObject {
/**
* @var array
*/
protected $options;
/**
* @var array
*/
protected $changed;
public function __construct($options)
{
$this->options = $options;
$this->changed = [];
}
/**
* {@inheritdoc}
*/
public function get($option)
{
if (!array_key_exists($option, $this->options))
{
if (strpos($option, '___') !== false)
{
$option = str_replace('___', '.', $option);
return $this->get($option);
}
return null;
}
return $this->options[$option];
}
/**
* {@inheritdoc}
*/
public function set($option, $value)
{
if (!$this->exists($option))
{
throw new \LogicException("You need initialize this option manually.");
}
if (!in_array($option, $this->changed))
{
$this->changed[] = $option;
}
$this->options[$option] = $value;
}
/**
* {@inheritdoc}
*/
public function exists($key)
{
return array_key_exists($key, $this->options);
}
/**
* {@inheritdoc}
*/
public function remove($key)
{
throw new \LogicException("You can't delete options.");
}
/**
* Commits all changes in DB.
*/
public function commit()
{
$query = "
REPLACE INTO
`{{prefix}}settings`
(`setting`, `value`)
VALUES
";
$subquery = [];
$data = [];
foreach ($this->changed as $option)
{
$subquery[] = "(?, ?)";
$data[] = $option;
$data[] = $this->options[$option];
}
$query .= implode(', ', $subquery);
$db = \App::db();
$db->Prepare($query);
$db->Finish(true, $data);
}
public function create($optionName, $value)
{
$query = "
INSERT INTO `{{prefix}}settings` (`setting`, `value`)
VALUES (?, ?)
";
$db = \App::db();
$db->Prepare($query);
$db->Finish(true, [$optionName, $value]);
}
}