[ticket/17191] Use di and avoid circular ref

PHPBB3-17191
This commit is contained in:
Christian Schnegelberger 2023-09-23 11:52:53 +02:00 committed by Marc Alexander
parent 14b7caecd1
commit 8abf9b66e1
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
7 changed files with 45 additions and 29 deletions

View file

@ -3,12 +3,14 @@ services:
class: phpbb\language\language_file_helper class: phpbb\language\language_file_helper
arguments: arguments:
- '%core.root_path%' - '%core.root_path%'
- '@user' - '@config'
language: language:
class: phpbb\language\language class: phpbb\language\language
arguments: arguments:
- '@language.loader' - '@language.loader'
- '@language.helper.language_file'
- '@config'
language.loader_abstract: language.loader_abstract:
abstract: true abstract: true

View file

@ -3829,10 +3829,10 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
$language_file_helper = $phpbb_container->get('language.helper.language_file'); $language_file_helper = $phpbb_container->get('language.helper.language_file');
// Grab the users lang direction and store it for later use // Grab the users lang direction and store it for later use
$direction = $language_file_helper->get_lang_key_value('direction'); $direction = $language_file_helper->get_lang_key_value('direction', $user->data['user_lang']);
// Get the user_lang string // Get the user_lang string
$user_lang = $language_file_helper->get_lang_key_value('user_lang'); $user_lang = $language_file_helper->get_lang_key_value('user_lang', $user->data['user_lang']);
if (strpos($user_lang, '-x-') !== false) if (strpos($user_lang, '-x-') !== false)
{ {
$user_lang = substr($user_lang, 0, strpos($user_lang, '-x-')); $user_lang = substr($user_lang, 0, strpos($user_lang, '-x-'));
@ -3938,7 +3938,7 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
'L_INDEX' => ($config['board_index_text'] !== '') ? $config['board_index_text'] : $user->lang['FORUM_INDEX'], 'L_INDEX' => ($config['board_index_text'] !== '') ? $config['board_index_text'] : $user->lang['FORUM_INDEX'],
'L_SITE_HOME' => ($config['site_home_text'] !== '') ? $config['site_home_text'] : $user->lang['HOME'], 'L_SITE_HOME' => ($config['site_home_text'] !== '') ? $config['site_home_text'] : $user->lang['HOME'],
'L_ONLINE_EXPLAIN' => $l_online_time, 'L_ONLINE_EXPLAIN' => $l_online_time,
'L_RECAPTCHA_LANG' => $language_file_helper->get_lang_key_value('recaptcha_lang'), 'L_RECAPTCHA_LANG' => $language_file_helper->get_lang_key_value('recaptcha_lang', $user->data['user_lang']),
'U_PRIVATEMSGS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'), 'U_PRIVATEMSGS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'),
'U_RETURN_INBOX' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'), 'U_RETURN_INBOX' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'),

View file

