[ticket/16565] Lazy-load cron.task_collection

PHPBB3-16565
This commit is contained in:
kasimi 2020-08-11 07:59:10 +02:00
parent d7ccd22383
commit fe97d19c66
No known key found for this signature in database
GPG key ID: 3163AB573241193A

View file

@ -15,6 +15,7 @@ namespace phpbb\cron;
use phpbb\cron\task\wrapper; use phpbb\cron\task\wrapper;
use phpbb\routing\helper; use phpbb\routing\helper;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Cron manager class. * Cron manager class.
@ -23,6 +24,11 @@ use phpbb\routing\helper;
*/ */
class manager class manager
{ {
/**
* @var ContainerInterface
*/
protected $phpbb_container;
/** /**
* @var helper * @var helper
*/ */
@ -34,7 +40,14 @@ class manager
* *
* @var array * @var array
*/ */
protected $tasks = array(); protected $tasks = [];
/**
* Flag indicating if $this->tasks contains tasks registered in the container
*
* @var bool
*/
protected $is_initialised_from_container = false;
/** /**
* @var string * @var string
@ -49,18 +62,17 @@ class manager
/** /**
* Constructor. Loads all available tasks. * Constructor. Loads all available tasks.
* *
* @param array|\Traversable $tasks Provides an iterable set of task names * @param ContainerInterface $phpbb_container Container
* @param helper $routing_helper Routing helper * @param helper $routing_helper Routing helper
* @param string $phpbb_root_path Relative path to phpBB root * @param string $phpbb_root_path Relative path to phpBB root
* @param string $php_ext PHP file extension * @param string $php_ext PHP file extension
*/ */
public function __construct($tasks, helper $routing_helper, $phpbb_root_path, $php_ext) public function __construct(ContainerInterface $phpbb_container, helper $routing_helper, $phpbb_root_path, $php_ext)
{ {
$this->phpbb_container = $phpbb_container;
$this->routing_helper = $routing_helper; $this->routing_helper = $routing_helper;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext; $this->php_ext = $php_ext;
$this->load_tasks($tasks);
} }
/** /**
@ -79,6 +91,24 @@ class manager
} }
} }
/**
* Loads registered tasks from the container, wraps them
* and puts them into $this->tasks.
*
* @return null
*/
public function load_tasks_from_container()
{
if (!$this->is_initialised_from_container)
{
$this->is_initialised_from_container = true;
$tasks = $this->phpbb_container->get('cron.task_collection');
$this->load_tasks($tasks);
}
}
/** /**
* Finds a task that is ready to run. * Finds a task that is ready to run.
* *
@ -90,6 +120,8 @@ class manager
*/ */
public function find_one_ready_task() public function find_one_ready_task()
{ {
$this->load_tasks_from_container();
shuffle($this->tasks); shuffle($this->tasks);
foreach ($this->tasks as $task) foreach ($this->tasks as $task)
{ {
@ -108,7 +140,9 @@ class manager
*/ */
public function find_all_ready_tasks() public function find_all_ready_tasks()
{ {
$tasks = array(); $this->load_tasks_from_container();
$tasks = [];
foreach ($this->tasks as $task) foreach ($this->tasks as $task)
{ {
if ($task->is_ready()) if ($task->is_ready())
@ -131,6 +165,8 @@ class manager
*/ */
public function find_task($name) public function find_task($name)
{ {
$this->load_tasks_from_container();
foreach ($this->tasks as $task) foreach ($this->tasks as $task)
{ {
if ($task->get_name() == $name) if ($task->get_name() == $name)
@ -148,6 +184,8 @@ class manager
*/ */
public function get_tasks() public function get_tasks()
{ {
$this->load_tasks_from_container();
return $this->tasks; return $this->tasks;
} }