[ticket/17135] Use email method instead of service collection where appropriate

PHPBB-17135
This commit is contained in:
rxu 2024-06-23 14:29:17 +07:00
parent 499464e1d3
commit 3fddff240c
No known key found for this signature in database
GPG key ID: 955F0567380E586A
9 changed files with 124 additions and 57 deletions

View file

@ -327,7 +327,7 @@ services:
- '@config' - '@config'
- '@language' - '@language'
- '@log' - '@log'
- '@messenger.method_collection' - '@messenger.method.email'
- '@notification_manager' - '@notification_manager'
- '@user_loader' - '@user_loader'
- '%core.root_path%' - '%core.root_path%'
@ -342,7 +342,7 @@ services:
- '@dbal.conn' - '@dbal.conn'
- '@config' - '@config'
- '@language' - '@language'
- '@messenger.method_collection' - '@messenger.method.email'
- '@passwords.manager' - '@passwords.manager'
- '%core.root_path%' - '%core.root_path%'
- '%core.php_ext%' - '%core.php_ext%'

View file

@ -5,6 +5,7 @@ imports:
- { resource: ../../default/container/services_filesystem.yml } - { resource: ../../default/container/services_filesystem.yml }
- { resource: ../../default/container/services_http.yml } - { resource: ../../default/container/services_http.yml }
- { resource: ../../default/container/services_language.yml } - { resource: ../../default/container/services_language.yml }
- { resource: ../../default/container/services_messenger.yml }
- { resource: ../../default/container/services_php.yml } - { resource: ../../default/container/services_php.yml }
- { resource: ../../default/container/services_routing.yml } - { resource: ../../default/container/services_routing.yml }
- { resource: ../../default/container/services_twig.yml } - { resource: ../../default/container/services_twig.yml }

View file

@ -17,10 +17,10 @@ use phpbb\config\config;
use phpbb\console\command\command; use phpbb\console\command\command;
use phpbb\language\language; use phpbb\language\language;
use phpbb\log\log_interface; use phpbb\log\log_interface;
use phpbb\messenger\method\email;
use phpbb\notification\manager; use phpbb\notification\manager;
use phpbb\user; use phpbb\user;
use phpbb\user_loader; use phpbb\user_loader;
use phpbb\di\service_collection;
use Symfony\Component\Console\Command\Command as symfony_command; use Symfony\Component\Console\Command\Command as symfony_command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -33,15 +33,15 @@ class activate extends command
/** @var config */ /** @var config */
protected $config; protected $config;
/** @var email */
protected $email_method;
/** @var language */ /** @var language */
protected $language; protected $language;
/** @var log_interface */ /** @var log_interface */
protected $log; protected $log;
/** @var service_collection */
protected $messenger;
/** @var manager */ /** @var manager */
protected $notifications; protected $notifications;
@ -69,18 +69,18 @@ class activate extends command
* @param config $config * @param config $config
* @param language $language * @param language $language
* @param log_interface $log * @param log_interface $log
* @param service_collection $messenger * @param email $email_method
* @param manager $notifications * @param manager $notifications
* @param user_loader $user_loader * @param user_loader $user_loader
* @param string $phpbb_root_path * @param string $phpbb_root_path
* @param string $php_ext * @param string $php_ext
*/ */
public function __construct(user $user, config $config, language $language, log_interface $log, service_collection $messenger, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext) public function __construct(user $user, config $config, language $language, log_interface $log, email $email_method, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext)
{ {
$this->config = $config; $this->config = $config;
$this->email_method = $email_method;
$this->language = $language; $this->language = $language;
$this->log = $log; $this->log = $log;
$this->messenger = $messenger;
$this->notifications = $notifications; $this->notifications = $notifications;
$this->user_loader = $user_loader; $this->user_loader = $user_loader;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
@ -200,15 +200,14 @@ class activate extends command
if ($input->getOption('send-email')) if ($input->getOption('send-email'))
{ {
$email_method = $this->messenger->offsetGet('messenger.method.email'); $this->email_method->set_use_queue(false);
$email_method->set_use_queue(false); $this->email_method->template('admin_welcome_activated', $user_row['user_lang']);
$email_method->template('admin_welcome_activated', $user_row['user_lang']); $this->email_method->set_addresses($user_row);
$email_method->set_addresses($user_row); $this->email_method->anti_abuse_headers($this->config, $this->user);
$email_method->anti_abuse_headers($this->config, $this->user); $this->email_method->assign_vars([
$email_method->assign_vars([
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT), 'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
]); ]);
$email_method->send(); $this->email_method->send();
} }
} }
} }

View file

