Merge pull request #6431 from rubencm/ticket/17062

[ticket/17062] Call search service using dependency injection
This commit is contained in:
Marc Alexander 2022-11-30 19:39:13 +01:00 committed by GitHub
commit f48ff960f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 65 deletions

View file

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

View file

@ -13,75 +13,46 @@
namespace phpbb\cron\task\core;
use phpbb\config\config;
use phpbb\cron\task\base;
use phpbb\search\backend\search_backend_interface;
use phpbb\search\search_backend_factory;
/**
* Tidy search cron task.
*
* 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
* @var \phpbb\config\config
* @var config
*/
protected $config;
/**
* Database object
* @var \phpbb\db\driver\driver_interface
* Search backend factory
* @var search_backend_factory
*/
protected $db;
protected $search_backend_factory;
/**
* User object
* @var \phpbb\user
* Reference to active search backend to avoid calling the factory multiple times
* @var search_backend_interface
*/
protected $user;
/**
* Event dispatcher object
* @var \phpbb\event\dispatcher_interface
*/
protected $phpbb_dispatcher;
protected $active_search;
/**
* 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
* @param config $config The config object
* @param search_backend_factory $search_backend_factory
*/
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)
public function __construct(config $config, search_backend_factory $search_backend_factory)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->auth = $auth;
$this->config = $config;
$this->db = $db;
$this->user = $user;
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->search_backend_factory = $search_backend_factory;
}
/**
@ -91,16 +62,12 @@ class tidy_search extends \phpbb\cron\task\base
*/
public function run()
{
$search_type = $this->config['search_type'];
// 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)
if ($this->active_search === null)
{
$search->tidy();
$this->active_search = $this->search_backend_factory->get_active();
}
$this->active_search->tidy();
}
/**
@ -114,7 +81,19 @@ class tidy_search extends \phpbb\cron\task\base
*/
public function is_runnable()
{
return class_exists($this->config['search_type']);
try
{
if ($this->active_search === null)
{
$this->active_search = $this->search_backend_factory->get_active();
}
}
catch (\RuntimeException $e)
{
return false;
}
return true;
}
/**