[feature/dic] Give all cron tasks a name, change some manager usage

PHPBB3-10739
This commit is contained in:
Igor Wiedler 2012-04-09 14:34:35 +02:00
parent aa0c995ed9
commit 3896ee953f
8 changed files with 61 additions and 23 deletions

View file

@ -116,7 +116,8 @@ services:
class: phpbb_cron_manager class: phpbb_cron_manager
arguments: arguments:
- @cron.task_provider - @cron.task_provider
- @cache.driver - %core.root_path%
- %core.php_ext%
cron.lock_db: cron.lock_db:
class: phpbb_lock_db class: phpbb_lock_db

View file

@ -61,7 +61,7 @@ function do_cron($cron_lock, $run_tasks)
if ($config['use_system_cron']) if ($config['use_system_cron'])
{ {
$cron = new phpbb_cron_manager(new phpbb_cron_task_provider($phpbb_extension_manager), $cache->get_driver()); $cron = $container->get('cron.manager');
} }
else else
{ {

View file

@ -32,13 +32,18 @@ class phpbb_cron_manager
*/ */
protected $tasks = array(); protected $tasks = array();
protected $phpbb_root_path, $phpEx;
/** /**
* Constructor. Loads all available tasks. * Constructor. Loads all available tasks.
* *
* @param array|Traversable $tasks Provides an iterable set of task names * @param array|Traversable $tasks Provides an iterable set of task names
*/ */
public function __construct($tasks) public function __construct($tasks, $phpbb_root_path, $phpEx)
{ {
$this->phpbb_root_path = $phpbb_root_path;
$this->phpEx = $phpEx;
$this->load_tasks($tasks); $this->load_tasks($tasks);
} }
@ -54,8 +59,7 @@ class phpbb_cron_manager
{ {
foreach ($tasks as $task) foreach ($tasks as $task)
{ {
$wrapper = new phpbb_cron_task_wrapper($task); $this->tasks[] = $this->wrap_task($task);
$this->tasks[] = $wrapper;
} }
} }
@ -119,4 +123,9 @@ class phpbb_cron_manager
} }
return null; return null;
} }
public function wrap_task(phpbb_cron_task $task)
{
return new phpbb_cron_task_wrapper($task, $this->phpbb_root_path, $this->phpEx);
}
} }

View file

@ -28,6 +28,28 @@ if (!defined('IN_PHPBB'))
*/ */
abstract class phpbb_cron_task_base implements phpbb_cron_task abstract class phpbb_cron_task_base implements phpbb_cron_task
{ {
private $name;
/**
* Returns the name of the task.
*
* @return string Name of wrapped task.
*/
public function get_name()
{
return $this->name;
}
/**
* Sets the name of the task.
*
* @param string $name The task name
*/
public function set_name($name)
{
$this->name = $name;
}
/** /**
* Returns whether this cron task can run, given current board configuration. * Returns whether this cron task can run, given current board configuration.
* *

View file

@ -45,7 +45,12 @@ class phpbb_cron_task_provider implements IteratorAggregate
$tasks = array(); $tasks = array();
foreach ($definitions as $name => $definition) foreach ($definitions as $name => $definition)
{ {
$tasks[] = $this->container->get($name); $task = $this->container->get($name);
if ($task instanceof phpbb_cron_task_base) {
$task->set_name($name);
}
$tasks[] = $task;
} }
return new ArrayIterator($tasks); return new ArrayIterator($tasks);

View file

@ -21,6 +21,13 @@ if (!defined('IN_PHPBB'))
*/ */
interface phpbb_cron_task interface phpbb_cron_task
{ {
/**
* Returns the name of the task.
*
* @return string Name of wrapped task.
*/
public function get_name();
/** /**
* Runs this cron task. * Runs this cron task.
* *

View file

@ -23,6 +23,8 @@ if (!defined('IN_PHPBB'))
*/ */
class phpbb_cron_task_wrapper class phpbb_cron_task_wrapper
{ {
private $task, $phpbb_root_path, $phpEx;
/** /**
* Constructor. * Constructor.
* *
@ -30,9 +32,11 @@ class phpbb_cron_task_wrapper
* *
* @param phpbb_cron_task $task The cron task to wrap. * @param phpbb_cron_task $task The cron task to wrap.
*/ */
public function __construct(phpbb_cron_task $task) public function __construct(phpbb_cron_task $task, $phpbb_root_path, $phpEx)
{ {
$this->task = $task; $this->task = $task;
$this->phpbb_root_path = $phpbb_root_path;
$this->phpEx = $phpEx;
} }
/** /**
@ -61,16 +65,6 @@ class phpbb_cron_task_wrapper
return $this->task->is_runnable() && $this->task->should_run(); return $this->task->is_runnable() && $this->task->should_run();
} }
/**
* Returns the name of wrapped task. It is the same as the wrapped class's class name.
*
* @return string Class name of wrapped task.
*/
public function get_name()
{
return get_class($this->task);
}
/** /**
* Returns a url through which this task may be invoked via web. * Returns a url through which this task may be invoked via web.
* *
@ -82,8 +76,6 @@ class phpbb_cron_task_wrapper
*/ */
public function get_url() public function get_url()
{ {
global $phpbb_root_path, $phpEx;
$name = $this->get_name(); $name = $this->get_name();
if ($this->is_parametrized()) if ($this->is_parametrized())
{ {
@ -98,7 +90,7 @@ class phpbb_cron_task_wrapper
{ {
$extra = ''; $extra = '';
} }
$url = append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $name . $extra); $url = append_sid($this->phpbb_root_path . 'cron.' . $this->phpEx, 'cron_type=' . $name . $extra);
return $url; return $url;
} }

View file

@ -193,10 +193,12 @@ if ($forum_data['forum_topics_per_page'])
// Do the forum Prune thang - cron type job ... // Do the forum Prune thang - cron type job ...
if (!$config['use_system_cron']) if (!$config['use_system_cron'])
{ {
$task = $container->get('cron.task.core.prune_forum'); $cron = $container->get('cron.manager');
$task = new phpbb_cron_task_wrapper($task);
$task = $cron->find_task('cron.task.core.prune_forum');
$task->set_forum_data($forum_data); $task->set_forum_data($forum_data);
if ($task && $task->is_ready())
if ($task->is_ready())
{ {
$url = $task->get_url(); $url = $task->get_url();
$template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />'); $template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');