-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.php
More file actions
173 lines (152 loc) · 3.78 KB
/
Copy pathConfiguration.php
File metadata and controls
173 lines (152 loc) · 3.78 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @copyright 2018 Constantin Romankiewicz <constantin@zweiiconkram.de>
* @license Apache License 2.0; see LICENSE
*/
/**
* Allows configuring the application.
*
* @author Constantin Romankiewicz <constantin@zweiiconkram.de>
* @since 1.0
*/
class Configuration
{
/**
* Path to the file where the configuration is stored.
*
* @var string
*/
private static $configPath = 'configuration.json';
/**
* Singleton instance.
*
* @var Configuration
*/
private static $instance;
/**
* Holds the configuration after loaded.
*
* @var array
*/
private $config;
/**
* Creates a singleton instance if necessary and returns the instance.
*
* @return Configuration
*/
public static function getInstance()
{
if (!isset(self::$instance))
{
self::$instance = new Configuration;
}
return self::$instance;
}
/**
* Configuration constructor.
*/
private function __construct()
{
$this->load();
}
/**
* Saves the configuration to a file.
*
* @return void
*/
public function save()
{
$json = json_encode($this->config, JSON_PRETTY_PRINT);
if (!$h = fopen(self::$configPath, 'w'))
{
die('Could not open file ' . self::$configPath);
}
if (!fwrite($h, $json))
{
die('Could not write file ' . self::$configPath);
}
fclose($h);
}
/**
* Loads the configuration from a file.
*
* @return void
*/
public function load()
{
if (file_exists(self::$configPath))
{
$this->config = json_decode(file_get_contents(self::$configPath), true);
}
else
{
$this->config = array();
}
}
/**
* Hides a given language string from being displayed as unused or missing.
*
* @param string $extension Extension identifier.
* @param string $string The language string to hide.
* @param string $scope Either 'site' or 'admin'. Defaults to 'site'.
*
* @return void
*/
public function hideString($extension, $string, $scope = 'site')
{
if (!isset($this->config[$extension]))
{
$this->config[$extension] = array();
}
if (!isset($this->config[$extension]['hidden']))
{
$this->config[$extension]['hidden'] = array();
}
if (!isset($this->config[$extension]['hidden'][$scope]))
{
$this->config[$extension]['hidden'][$scope] = array();
}
if (!in_array($string, $this->config[$extension]['hidden'][$scope]))
{
array_push($this->config[$extension]['hidden'][$scope], $string);
sort($this->config[$extension]['hidden'][$scope]);
}
}
/**
* Shows a given language string, allowing again to it being displayed as unused or missing.
*
* @param string $extension Extension identifier.
* @param string $string The language string to hide.
* @param string $scope Either 'site' or 'admin'. Defaults to 'site'.
*
* @return void
*/
public function showString($extension, $string, $scope = 'site')
{
if (!isset($this->config[$extension]) || !isset($this->config[$extension]['hidden']) || !isset($this->config[$extension]['hidden'][$scope]))
{
return;
}
if (($index = array_search($string, $this->config[$extension]['hidden'][$scope])) !== false)
{
unset($this->config[$extension]['hidden'][$scope][$index]);
$this->config[$extension]['hidden'][$scope] = array_values($this->config[$extension]['hidden'][$scope]);
}
}
/**
* Gets all hidden strings for a given extension and scope.
*
* @param string $extension Extension identifier.
* @param string $scope Either 'site' or 'admin'. Defaults to 'site'.
*
* @return array
*/
public function getHiddenStrings($extension, $scope = 'site')
{
if (isset($this->config[$extension]) && isset($this->config[$extension]['hidden']) && isset($this->config[$extension]['hidden'][$scope]))
{
return $this->config[$extension]['hidden'][$scope];
}
return array();
}
}