[feature/system-cron] rewrite cron manager to use autoloading

PHPBB3-9596
This commit is contained in:
Igor Wiedler 2010-10-28 22:15:30 +02:00 committed by Oleg Pudeyev
parent 5a075c3dca
commit 0aa491ffa2

View file

@ -29,15 +29,15 @@ class cron_manager
public function __construct() public function __construct()
{ {
$task_files = $this->find_cron_task_files(); $task_names = $this->find_cron_task_names();
$this->load_tasks($task_files); $this->load_tasks($task_names);
} }
/** /**
* Finds cron task files. * Finds cron task files.
* *
* A cron task file must follow the naming convention: * A cron task file must follow the naming convention:
* includes/cron/tasks/$mod/$name.php. * includes/cron/task/$mod/$name.php.
* $mod is core for tasks that are part of phpbb. * $mod is core for tasks that are part of phpbb.
* Modifications should use their name as $mod. * Modifications should use their name as $mod.
* $name is the name of the cron task. * $name is the name of the cron task.
@ -45,43 +45,41 @@ class cron_manager
* *
* Todo: consider caching found task file list in global cache. * Todo: consider caching found task file list in global cache.
*/ */
public function find_cron_task_files() public function find_cron_task_names()
{ {
global $phpbb_root_path, $phpEx; global $phpbb_root_path, $phpEx;
$tasks_root_path = $phpbb_root_path . 'includes/cron/task'; $tasks_root_path = $phpbb_root_path . 'includes/cron/task/';
$dir = opendir($tasks_root_path);
$task_dirs = array(); $task_names = array();
while (($entry = readdir($dir)) !== false) $ext = '.' . $phpEx;
$ext_length = strlen($ext);
$dh = opendir($tasks_root_path);
while (($mod = readdir($dh)) !== false)
{ {
// ignore ., .. and dot directories // ignore ., .. and dot directories
// todo: change is_dir to account for symlinks // todo: change is_dir to account for symlinks
if ($entry[0] == '.' || !is_dir("$tasks_root_path/$entry")) if ($mod[0] == '.' || !is_dir($tasks_root_path . $mod))
{ {
continue; continue;
} }
$task_dirs[] = $entry;
}
closedir($dir);
$ext = '.' . $phpEx; $dh2 = opendir($tasks_root_path . $mod);
$ext_length = strlen($ext); while (($file = readdir($dh2)) !== false)
$task_files = array();
foreach ($task_dirs as $task_dir)
{
$path = $phpbb_root_path . 'includes/cron/tasks/' . $task_dir;
$dir = opendir($path);
while (($entry = readdir($dir)) !== false)
{ {
if (substr($entry, -$ext_length) == $ext) $task_name = substr($file, 0, -$ext_length);
if (substr($file, -$ext_length) == $ext && $this->is_valid_name($mod) && $this->is_valid_name($task_name))
{ {
$task_file = substr($entry, 0, -$ext_length); $full_task_name = $mod . '_' . $task_name;
$task_files[] = array($task_dir, $task_file); $task_names[] = $full_task_name;
} }
} }
closedir($dir); closedir($dh2);
} }
return $task_files; closedir($dh);
return $task_names;
} }
/** /**
@ -92,24 +90,14 @@ class cron_manager
return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name);
} }
public function load_tasks($task_files) public function load_tasks($task_names)
{ {
global $phpbb_root_path, $phpEx; foreach ($task_names as $task_name)
foreach ($task_files as $task_file)
{ {
list($mod, $filename) = $task_file; $class = "cron_task_$task_name";
if ($this->is_valid_name($mod) && $this->is_valid_name($filename)) $task = new $class();
{ $wrapper = new cron_task_wrapper($task);
$class = "cron_task_${mod}_${filename}";
if (!class_exists($class))
{
include($phpbb_root_path . "includes/cron/tasks/$mod/$filename.$phpEx");
}
$object = new $class;
$wrapper = new cron_task_wrapper($object);
$this->tasks[] = $wrapper; $this->tasks[] = $wrapper;
}
} }
} }