Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
42.11% |
40 / 95 |
|
33.33% |
8 / 24 |
CRAP | |
0.00% |
0 / 1 |
config | |
42.11% |
40 / 95 |
|
33.33% |
8 / 24 |
384.31 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
system_get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
get_time_remaining | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
get_memory_remaining | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
set_finished_task | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
set_active_module | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get_progress_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
load_config | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
create_progress_restart_point | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
jump_to_restart_point | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
has_restart_point | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
save_config | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
increment_current_task_progress | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
set_current_task_progress | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
set_task_progress_count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get_current_task_progress | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_task_progress_count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
set_finished_navigation_stage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
set_active_navigation_stage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_navigation_data | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
clean_up_config_file | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setup_system_data | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\install\helper; |
15 | |
16 | use phpbb\install\exception\installer_config_not_writable_exception; |
17 | |
18 | /** |
19 | * Stores common settings and installation status |
20 | */ |
21 | class config |
22 | { |
23 | /** |
24 | * @var \phpbb\filesystem\filesystem_interface |
25 | */ |
26 | protected $filesystem; |
27 | |
28 | /** |
29 | * Array which contains config settings for the installer |
30 | * |
31 | * The array will also store all the user input, as well as any |
32 | * data that is passed to other tasks by a task. |
33 | * |
34 | * @var array |
35 | */ |
36 | protected $installer_config; |
37 | |
38 | /** |
39 | * @var string |
40 | */ |
41 | protected $install_config_file; |
42 | |
43 | /** |
44 | * @var \bantu\IniGetWrapper\IniGetWrapper |
45 | */ |
46 | protected $php_ini; |
47 | |
48 | /** |
49 | * @var string |
50 | */ |
51 | protected $phpbb_root_path; |
52 | |
53 | /** |
54 | * Array containing progress information |
55 | * |
56 | * @var array |
57 | */ |
58 | protected $progress_data; |
59 | |
60 | /** |
61 | * Array containing system information |
62 | * |
63 | * The array contains run time and memory limitations. |
64 | * |
65 | * @var array |
66 | */ |
67 | protected $system_data; |
68 | |
69 | /** |
70 | * Array containing navigation bar information |
71 | * |
72 | * @var array |
73 | */ |
74 | protected $navigation_data; |
75 | |
76 | /** |
77 | * Flag indicating that config file should be cleaned up |
78 | * |
79 | * @var bool |
80 | */ |
81 | protected $do_clean_up; |
82 | |
83 | /** |
84 | * Constructor |
85 | */ |
86 | public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, \bantu\IniGetWrapper\IniGetWrapper $php_ini, $phpbb_root_path) |
87 | { |
88 | $this->filesystem = $filesystem; |
89 | $this->php_ini = $php_ini; |
90 | $this->phpbb_root_path = $phpbb_root_path; |
91 | $this->do_clean_up = false; |
92 | |
93 | // Set up data arrays |
94 | $this->navigation_data = array(); |
95 | $this->installer_config = array(); |
96 | $this->system_data = array(); |
97 | $this->progress_data = array( |
98 | 'last_task_module_name' => '', // Stores the service name of the latest finished module |
99 | 'last_task_module_index' => 0, // Stores the index of the latest finished module |
100 | 'last_task_index' => 0, // Stores the index of the latest finished task |
101 | 'max_task_progress' => 0, |
102 | 'current_task_progress' => 0, |
103 | '_restart_points' => array(), |
104 | 'use_restart_point' => false, |
105 | ); |
106 | |
107 | $this->install_config_file = $this->phpbb_root_path . 'store/install_config.php'; |
108 | |
109 | $this->setup_system_data(); |
110 | } |
111 | |
112 | /** |
113 | * Returns data for a specified parameter |
114 | * |
115 | * @param string $param_name Name of the parameter to return |
116 | * @param mixed $default Default value to return when the specified data |
117 | * does not exist. |
118 | * |
119 | * @return mixed value of the specified parameter or the default value if the data |
120 | * cannot be recovered. |
121 | */ |
122 | public function get($param_name, $default = false) |
123 | { |
124 | return (isset($this->installer_config[$param_name])) ? $this->installer_config[$param_name] : $default; |
125 | } |
126 | |
127 | /** |
128 | * Sets a parameter in installer_config |
129 | * |
130 | * @param string $param_name Name of the parameter |
131 | * @param mixed $value Values to set the parameter |
132 | */ |
133 | public function set($param_name, $value) |
134 | { |
135 | $this->installer_config = array_merge($this->installer_config, array( |
136 | $param_name => $value, |
137 | )); |
138 | } |
139 | |
140 | /** |
141 | * Returns system parameter |
142 | * |
143 | * @param string $param_name Name of the parameter |
144 | * |
145 | * @return mixed Returns system parameter if it is defined, false otherwise |
146 | */ |
147 | public function system_get($param_name) |
148 | { |
149 | return (isset($this->system_data[$param_name])) ? $this->system_data[$param_name] : false; |
150 | } |
151 | |
152 | /** |
153 | * Returns remaining time until the run time limit |
154 | * |
155 | * @return int Remaining time until the run time limit in seconds |
156 | */ |
157 | public function get_time_remaining() |
158 | { |
159 | if ($this->system_data['max_execution_time'] <= 0) |
160 | { |
161 | return PHP_INT_MAX; |
162 | } |
163 | |
164 | return ($this->system_data['start_time'] + $this->system_data['max_execution_time']) - microtime(true); |
165 | } |
166 | |
167 | /** |
168 | * Returns remaining memory available for PHP |
169 | * |
170 | * @return int Remaining memory until reaching the limit |
171 | */ |
172 | public function get_memory_remaining() |
173 | { |
174 | if ($this->system_data['memory_limit'] <= 0) |
175 | { |
176 | return 1; |
177 | } |
178 | |
179 | if (function_exists('memory_get_usage')) |
180 | { |
181 | return ($this->system_data['memory_limit'] - memory_get_usage()); |
182 | } |
183 | |
184 | // If we cannot get the information then just return a positive number (and cross fingers) |
185 | return 1; |
186 | } |
187 | |
188 | /** |
189 | * Saves the latest executed task |
190 | * |
191 | * @param int $task_service_index Index of the installer task service in the module |
192 | */ |
193 | public function set_finished_task($task_service_index) |
194 | { |
195 | $this->progress_data['last_task_index'] = $task_service_index; |
196 | } |
197 | |
198 | /** |
199 | * Set active module |
200 | * |
201 | * @param string $module_service_name Name of the installer module service |
202 | * @param int $module_service_index Index of the installer module service |
203 | */ |
204 | public function set_active_module($module_service_name, $module_service_index) |
205 | { |
206 | $this->progress_data['last_task_module_name'] = $module_service_name; |
207 | $this->progress_data['last_task_module_index'] = $module_service_index; |
208 | } |
209 | |
210 | /** |
211 | * Getter for progress data |
212 | * |
213 | * @return array |
214 | */ |
215 | public function get_progress_data() |
216 | { |
217 | return $this->progress_data; |
218 | } |
219 | |
220 | /** |
221 | * Recovers install configuration from file |
222 | */ |
223 | public function load_config() |
224 | { |
225 | if (!$this->filesystem->exists($this->install_config_file)) |
226 | { |
227 | return; |
228 | } |
229 | |
230 | $file_content = @file_get_contents($this->install_config_file); |
231 | $serialized_data = trim(substr($file_content, 8)); |
232 | |
233 | $installer_config = array(); |
234 | $progress_data = array(); |
235 | $navigation_data = array(); |
236 | |
237 | if (!empty($serialized_data)) |
238 | { |
239 | $unserialized_data = json_decode($serialized_data, true); |
240 | |
241 | $installer_config = (is_array($unserialized_data['installer_config'])) ? $unserialized_data['installer_config'] : array(); |
242 | $progress_data = (is_array($unserialized_data['progress_data'])) ? $unserialized_data['progress_data'] : array(); |
243 | $navigation_data = (is_array($unserialized_data['navigation_data'])) ? $unserialized_data['navigation_data'] : array(); |
244 | } |
245 | |
246 | $this->installer_config = array_merge($this->installer_config, $installer_config); |
247 | $this->progress_data = array_merge($this->progress_data, $progress_data); |
248 | $this->navigation_data = array_merge($this->navigation_data, $navigation_data); |
249 | } |
250 | |
251 | /** |
252 | * Creates a progress restart point |
253 | * |
254 | * Restart points can be used to repeat certain tasks periodically. |
255 | * You need to call this method from the first task you want to repeat. |
256 | * |
257 | * @param string $name Name of the restart point |
258 | */ |
259 | public function create_progress_restart_point($name) |
260 | { |
261 | $tmp_progress_data = $this->progress_data; |
262 | unset($tmp_progress_data['_restart_points']); |
263 | |
264 | $this->progress_data['_restart_points'][$name] = $tmp_progress_data; |
265 | } |
266 | |
267 | /** |
268 | * Set restart point to continue from |
269 | * |
270 | * @param string $name Name of the restart point |
271 | * |
272 | * @return bool Returns false if the restart point name does not exist, otherwise true |
273 | */ |
274 | public function jump_to_restart_point($name) |
275 | { |
276 | if (!isset($this->progress_data['_restart_points'][$name]) || empty($this->progress_data['_restart_points'][$name])) |
277 | { |
278 | return false; |
279 | } |
280 | |
281 | foreach ($this->progress_data['_restart_points'][$name] as $key => $value) |
282 | { |
283 | $this->progress_data[$key] = $value; |
284 | } |
285 | |
286 | return true; |
287 | } |
288 | |
289 | /** |
290 | * Returns whether a restart point with a given name exists or not |
291 | * |
292 | * @param string $name Name of the restart point |
293 | * |
294 | * @return bool |
295 | */ |
296 | public function has_restart_point($name) |
297 | { |
298 | return isset($this->progress_data['_restart_points'][$name]); |
299 | } |
300 | |
301 | /** |
302 | * Dumps install configuration to disk |
303 | */ |
304 | public function save_config() |
305 | { |
306 | if ($this->do_clean_up) |
307 | { |
308 | @unlink($this->install_config_file); |
309 | return; |
310 | } |
311 | |
312 | // Create array to save |
313 | $save_array = array( |
314 | 'installer_config' => $this->installer_config, |
315 | 'progress_data' => $this->progress_data, |
316 | 'navigation_data' => $this->navigation_data, |
317 | ); |
318 | |
319 | // Create file content |
320 | $file_content = '<?php // '; |
321 | $file_content .= json_encode($save_array); |
322 | $file_content .= "\n"; |
323 | |
324 | // Dump file_content to disk |
325 | $fp = @fopen($this->install_config_file, 'w'); |
326 | if (!$fp) |
327 | { |
328 | throw new installer_config_not_writable_exception(); |
329 | } |
330 | |
331 | fwrite($fp, $file_content); |
332 | fclose($fp); |
333 | // Enforce 0600 permission for install config |
334 | $this->filesystem->chmod([$this->install_config_file], 0600); |
335 | } |
336 | |
337 | /** |
338 | * Increments the task progress |
339 | * |
340 | * @param int $increment_by The amount to increment by |
341 | */ |
342 | public function increment_current_task_progress($increment_by = 1) |
343 | { |
344 | $this->progress_data['current_task_progress'] += $increment_by; |
345 | |
346 | if ($this->progress_data['current_task_progress'] > $this->progress_data['max_task_progress']) |
347 | { |
348 | $this->progress_data['current_task_progress'] = $this->progress_data['max_task_progress']; |
349 | } |
350 | } |
351 | |
352 | /** |
353 | * Sets the task progress to a specific number |
354 | * |
355 | * @param int $task_progress The task progress number to be set |
356 | */ |
357 | public function set_current_task_progress($task_progress) |
358 | { |
359 | $this->progress_data['current_task_progress'] = $task_progress; |
360 | } |
361 | |
362 | /** |
363 | * Sets the number of tasks belonging to the installer in the current mode. |
364 | * |
365 | * @param int $task_progress_count Number of tasks |
366 | */ |
367 | public function set_task_progress_count($task_progress_count) |
368 | { |
369 | $this->progress_data['max_task_progress'] = $task_progress_count; |
370 | } |
371 | |
372 | /** |
373 | * Returns the number of the current task being executed |
374 | * |
375 | * @return int |
376 | */ |
377 | public function get_current_task_progress() |
378 | { |
379 | return $this->progress_data['current_task_progress']; |
380 | } |
381 | |
382 | /** |
383 | * Returns the number of tasks belonging to the installer in the current mode. |
384 | * |
385 | * @return int |
386 | */ |
387 | public function get_task_progress_count() |
388 | { |
389 | return $this->progress_data['max_task_progress']; |
390 | } |
391 | |
392 | /** |
393 | * Marks stage as completed in the navigation bar |
394 | * |
395 | * @param array $nav_path Array to the navigation elem |
396 | */ |
397 | public function set_finished_navigation_stage($nav_path) |
398 | { |
399 | if (isset($this->navigation_data['finished']) && in_array($nav_path, $this->navigation_data['finished'])) |
400 | { |
401 | return; |
402 | } |
403 | |
404 | $this->navigation_data['finished'][] = $nav_path; |
405 | } |
406 | |
407 | /** |
408 | * Marks stage as active in the navigation bar |
409 | * |
410 | * @param array $nav_path Array to the navigation elem |
411 | */ |
412 | public function set_active_navigation_stage($nav_path) |
413 | { |
414 | $this->navigation_data['active'] = $nav_path; |
415 | } |
416 | |
417 | /** |
418 | * Returns navigation data |
419 | * |
420 | * @return array |
421 | */ |
422 | public function get_navigation_data() |
423 | { |
424 | return $this->navigation_data; |
425 | } |
426 | |
427 | /** |
428 | * Removes install config file |
429 | */ |
430 | public function clean_up_config_file() |
431 | { |
432 | $this->do_clean_up = true; |
433 | @unlink($this->install_config_file); |
434 | } |
435 | |
436 | /** |
437 | * Filling up system_data array |
438 | */ |
439 | protected function setup_system_data() |
440 | { |
441 | // Query maximum runtime from php.ini |
442 | $execution_time = $this->php_ini->getNumeric('max_execution_time'); |
443 | $execution_time = min(15, $execution_time / 2); |
444 | $this->system_data['max_execution_time'] = $execution_time; |
445 | |
446 | // Set start time |
447 | $this->system_data['start_time'] = microtime(true); |
448 | |
449 | // Get memory limit |
450 | $this->system_data['memory_limit'] = $this->php_ini->getBytes('memory_limit'); |
451 | } |
452 | } |