1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-10 09:35:24 +02:00
php-phpbb/phpBB/phpbb/install/sequential_task.php
2021-01-31 12:57:04 +01:00

70 lines
1.6 KiB
PHP

<?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 $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();
}
}
}