Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
sequential_task | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
execute_step | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
execute | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 |
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; |
15 | |
16 | use phpbb\install\exception\resource_limit_reached_exception; |
17 | use phpbb\install\helper\config; |
18 | |
19 | /** |
20 | * Trait to execute tasks in steps with timeout management. |
21 | */ |
22 | trait sequential_task |
23 | { |
24 | /** |
25 | * Callback function to execute a unit of work. |
26 | * |
27 | * @param mixed $key The array key. |
28 | * @param mixed $value The array value. |
29 | */ |
30 | abstract protected function execute_step($key, $value) : void; |
31 | |
32 | /** |
33 | * Execute the tasks with timeout management. |
34 | * |
35 | * @param config $config Installer config. |
36 | * @param array $data Array of elements to iterate over. |
37 | * @param string|null $counter_name The name of the counter or null. |
38 | * |
39 | * @throws resource_limit_reached_exception When resources are exhausted. |
40 | */ |
41 | protected function execute(config $config, array $data, ?string $counter_name = null) : void |
42 | { |
43 | if ($counter_name === null) |
44 | { |
45 | $counter_name = 'step_counter_' . get_class($this); |
46 | } |
47 | |
48 | $counter = $config->get($counter_name, 0); |
49 | $total = count($data); |
50 | $data = array_slice($data, $counter); |
51 | foreach ($data as $key => $value) |
52 | { |
53 | if ($config->get_time_remaining() <= 0 || $config->get_memory_remaining() <= 0) |
54 | { |
55 | break; |
56 | } |
57 | |
58 | $this->execute_step($key, $value); |
59 | ++$counter; |
60 | } |
61 | |
62 | $config->set($counter_name, $counter); |
63 | |
64 | if ($counter < $total) |
65 | { |
66 | throw new resource_limit_reached_exception(); |
67 | } |
68 | } |
69 | } |