-
-
Notifications
You must be signed in to change notification settings - Fork 993
Expand file tree
/
Copy pathsequential_task.php
More file actions
69 lines (61 loc) · 1.63 KB
/
sequential_task.php
File metadata and controls
69 lines (61 loc) · 1.63 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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\install;
use phpbb\install\exception\resource_limit_reached_exception;
use phpbb\install\helper\config;
/**
* Trait to execute tasks in steps with timeout management.
*/
trait sequential_task
{
/**
* Callback function to execute a unit of work.
*
* @param mixed $key The array key.
* @param mixed $value The array value.
*/
abstract protected function execute_step($key, $value) : void;
/**
* Execute the tasks with timeout management.
*
* @param config $config Installer config.
* @param array $data Array of elements to iterate over.
* @param string|null $counter_name The name of the counter or null.
*
* @throws resource_limit_reached_exception When resources are exhausted.
*/
protected function execute(config $config, array $data, string|null $counter_name = null) : void
{
if ($counter_name === null)
{
$counter_name = 'step_counter_' . get_class($this);
}
$counter = $config->get($counter_name, 0);
$total = count($data);
$data = array_slice($data, $counter);
foreach ($data as $key => $value)
{
if ($config->get_time_remaining() <= 0 || $config->get_memory_remaining() <= 0)
{
break;
}
$this->execute_step($key, $value);
++$counter;
}
$config->set($counter_name, $counter);
if ($counter < $total)
{
throw new resource_limit_reached_exception();
}
}
}