@ -18,9 +18,9 @@ use phpbb\console\command\command;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\exception\runtime_exception; use phpbb\exception\runtime_exception;
use phpbb\language\language; use phpbb\language\language;
use phpbb\messenger\method\email;
use phpbb\passwords\manager; use phpbb\passwords\manager;
use phpbb\user; use phpbb\user;
use phpbb\di\service_collection;
use Symfony\Component\Console\Command\Command as symfony_command; use Symfony\Component\Console\Command\Command as symfony_command;
use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -40,12 +40,12 @@ class add extends command
/** @var config */ /** @var config */
protected $config; protected $config;
/** @var email */
protected $email_method;
/** @var language */ /** @var language */
protected $language; protected $language;
/** @var service_collection */
protected $messenger;
/** @var manager */ /** @var manager */
protected $password_manager; protected $password_manager;
@ -75,12 +75,12 @@ class add extends command
* @param string $phpbb_root_path * @param string $phpbb_root_path
* @param string $php_ext * @param string $php_ext
*/ */
public function __construct(user $user, driver_interface $db, config $config, language $language, service_collection $messenger, manager $password_manager, $phpbb_root_path, $php_ext) public function __construct(user $user, driver_interface $db, config $config, language $language, email $email_method, manager $password_manager, $phpbb_root_path, $php_ext)
{ {
$this->db = $db;
$this->config = $config; $this->config = $config;
$this->db = $db;
$this->email_method = $email_method;
$this->language = $language; $this->language = $language;
$this->messenger = $messenger;
$this->password_manager = $password_manager; $this->password_manager = $password_manager;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext; $this->php_ext = $php_ext;
@ -313,18 +313,17 @@ class add extends command
$user_actkey = $this->get_activation_key($user_id); $user_actkey = $this->get_activation_key($user_id);
$email_method = $this->messenger->offsetGet('messenger.method.email'); $this->email_method->set_use_queue(false);
$email_method->set_use_queue(false); $this->email_method->template($email_template, $this->user->lang_name);
$email_method->template($email_template, $this->user->lang_name); $this->email_method->to($this->data['email'], $this->data['username']);
$email_method->to($this->data['email'], $this->data['username']); $this->email_method->anti_abuse_headers($this->config, $this->user);
$email_method->anti_abuse_headers($this->config, $this->user); $this->email_method->assign_vars([
$email_method->assign_vars([
'WELCOME_MSG' => html_entity_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename']), ENT_COMPAT), 'WELCOME_MSG' => html_entity_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename']), ENT_COMPAT),
'USERNAME' => html_entity_decode($this->data['username'], ENT_COMPAT), 'USERNAME' => html_entity_decode($this->data['username'], ENT_COMPAT),
'PASSWORD' => html_entity_decode($this->data['new_password'], ENT_COMPAT), 'PASSWORD' => html_entity_decode($this->data['new_password'], ENT_COMPAT),
'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey", 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey",
]); ]);
$email_method->send(); $this->email_method->send();
} }
/** /**

View file

@ -20,7 +20,7 @@ use phpbb\auth\auth;
use phpbb\log\log_interface; use phpbb\log\log_interface;
use phpbb\user; use phpbb\user;
use phpbb\install\helper\container_factory; use phpbb\install\helper\container_factory;
use phpbb\di\service_collection; use phpbb\messenger\method\email;
/** /**
* Logs installation and sends an email to the admin * Logs installation and sends an email to the admin
@ -39,6 +39,9 @@ class notify_user extends \phpbb\install\task_base
/** @var db */ /** @var db */
protected $config; protected $config;
/** @var email */
protected $email_method;
/** @var log_interface */ /** @var log_interface */
protected $log; protected $log;
@ -48,9 +51,6 @@ class notify_user extends \phpbb\install\task_base
/** @var user */ /** @var user */
protected $user; protected $user;
/** @var service_collection */
protected $messenger;
/** /**
* Constructor * Constructor
* *
@ -67,7 +67,7 @@ class notify_user extends \phpbb\install\task_base
$this->auth = $container->get('auth'); $this->auth = $container->get('auth');
$this->log = $container->get('log'); $this->log = $container->get('log');
$this->user = $container->get('user'); $this->user = $container->get('user');
$this->messenger = $container->get('messenger.method_collection'); $this->email_method = $container->get('messenger.method.email');
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
// We need to reload config for cases when it doesn't have all values // We need to reload config for cases when it doesn't have all values
@ -97,16 +97,15 @@ class notify_user extends \phpbb\install\task_base
if ($this->config['email_enable']) if ($this->config['email_enable'])
{ {
$email_method = $this->messenger->offsetGet('messenger.method.email'); $this->email_method->set_use_queue(false);
$email_method->set_use_queue(false); $this->email_method->template('installed', $this->install_config->get('user_language', 'en'));
$email_method->template('installed', $this->install_config->get('user_language', 'en')); $this->email_method->to($this->config['board_email'], $this->install_config->get('admin_name'));
$email_method->to($this->config['board_email'], $this->install_config->get('admin_name')); $this->email_method->anti_abuse_headers($this->config, $this->user);
$email_method->anti_abuse_headers($this->config, $this->user); $this->email_method->assign_vars([
$email_method->assign_vars([
'USERNAME' => html_entity_decode($this->install_config->get('admin_name'), ENT_COMPAT), 'USERNAME' => html_entity_decode($this->install_config->get('admin_name'), ENT_COMPAT),
'PASSWORD' => html_entity_decode($this->install_config->get('admin_passwd'), ENT_COMPAT), 'PASSWORD' => html_entity_decode($this->install_config->get('admin_passwd'), ENT_COMPAT),
]); ]);
$email_method->send(); $this->email_method->send();
} }
// Login admin // Login admin

View file

@ -17,13 +17,7 @@ use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email as symfony_email; use Symfony\Component\Mime\Email as symfony_email;
use Symfony\Component\Mime\Header\DateHeader;
use Symfony\Component\Mime\Header\Headers; use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Header\IdentificationHeader;
use Symfony\Component\Mime\Header\MailboxHeader;
use Symfony\Component\Mime\Header\MailboxListHeader;
use Symfony\Component\Mime\Header\PathHeader;
use Symfony\Component\Mime\Header\UnstructuredHeader;
/** /**
* Messenger class * Messenger class

View file

@ -38,11 +38,11 @@ class phpbb_console_user_activate_test extends phpbb_console_user_base
$this->config, $this->config,
$this->language, $this->language,
$this->log, $this->log,
$this->email,
$this->notifications, $this->notifications,
$this->user_loader, $this->user_loader,
$this->phpbb_root_path, $this->phpbb_root_path,
$this->php_ext, $this->php_ext
$this->messenger_method_collection
)); ));
$command = $application->find('user:activate'); $command = $application->find('user:activate');

View file

@ -30,10 +30,10 @@ class phpbb_console_user_add_test extends phpbb_console_user_base
$this->db, $this->db,
$this->config, $this->config,
$this->language, $this->language,
$this->email,
$this->passwords_manager, $this->passwords_manager,
$this->phpbb_root_path, $this->phpbb_root_path,
$this->php_ext, $this->php_ext
$this->messenger_method_collection
)); ));
$command = $application->find('user:add'); $command = $application->find('user:add');

View file

@ -105,10 +105,85 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case
$phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications'); $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications');
$this->messenger_method_collection = new \phpbb\di\service_collection($phpbb_container); $assets_bag = new \phpbb\template\assets_bag();
$this->messenger_method_collection->add('messenger.method.email'); $phpbb_container->set('assets.bag', $assets_bag);
$this->messenger_method_collection->add('messenger.method.jabber');
$phpbb_container->set('messenger.method_collection', $this->messenger_method_collection); $dispatcher = new \phpbb\event\dispatcher();
$phpbb_container->set('dispatcher', $dispatcher);
$core_cache_dir = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/';
$phpbb_container->setParameter('core.cache_dir', $core_cache_dir);
$core_messenger_queue_file = $core_cache_dir . 'queue.' . $phpEx;
$phpbb_container->setParameter('core.messenger_queue_file', $core_messenger_queue_file);
$messenger_method_collection = new \phpbb\di\service_collection($phpbb_container);
$messenger_method_collection->add('messenger.method.email');
$phpbb_container->set('messenger.method_collection', $messenger_method_collection);
$messenger_queue = new \phpbb\messenger\queue($config, $dispatcher, $messenger_method_collection, $core_messenger_queue_file);
$phpbb_container->set('messenger.queue', $messenger_queue);
$request = new phpbb_mock_request;
$phpbb_container->set('request', $request);
$symfony_request = new \phpbb\symfony_request(
$request
);
$phpbb_path_helper = new \phpbb\path_helper(
$symfony_request,
$request,
$phpbb_root_path,
$phpEx
);
$phpbb_container->set('path_helper', $phpbb_path_helper);
$extension_manager = new phpbb_mock_extension_manager(
__DIR__ . '/',
[]
);
$phpbb_container->set('ext.manager', $extension_manager);
$context = new \phpbb\template\context();
$cache_path = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/twig';
$phpbb_container->setParameter('core.template.cache_path', $cache_path);
$filesystem = new \phpbb\filesystem\filesystem();
$phpbb_container->set('filesystem', $filesystem);
$twig = new \phpbb\template\twig\environment(
$assets_bag,
$this->config,
$filesystem,
$phpbb_path_helper,
$cache_path,
null,
new \phpbb\template\twig\loader(''),
$dispatcher,
[
'cache' => false,
'debug' => false,
'auto_reload' => true,
'autoescape' => false,
]
);
$twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang);
$phpbb_container->set('template.twig.extensions.phpbb', $twig_extension);
$twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
$twig_extensions_collection->add('template.twig.extensions.phpbb');
$phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection);
$twig->addExtension($twig_extension);
$twig_lexer = new \phpbb\template\twig\lexer($twig);
$phpbb_container->set('template.twig.lexer', $twig_lexer);
$this->email = new \phpbb\messenger\method\phpbb_email(
$assets_bag, $this->config, $dispatcher, $this->language, $log, $request, $user, $messenger_queue,
$phpbb_path_helper, $extension_manager, $twig_extensions_collection, $twig_lexer,
$cache_path, $phpbb_root_path
);
$phpbb_container->set('messenger.method.email', $this->email);
parent::setUp(); parent::setUp();
} }