2010-04-17 03:13:30 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
* @copyright (c) 2010 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cron manager class.
|
|
|
|
*
|
|
|
|
* Finds installed cron tasks, stores task objects, provides task selection.
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
*/
|
2010-10-28 22:26:49 +02:00
|
|
|
class phpbb_cron_manager
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Set of phpbb_cron_task_wrapper objects.
|
|
|
|
* Array holding all tasks that have been found.
|
|
|
|
*
|
2011-01-12 23:02:00 +01:00
|
|
|
* @var array
|
2010-12-17 01:15:57 +01:00
|
|
|
*/
|
2011-01-12 23:02:00 +01:00
|
|
|
protected $tasks = array();
|
|
|
|
|
|
|
|
/**
|
2011-07-13 08:22:27 -04:00
|
|
|
* An extension manager to search for cron tasks in extensions.
|
|
|
|
* @var phpbb_extension_manager
|
2011-01-12 23:02:00 +01:00
|
|
|
*/
|
2011-07-13 08:22:27 -04:00
|
|
|
protected $extension_manager;
|
2011-01-13 00:51:32 +01:00
|
|
|
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Constructor. Loads all available tasks.
|
2011-01-13 00:51:32 +01:00
|
|
|
*
|
2011-07-13 08:22:27 -04:00
|
|
|
* Tasks will be looked up in the core task directory located in
|
|
|
|
* includes/cron/task/core/ and in extensions. Task classes will be
|
|
|
|
* autoloaded and must be named according to autoloading naming conventions.
|
2011-01-18 17:03:45 -05:00
|
|
|
*
|
2011-07-13 08:22:27 -04:00
|
|
|
* Tasks in extensions must be located in a directory called cron or a subdir
|
|
|
|
* of a directory called cron. The class and filename must end in a _task
|
|
|
|
* suffix.
|
2011-01-18 17:03:45 -05:00
|
|
|
*
|
2011-07-13 08:22:27 -04:00
|
|
|
* @param phpbb_extension_manager $extension_manager phpBB extension manager
|
2010-12-17 01:15:57 +01:00
|
|
|
*/
|
2011-07-13 08:22:27 -04:00
|
|
|
public function __construct(phpbb_extension_manager $extension_manager)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2011-07-13 08:22:27 -04:00
|
|
|
$this->extension_manager = $extension_manager;
|
2011-01-12 23:02:00 +01:00
|
|
|
|
2010-10-28 22:15:30 +02:00
|
|
|
$task_names = $this->find_cron_task_names();
|
|
|
|
$this->load_tasks($task_names);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-13 08:22:27 -04:00
|
|
|
* Finds cron task names using the extension manager.
|
2010-04-17 03:13:30 -04:00
|
|
|
*
|
2011-07-13 08:22:27 -04:00
|
|
|
* All PHP files in includes/cron/task/core/ are considered tasks. Tasks
|
|
|
|
* in extensions have to be located in a directory called cron or a subdir
|
|
|
|
* of a directory called cron. The class and filename must end in a _task
|
|
|
|
* suffix.
|
2010-04-17 03:13:30 -04:00
|
|
|
*
|
2010-12-19 23:56:43 +01:00
|
|
|
* @return array List of task names
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-10-28 22:15:30 +02:00
|
|
|
public function find_cron_task_names()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2011-07-13 08:22:27 -04:00
|
|
|
$finder = $this->extension_manager->get_finder();
|
|
|
|
|
|
|
|
return $finder
|
|
|
|
->suffix('_task')
|
|
|
|
->directory('/cron')
|
|
|
|
->default_path('includes/cron/task/core/')
|
|
|
|
->default_suffix('')
|
|
|
|
->default_directory('')
|
|
|
|
->get_classes();
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Loads tasks given by name, wraps them
|
|
|
|
* and puts them into $this->tasks.
|
|
|
|
*
|
|
|
|
* @param array $task_names Array of strings
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-01-12 23:02:00 +01:00
|
|
|
public function load_tasks(array $task_names)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-10-28 22:15:30 +02:00
|
|
|
foreach ($task_names as $task_name)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2011-01-13 00:51:32 +01:00
|
|
|
$task = new $task_name();
|
2010-12-14 13:26:42 +01:00
|
|
|
$wrapper = new phpbb_cron_task_wrapper($task);
|
|
|
|
$this->tasks[] = $wrapper;
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a task that is ready to run.
|
2010-12-19 23:56:43 +01:00
|
|
|
*
|
2010-04-17 03:13:30 -04:00
|
|
|
* If several tasks are ready, any one of them could be returned.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2010-12-19 23:56:43 +01:00
|
|
|
* If no tasks are ready, null is returned.
|
|
|
|
*
|
2010-12-17 01:15:57 +01:00
|
|
|
* @return phpbb_cron_task_wrapper|null
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_one_ready_task()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->is_ready())
|
|
|
|
{
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all tasks that are ready to run.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2010-12-19 23:56:43 +01:00
|
|
|
* @return array List of tasks which are ready to run (wrapped in phpbb_cron_task_wrapper).
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_all_ready_tasks()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
$tasks = array();
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->is_ready())
|
|
|
|
{
|
|
|
|
$tasks[] = $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a task by name.
|
2010-12-19 23:56:43 +01:00
|
|
|
*
|
|
|
|
* If there is no task with the specified name, null is returned.
|
|
|
|
*
|
2010-04-17 03:13:30 -04:00
|
|
|
* Web runner uses this method to resolve names to tasks.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
2010-12-19 23:56:43 +01:00
|
|
|
* @param string $name Name of the task to look up.
|
|
|
|
* @return phpbb_cron_task A task corresponding to the given name, or null.
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_task($name)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->get_name() == $name)
|
|
|
|
{
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-05-09 16:47:52 -04:00
|
|
|
/**
|
|
|
|
* Creates an instance of parametrized cron task $name with args $args.
|
|
|
|
* The constructed task is wrapped with cron task wrapper before being returned.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
|
|
|
* @param string $name The task name, which is the same as cron task class name.
|
|
|
|
* @param array $args Will be passed to the task class's constructor.
|
|
|
|
*
|
|
|
|
* @return phpbb_cron_task_wrapper|null
|
2010-05-09 16:47:52 -04:00
|
|
|
*/
|
2011-01-12 23:02:00 +01:00
|
|
|
public function instantiate_task($name, array $args)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
$task = $this->find_task($name);
|
|
|
|
if ($task)
|
|
|
|
{
|
2010-05-09 16:47:52 -04:00
|
|
|
// task here is actually an instance of cron task wrapper
|
|
|
|
$class = $task->get_name();
|
2010-04-17 03:13:30 -04:00
|
|
|
$task = new $class($args);
|
2010-05-09 16:47:52 -04:00
|
|
|
// need to wrap the new task too
|
2010-10-28 22:26:49 +02:00
|
|
|
$task = new phpbb_cron_task_wrapper($task);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|