@ -74,10 +74,10 @@ function adm_page_header($page_title)
$language_file_helper = $phpbb_container->get('language.helper.language_file'); $language_file_helper = $phpbb_container->get('language.helper.language_file');
// Grab the users lang direction and store it for later use // Grab the users lang direction and store it for later use
$direction = $language_file_helper->get_lang_key_value('direction'); $direction = $language_file_helper->get_lang_key_value('direction', $user->data['user_lang']);
// Get the user_lang string // Get the user_lang string
$user_lang = $language_file_helper->get_lang_key_value('user_lang'); $user_lang = $language_file_helper->get_lang_key_value('user_lang', $user->data['user_lang']);
$template->assign_vars(array( $template->assign_vars(array(
'PAGE_TITLE' => $page_title, 'PAGE_TITLE' => $page_title,

View file

@ -395,7 +395,7 @@ class feed
'FEED_TITLE' => $this->config['sitename'], 'FEED_TITLE' => $this->config['sitename'],
'FEED_SUBTITLE' => $this->config['site_desc'], 'FEED_SUBTITLE' => $this->config['site_desc'],
'FEED_UPDATED' => $this->feed_helper->format_date($feed_updated_time), 'FEED_UPDATED' => $this->feed_helper->format_date($feed_updated_time),
'FEED_LANG' => $this->lang_helper->get_lang_key_value('user_lang'), 'FEED_LANG' => $this->lang_helper->get_lang_key_value('user_lang', $user->data['user_lang']),
'FEED_AUTHOR' => $this->config['sitename'], 'FEED_AUTHOR' => $this->config['sitename'],
// Feed entries // Feed entries

View file

@ -253,19 +253,16 @@ class helper
*/ */
protected function page_header($page_title, $selected_language = false) protected function page_header($page_title, $selected_language = false)
{ {
global $phpbb_container; global $user;
// Path to templates // Path to templates
$paths = array($this->phpbb_root_path . 'install/update/new/adm/', $this->phpbb_admin_path); $paths = array($this->phpbb_root_path . 'install/update/new/adm/', $this->phpbb_admin_path);
$paths = array_filter($paths, 'is_dir'); $paths = array_filter($paths, 'is_dir');
$path = array_shift($paths); $path = array_shift($paths);
$path = substr($path, strlen($this->phpbb_root_path)); $path = substr($path, strlen($this->phpbb_root_path));
// Get the language helper
/* @var $language_helper \phpbb\language\language_file_helper */
$language_file_helper = $phpbb_container->get('language.helper.language_file');
// Grab the users lang direction and store it for later use // Grab the users lang direction and store it for later use
$direction = $language_file_helper->get_lang_key_value('direction'); $direction = $this->lang_helper->get_lang_key_value('direction', $user->data['user_lang']);
$this->template->assign_vars(array( $this->template->assign_vars(array(
'L_CHANGE' => $this->language->lang('CHANGE'), 'L_CHANGE' => $this->language->lang('CHANGE'),
@ -285,7 +282,7 @@ class helper
'S_CONTENT_ENCODING' => 'UTF-8', 'S_CONTENT_ENCODING' => 'UTF-8',
'S_LANG_SELECT' => $selected_language, 'S_LANG_SELECT' => $selected_language,
'S_USER_LANG' => $language_file_helper->get_lang_key_value('user_lang'), 'S_USER_LANG' => $this->lang_helper->get_lang_key_value('user_lang', $user->data['user_lang']),
)); ));
$this->render_navigation(); $this->render_navigation();

View file

@ -13,6 +13,7 @@
namespace phpbb\language; namespace phpbb\language;
use phpbb\config\config;
use phpbb\language\exception\invalid_plural_rule_exception; use phpbb\language\exception\invalid_plural_rule_exception;
/** /**
@ -70,15 +71,29 @@ class language
*/ */
protected $loader; protected $loader;
/**
* @var language_file_helper
*/
protected $lang_helper;
/**
* @var \phpbb\config\config
*/
protected $config;
/** /**
* Constructor * Constructor
* *
* @param \phpbb\language\language_file_loader $loader Language file loader * @param \phpbb\language\language_file_loader $loader Language file loader
* @param language_file_helper $lang_helper
* @param array|null $common_modules Array of common language modules to load (optional) * @param array|null $common_modules Array of common language modules to load (optional)
* @param config $config The config
*/ */
public function __construct(language_file_loader $loader, $common_modules = null) public function __construct(language_file_loader $loader, language_file_helper $lang_helper, config $config, $common_modules = null)
{ {
$this->loader = $loader; $this->loader = $loader;
$this->lang_helper = $lang_helper;
$this->config = $config;
// Set up default information // Set up default information
$this->user_language = null; $this->user_language = null;
@ -403,13 +418,10 @@ class language
*/ */
public function get_plural_form($number, $force_rule = false) public function get_plural_form($number, $force_rule = false)
{ {
global $phpbb_container; global $user;
// Get the language helper
/* @var $language_helper \phpbb\language\language_file_helper */
$language_file_helper = $phpbb_container->get('language.helper.language_file');
// Grab the users lang plural rule // Grab the users lang plural rule
$plural_rule_content = $language_file_helper->get_lang_key_value('plural_rule'); $plural_rule_content = $this->lang_helper->get_lang_key_value('plural_rule', $user->data['user_lang']);
$number = (int) $number; $number = (int) $number;
$plural_rule = ($force_rule !== false) ? $force_rule : ((isset($plural_rule_content)) ? $plural_rule_content : 1); $plural_rule = ($force_rule !== false) ? $force_rule : ((isset($plural_rule_content)) ? $plural_rule_content : 1);

View file

@ -15,8 +15,8 @@ namespace phpbb\language;
use DomainException; use DomainException;
use phpbb\json\sanitizer as json_sanitizer; use phpbb\json\sanitizer as json_sanitizer;
use phpbb\user;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use phpbb\config\config;
/** /**
* Helper class for language file related functions * Helper class for language file related functions
@ -28,19 +28,22 @@ class language_file_helper
*/ */
protected $phpbb_root_path; protected $phpbb_root_path;
/** @var user */ /**
protected $user; * @var \phpbb\config\config
*/
protected $config;
/** /**
* Constructor * Constructor
* *
* @param string $phpbb_root_path Path to phpBB's root * @param string $phpbb_root_path Path to phpBB's root
* @param user $user User Object * @param config $config The config
*
*/ */
public function __construct(string $phpbb_root_path, \phpbb\user $user) public function __construct(string $phpbb_root_path, config $config)
{ {
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
$this->user = $user; $this->config = $config;
} }
/** /**
@ -79,16 +82,18 @@ class language_file_helper
* @return string * @return string
* *
*/ */
public function get_lang_key_value($lang_key) : string public function get_lang_key_value($lang_key, $user_lang_db) : string
{ {
$available_languages = $this->get_available_languages(); $available_languages = $this->get_available_languages();
foreach ($available_languages as $key => $value) foreach ($available_languages as $key => $value)
{ {
$available_languages[$value['iso']] = $value; $available_languages[$value['iso']] = $value;
unset($available_languages[$key]);
} }
$user_lang = $this->user->data['user_lang'] ?? 'en'; $board_default_lang = $this->config['default_lang'];
return $available_languages[$user_lang][$lang_key]; $user_lang_db = $user_lang_db ?? $board_default_lang;
return $available_languages[$user_lang_db][$lang_key];
} }
/** /**