mirror of
https://github.com/phpbb/phpbb.git
synced 2025-08-05 16:27:38 +02:00
[ticket/16643] Refactor installer tasks to use Doctrine DBAL
PHPBB3-16643
This commit is contained in:
69
phpBB/phpbb/install/sequential_task.php
Normal file
69
phpBB/phpbb/install/sequential_task.php
Normal file
@@ -0,0 +1,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 $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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user