[ticket/17062] Call search service using dependency injection

PHPBB3-17062
This commit is contained in:
Ruben Calvo 2022-11-26 16:31:56 +01:00
parent 1b6902c4bf
commit 6fc476c692
2 changed files with 29 additions and 69 deletions

View file

@ -130,13 +130,8 @@ services:
cron.task.core.tidy_search: cron.task.core.tidy_search:
class: phpbb\cron\task\core\tidy_search class: phpbb\cron\task\core\tidy_search
arguments: arguments:
- '%core.root_path%'
- '%core.php_ext%'
- '@auth'
- '@config' - '@config'
- '@dbal.conn' - '@search.backend_factory'
- '@user'
- '@dispatcher'
calls: calls:
- [set_name, [cron.task.core.tidy_search]] - [set_name, [cron.task.core.tidy_search]]
tags: tags:

View file

@ -13,75 +13,39 @@
namespace phpbb\cron\task\core; namespace phpbb\cron\task\core;
use phpbb\config\config;
use phpbb\cron\task\base;
use phpbb\search\search_backend_factory;
/** /**
* Tidy search cron task. * Tidy search cron task.
* *
* Will only run when the currently selected search backend supports tidying. * Will only run when the currently selected search backend supports tidying.
*/ */
class tidy_search extends \phpbb\cron\task\base class tidy_search extends base
{ {
/**
* phpBB root path
* @var string
*/
protected $phpbb_root_path;
/**
* PHP file extension
* @var string
*/
protected $php_ext;
/**
* Auth object
* @var \phpbb\auth\auth
*/
protected $auth;
/** /**
* Config object * Config object
* @var \phpbb\config\config * @var config
*/ */
protected $config; protected $config;
/** /**
* Database object * Search backend factory
* @var \phpbb\db\driver\driver_interface * @var search_backend_factory
*/ */
protected $db; protected $search_backend_factory;
/** /**
* User object * Constructor.
* @var \phpbb\user *
*/ * @param config $config The config object
protected $user; * @param search_backend_factory $search_backend_factory
*/
/** public function __construct(config $config, search_backend_factory $search_backend_factory)
* Event dispatcher object
* @var \phpbb\event\dispatcher_interface
*/
protected $phpbb_dispatcher;
/**
* Constructor.
*
* @param string $phpbb_root_path The phpBB root path
* @param string $php_ext The PHP file extension
* @param \phpbb\auth\auth $auth The auth object
* @param \phpbb\config\config $config The config object
* @param \phpbb\db\driver\driver_interface $db The database object
* @param \phpbb\user $user The user object
* @param \phpbb\event\dispatcher_interface $phpbb_dispatcher The event dispatcher object
*/
public function __construct($phpbb_root_path, $php_ext, \phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbb\event\dispatcher_interface $phpbb_dispatcher)
{ {
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->auth = $auth;
$this->config = $config; $this->config = $config;
$this->db = $db; $this->search_backend_factory = $search_backend_factory;
$this->user = $user;
$this->phpbb_dispatcher = $phpbb_dispatcher;
} }
/** /**
@ -91,16 +55,8 @@ class tidy_search extends \phpbb\cron\task\base
*/ */
public function run() public function run()
{ {
$search_type = $this->config['search_type']; $search = $this->search_backend_factory->get_active();
$search->tidy();
// We do some additional checks in the module to ensure it can actually be utilised
$error = false;
$search = new $search_type($error, $this->phpbb_root_path, $this->php_ext, $this->auth, $this->config, $this->db, $this->user, $this->phpbb_dispatcher);
if (!$error)
{
$search->tidy();
}
} }
/** /**
@ -114,7 +70,16 @@ class tidy_search extends \phpbb\cron\task\base
*/ */
public function is_runnable() public function is_runnable()
{ {
return class_exists($this->config['search_type']); try
{
$this->search_backend_factory->get_active();
}
catch (\RuntimeException $e)
{
return false;
}
return true;
} }
/** /**