mirror of
https://github.com/phpbb/phpbb.git
synced 2025-05-28 18:23:00 +02:00
[feature/extension-manager] Remove cron's dependency on the extension manager.
Instead a separate cron provider supplies the manager with tasks from the extension finder. PHPBB3-10323
This commit is contained in:
parent
96209e0224
commit
5d5030a48b
@ -144,5 +144,5 @@ foreach ($cache->obtain_hooks() as $hook)
|
||||
|
||||
if (!$config['use_system_cron'])
|
||||
{
|
||||
$cron = new phpbb_cron_manager($phpbb_extension_manager, $cache->get_driver());
|
||||
$cron = new phpbb_cron_manager(new phpbb_cron_provider($phpbb_extension_manager), $cache->get_driver());
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ function do_cron($cron_lock, $run_tasks)
|
||||
|
||||
if ($config['use_system_cron'])
|
||||
{
|
||||
$cron = new phpbb_cron_manager($phpbb_root_path . 'includes/cron/task', $phpEx, $cache->get_driver());
|
||||
$cron = new phpbb_cron_manager(new phpbb_cron_provider($phpbb_extension_manager), $cache->get_driver());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -32,65 +32,25 @@ class phpbb_cron_manager
|
||||
*/
|
||||
protected $tasks = array();
|
||||
|
||||
/**
|
||||
* An extension manager to search for cron tasks in extensions.
|
||||
* @var phpbb_extension_manager
|
||||
*/
|
||||
protected $extension_manager;
|
||||
|
||||
/**
|
||||
* Constructor. Loads all available tasks.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param phpbb_extension_manager $extension_manager phpBB extension manager
|
||||
* @param array|Traversable $task_names Provides an iterable set of task names
|
||||
*/
|
||||
public function __construct(phpbb_extension_manager $extension_manager)
|
||||
public function __construct($task_names)
|
||||
{
|
||||
$this->extension_manager = $extension_manager;
|
||||
|
||||
$task_names = $this->find_cron_task_names();
|
||||
$this->load_tasks($task_names);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds cron task names using the extension manager.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @return array List of task names
|
||||
*/
|
||||
public function find_cron_task_names()
|
||||
{
|
||||
$finder = $this->extension_manager->get_finder();
|
||||
|
||||
return $finder
|
||||
->suffix('_task')
|
||||
->directory('/cron')
|
||||
->default_path('includes/cron/task/core/')
|
||||
->default_suffix('')
|
||||
->default_directory('')
|
||||
->get_classes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads tasks given by name, wraps them
|
||||
* and puts them into $this->tasks.
|
||||
*
|
||||
* @param array $task_names Array of strings
|
||||
* @param array|Traversable $task_names Array of strings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_tasks(array $task_names)
|
||||
public function load_tasks($task_names)
|
||||
{
|
||||
foreach ($task_names as $task_name)
|
||||
{
|
||||
|
92
phpBB/includes/cron/provider.php
Normal file
92
phpBB/includes/cron/provider.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package phpBB3
|
||||
* @copyright (c) 2011 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
if (!defined('IN_PHPBB'))
|
||||
{
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides cron manager with tasks
|
||||
*
|
||||
* Finds installed cron tasks and makes them available to the cron manager.
|
||||
*
|
||||
* @package phpBB3
|
||||
*/
|
||||
class phpbb_cron_provider implements \IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* Array holding all found task class names.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $task_names = array();
|
||||
|
||||
/**
|
||||
* An extension manager to search for cron tasks in extensions.
|
||||
* @var phpbb_extension_manager
|
||||
*/
|
||||
protected $extension_manager;
|
||||
|
||||
/**
|
||||
* Constructor. Loads all available tasks.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param phpbb_extension_manager $extension_manager phpBB extension manager
|
||||
*/
|
||||
public function __construct(phpbb_extension_manager $extension_manager)
|
||||
{
|
||||
$this->extension_manager = $extension_manager;
|
||||
|
||||
$this->task_names = $this->find_cron_task_names();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds cron task names using the extension manager.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @return array List of task names
|
||||
*/
|
||||
public function find_cron_task_names()
|
||||
{
|
||||
$finder = $this->extension_manager->get_finder();
|
||||
|
||||
return $finder
|
||||
->suffix('_task')
|
||||
->directory('/cron')
|
||||
->default_path('includes/cron/task/core/')
|
||||
->default_suffix('')
|
||||
->default_directory('')
|
||||
->get_classes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an iterator over all task names
|
||||
*
|
||||
* @return ArrayIterator An iterator for the array of task names
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new ArrayIterator($this->task_names);
|
||||
}
|
||||
}
|
@ -11,33 +11,22 @@ require_once dirname(__FILE__) . '/../mock/extension_manager.php';
|
||||
require_once dirname(__FILE__) . '/includes/cron/task/core/dummy_task.php';
|
||||
require_once dirname(__FILE__) . '/includes/cron/task/core/second_dummy_task.php';
|
||||
require_once dirname(__FILE__) . '/ext/testext/cron/dummy_task.php';
|
||||
require_once dirname(__FILE__) . '/root2/includes/cron/task/core/simple_ready.php';
|
||||
require_once dirname(__FILE__) . '/root2/includes/cron/task/core/simple_not_runnable.php';
|
||||
require_once dirname(__FILE__) . '/root2/includes/cron/task/core/simple_should_not_run.php';
|
||||
require_once dirname(__FILE__) . '/tasks/simple_ready.php';
|
||||
require_once dirname(__FILE__) . '/tasks/simple_not_runnable.php';
|
||||
require_once dirname(__FILE__) . '/tasks/simple_should_not_run.php';
|
||||
|
||||
class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->extension_manager = new phpbb_mock_extension_manager(
|
||||
dirname(__FILE__) . '/',
|
||||
array(
|
||||
'testext' => array(
|
||||
'ext_name' => 'testext',
|
||||
'ext_active' => true,
|
||||
'ext_path' => dirname(__FILE__) . '/ext/testext/'
|
||||
),
|
||||
));
|
||||
$this->manager = new phpbb_cron_manager($this->extension_manager);
|
||||
$this->manager = new phpbb_cron_manager(array(
|
||||
'phpbb_cron_task_core_dummy_task',
|
||||
'phpbb_cron_task_core_second_dummy_task',
|
||||
'phpbb_ext_testext_cron_dummy_task',
|
||||
));
|
||||
$this->task_name = 'phpbb_cron_task_core_dummy_task';
|
||||
}
|
||||
|
||||
public function test_manager_finds_shipped_tasks()
|
||||
{
|
||||
$tasks = $this->manager->find_cron_task_names();
|
||||
$this->assertEquals(3, sizeof($tasks));
|
||||
}
|
||||
|
||||
public function test_manager_finds_shipped_task_by_name()
|
||||
{
|
||||
$task = $this->manager->find_task($this->task_name);
|
||||
@ -66,7 +55,11 @@ class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function test_manager_finds_only_ready_tasks()
|
||||
{
|
||||
$manager = new phpbb_cron_manager(new phpbb_mock_extension_manager(dirname(__FILE__) . '/root2/'));
|
||||
$manager = new phpbb_cron_manager(array(
|
||||
'phpbb_cron_task_core_simple_ready',
|
||||
'phpbb_cron_task_core_simple_not_runnable',
|
||||
'phpbb_cron_task_core_simple_should_not_run',
|
||||
));
|
||||
$tasks = $manager->find_all_ready_tasks();
|
||||
$task_names = $this->tasks_to_names($tasks);
|
||||
$this->assertEquals(array('phpbb_cron_task_core_simple_ready'), $task_names);
|
||||
|
45
tests/cron/provider_test.php
Normal file
45
tests/cron/provider_test.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2010 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../mock/extension_manager.php';
|
||||
|
||||
class phpbb_cron_provider_test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->extension_manager = new phpbb_mock_extension_manager(
|
||||
dirname(__FILE__) . '/',
|
||||
array(
|
||||
'testext' => array(
|
||||
'ext_name' => 'testext',
|
||||
'ext_active' => true,
|
||||
'ext_path' => dirname(__FILE__) . '/ext/testext/'
|
||||
),
|
||||
));
|
||||
$this->provider = new phpbb_cron_provider($this->extension_manager);
|
||||
}
|
||||
|
||||
public function test_manager_finds_shipped_tasks()
|
||||
{
|
||||
$task_iterator = $this->provider->find_cron_task_names();
|
||||
|
||||
$tasks = array();
|
||||
foreach ($task_iterator as $task)
|
||||
{
|
||||
$tasks[] = $task;
|
||||
}
|
||||
sort($tasks);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'phpbb_cron_task_core_dummy_task',
|
||||
'phpbb_cron_task_core_second_dummy_task',
|
||||
'phpbb_ext_testext_cron_dummy_task',
|
||||
), $tasks);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user