1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: famoser |
5
|
|
|
* Date: 04/09/2017 |
6
|
|
|
* Time: 14:03 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Famoser\XKCDCache\Services; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Famoser\XKCDCache\Services\Base\BaseService; |
13
|
|
|
use Famoser\XKCDCache\Services\Interfaces\SettingServiceInterface; |
14
|
|
|
use Interop\Container\ContainerInterface; |
15
|
|
|
|
16
|
|
|
class SettingService extends BaseService implements SettingServiceInterface |
17
|
|
|
{ |
18
|
|
|
/* @var ContainerInterface $container */ |
19
|
|
|
private $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SettingService constructor. |
23
|
|
|
* @param ContainerInterface $container |
24
|
|
|
*/ |
25
|
19 |
|
public function __construct(ContainerInterface $container) |
26
|
|
|
{ |
27
|
19 |
|
parent::__construct($container); |
28
|
|
|
|
29
|
19 |
|
$this->container = $container; |
30
|
19 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $basePath |
34
|
|
|
* @param bool $debugMode |
35
|
|
|
* @param bool $testMode if set to true the database filename will be random |
36
|
|
|
* @return mixed[] |
37
|
|
|
*/ |
38
|
8 |
|
public static function generateRecommendedSettings(string $basePath, bool $debugMode, $testMode = false) |
39
|
|
|
{ |
40
|
8 |
|
$ds = DIRECTORY_SEPARATOR; |
41
|
|
|
|
42
|
8 |
|
$appBasePath = $basePath . $ds . "app"; |
43
|
8 |
|
$publicBasePath = $basePath . $ds . "public"; |
44
|
|
|
|
45
|
8 |
|
if ($testMode) { |
46
|
8 |
|
$dbConfig = ['db_path' => $appBasePath . $ds . "data" . $ds . "data" . uniqid() . ".db"]; |
47
|
|
|
} else { |
48
|
|
|
$dbConfig = ['db_path' => $appBasePath . $ds . "data" . $ds . "data.db"]; |
49
|
|
|
} |
50
|
|
|
return [ |
51
|
8 |
|
'displayErrorDetails' => $debugMode, |
52
|
8 |
|
'debug_mode' => $debugMode, |
53
|
8 |
|
'db_template_path' => $appBasePath . $ds . "data_templates" . $ds . "data_template.db", |
54
|
8 |
|
'file_path' => $appBasePath . $ds . "files", |
55
|
8 |
|
'cache_path' => $appBasePath . $ds . "cache", |
56
|
8 |
|
'src_path' => $basePath . $ds . "src", |
57
|
8 |
|
'log_file_path' => $appBasePath . $ds . "logs" . $ds . "log.log", |
58
|
8 |
|
'template_path' => $appBasePath . $ds . "templates", |
59
|
8 |
|
'public_path' => $publicBasePath, |
60
|
8 |
|
'image_cache_path' => $publicBasePath . $ds . "images" . $ds . "xkcd", |
61
|
8 |
|
'image_public_base_path' => "/images/xkcd", |
62
|
8 |
|
'zip_cache_path' => $publicBasePath . $ds . "zip", |
63
|
8 |
|
'max_refresh_images' => 10 |
64
|
8 |
|
] + $dbConfig; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* get the array with all the settings |
69
|
|
|
* |
70
|
|
|
* @return mixed[] |
71
|
|
|
*/ |
72
|
19 |
|
protected function getSettingArray() |
73
|
|
|
{ |
74
|
19 |
|
return $this->container->get($this->getSettingKey()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* indicates if the application is in debug mode |
79
|
|
|
* |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
9 |
|
public function getDebugMode() |
83
|
|
|
{ |
84
|
9 |
|
return $this->getSettingArray()["debug_mode"]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* gets the path of the active database |
89
|
|
|
* if the database does not exist, a new one is created by copying the template database |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
19 |
|
public function getDbPath() |
94
|
|
|
{ |
95
|
19 |
|
return $this->getSettingArray()["db_path"]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* gets the path of the database template. see at getDbPath what the database template is used for |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
19 |
|
public function getDbTemplatePath() |
104
|
|
|
{ |
105
|
19 |
|
return $this->getSettingArray()["db_template_path"]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* directory which can be written to by libraries which need a cache |
110
|
|
|
* this directory will not persist in different versions of the application |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
8 |
|
public function getCachePath() |
115
|
|
|
{ |
116
|
8 |
|
return $this->getSettingArray()["cache_path"]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* get the log file path |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
5 |
|
public function getLogFilePath() |
125
|
|
|
{ |
126
|
5 |
|
return $this->getSettingArray()["log_file_path"]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* path where twig templates are located |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
8 |
|
public function getTemplatePath() |
135
|
|
|
{ |
136
|
8 |
|
return $this->getSettingArray()["template_path"]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* get the path which is exposed to the public |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getPublicPath() |
145
|
|
|
{ |
146
|
|
|
return $this->getSettingArray()["public_path"]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* get the path where the xkcd images should be cached |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
3 |
|
public function getImageCachePath() |
155
|
|
|
{ |
156
|
3 |
|
return $this->getSettingArray()["image_cache_path"]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* get the path where the XKCD images are accessible from outside |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
2 |
|
public function getImagePublicBasePath() |
165
|
|
|
{ |
166
|
2 |
|
return $this->getSettingArray()["image_public_base_path"]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* get the path where the zips should be cached |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
3 |
|
public function getZipCachePath() |
175
|
|
|
{ |
176
|
3 |
|
return $this->getSettingArray()["zip_cache_path"]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* get the key the settings are saved under in the container |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
19 |
|
public static function getSettingKey() |
185
|
|
|
{ |
186
|
19 |
|
return "settings"; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* maximum number of downloaded images of one request to /refresh |
191
|
|
|
* |
192
|
|
|
* @return int |
193
|
|
|
*/ |
194
|
3 |
|
public function getMaxRefreshImages() |
195
|
|
|
{ |
196
|
3 |
|
return $this->getSettingArray()["max_refresh_images"]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* get the path which is the root of the PSR namespace |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function getSrcPath() |
205
|
|
|
{ |
206
|
|
|
return $this->getSettingArray()["src_path"]; |
207
|
|
|
} |
208
|
|
|
} |