diff --git a/core/core.services.yml b/core/core.services.yml index 3620939..668a04a 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -575,10 +575,7 @@ services: arguments: ['@database', '@request'] plugin.manager.mail: class: Drupal\Core\Mail\MailManager - parent: default_plugin_manager - mail.factory: - class: Drupal\Core\Mail\MailFactory - arguments: ['@config.factory', '@plugin.manager.mail'] + arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler', '@config.factory'] plugin.manager.condition: class: Drupal\Core\Condition\ConditionManager parent: default_plugin_manager diff --git a/core/includes/mail.inc b/core/includes/mail.inc index 61dfb7e..6a8d2b2 100644 --- a/core/includes/mail.inc +++ b/core/includes/mail.inc @@ -205,7 +205,7 @@ function drupal_mail($module, $key, $to, $langcode, $params = array(), $reply = * @see \Drupal\Core\Mail\MailFactory::get() */ function drupal_mail_system($module, $key) { - return \Drupal::service('mail.factory')->get($module, $key); + return \Drupal::service('plugin.manager.mail')->get($module, $key); } /** diff --git a/core/lib/Drupal/Core/Mail/MailFactory.php b/core/lib/Drupal/Core/Mail/MailFactory.php deleted file mode 100644 index 79edf86..0000000 --- a/core/lib/Drupal/Core/Mail/MailFactory.php +++ /dev/null @@ -1,130 +0,0 @@ -mailConfig = $configFactory->get('system.mail'); - $this->mailManager = $mailManager; - } - - - /** - * Returns an object that implements \Drupal\Core\Mail\MailInterface. - * - * Allows for one or more custom mail backends to format and send mail messages - * composed using drupal_mail(). - * - * The selection of a particular implementation is controlled via the config - * 'system.mail.interface', which is a keyed array. The default - * implementation is the mail plugin whose id is the value of 'default' key. A - * more specific match first to key and then to module will be used in - * preference to the default. To specify a different plugin for all mail sent - * by one module, set the plugin name as the value for the key corresponding to - * the module name. To specify a plugin for a particular message sent by one - * module, set the plugin id as the value for the array key that is the - * message id, which is "${module}_${key}". - * - * For example to debug all mail sent by the user module by logging it to a - * file, you might set the variable as something like: - * - * @code - * array( - * 'default' => 'PhpMail', - * 'user' => 'DevelMailLog', - * ); - * @endcode - * - * Finally, a different system can be specified for a specific e-mail ID (see - * the $key param), such as one of the keys used by the contact module: - * - * @code - * array( - * 'default' => 'PhpMail', - * 'user' => 'DevelMailLog', - * 'contact_page_autoreply' => 'NullMail', - * ); - * @endcode - * - * Other possible uses for system include a mail-sending plugin that actually - * sends (or duplicates) each message to SMS, Twitter, instant message, etc, - * or a plugin that queues up a large number of messages for more efficient - * bulk sending or for sending via a remote gateway so as to reduce the load - * on the local server. - * - * @param string $module - * The module name which was used by drupal_mail() to invoke hook_mail(). - * @param string $key - * A key to identify the e-mail sent. The final e-mail ID for the e-mail - * alter hook in drupal_mail() would have been {$module}_{$key}. - * - * @return \Drupal\Core\Mail\MailInterface - * An object that implements Drupal\Core\Mail\MailInterface. - * - * @throws \Exception - */ - public function get($module, $key) { - $mail_id = $module . '_' . $key; - - $configuration = $this->mailConfig->get('interface'); - - // Look for overrides for the default mail plugin, starting from the most - // specific mail_id, and falling back to the module name. - if (isset($configuration[$mail_id])) { - $plugin_id = $configuration[$mail_id]; - } - elseif (isset($configuration[$module])) { - $plugin_id = $configuration[$module]; - } - else { - $plugin_id = $configuration['default']; - } - - if (empty($this->instances[$plugin_id])) { - $plugin = $this->mailManager->createInstance($plugin_id); - $interfaces = class_implements($plugin); - if (isset($interfaces['Drupal\Core\Mail\MailInterface'])) { - $this->instances[$plugin_id] = $plugin; - } - else { - throw new \Exception(String::format('Class %class does not implement interface %interface', array('%class' => $class, '%interface' => 'Drupal\Core\Mail\MailInterface'))); - } - } - return $this->instances[$plugin_id]; - } - -} diff --git a/core/lib/Drupal/Core/Mail/MailInterface.php b/core/lib/Drupal/Core/Mail/MailInterface.php index 8839c54..848796c 100644 --- a/core/lib/Drupal/Core/Mail/MailInterface.php +++ b/core/lib/Drupal/Core/Mail/MailInterface.php @@ -17,10 +17,10 @@ * * Allows to preprocess, format, and postprocess a mail message before it is * passed to the sending system. By default, all messages may contain HTML and - * are converted to plain-text by the Drupal\system\Plugin\Mail\PhpMail implementation. + * are converted to plain-text by the Drupal\Core\Mail\Plugin\Mail\PhpMail implementation. * For example, an alternative implementation could override the default * implementation and additionally sanitize the HTML for usage in a - * MIME-encoded e-mail, but still invoking the Drupal\system\Plugin\Mail\PhpMail + * MIME-encoded e-mail, but still invoking the Drupal\Core\Mail\Plugin\Mail\PhpMail * implementation to generate an alternate plain-text version for sending. * * @param array $message diff --git a/core/lib/Drupal/Core/Mail/MailManager.php b/core/lib/Drupal/Core/Mail/MailManager.php index eec9f90..a6f3b97 100644 --- a/core/lib/Drupal/Core/Mail/MailManager.php +++ b/core/lib/Drupal/Core/Mail/MailManager.php @@ -11,11 +11,28 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Config\ConfigFactory; +use Drupal\Component\Utility\String; /** * Mail plugin manager. */ class MailManager extends DefaultPluginManager { + + /** + * Config object for mail system configurations. + * + * @var \Drupal\Core\Config\Config + */ + protected $mailConfig; + + /** + * List of already instantiated mail system objects. + * + * @var array + */ + protected $instances = array(); + /** * Constructs the MailManager object. * @@ -28,9 +45,96 @@ class MailManager extends DefaultPluginManager { * The language manager. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler to invoke the alter hook with. + * @param \Drupal\Core\Config\ConfigFactory $configFactory + * The configuration factory. */ - public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) { + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler, ConfigFactory $configFactory) { parent::__construct('Plugin/Mail', $namespaces); $this->setCacheBackend($cache_backend, $language_manager, 'mail_backend_plugins'); + $this->mailConfig = $configFactory->get('system.mail'); + } + + /** + * Returns the mail plugin for a given message id. + * + * Allows for one or more custom mail backends to format and send mail messages + * composed using drupal_mail(). + * + * The selection of a particular implementation is controlled via the config + * 'system.mail.interface', which is a keyed array. The default + * implementation is the mail plugin whose id is the value of 'default' key. A + * more specific match first to key and then to module will be used in + * preference to the default. To specify a different plugin for all mail sent + * by one module, set the plugin name as the value for the key corresponding to + * the module name. To specify a plugin for a particular message sent by one + * module, set the plugin id as the value for the array key that is the + * message id, which is "${module}_${key}". + * + * For example to debug all mail sent by the user module by logging it to a + * file, you might set the variable as something like: + * + * @code + * array( + * 'default' => 'PhpMail', + * 'user' => 'DevelMailLog', + * ); + * @endcode + * + * Finally, a different system can be specified for a specific message id (see + * the $key param), such as one of the keys used by the contact module: + * + * @code + * array( + * 'default' => 'PhpMail', + * 'user' => 'DevelMailLog', + * 'contact_page_autoreply' => 'NullMail', + * ); + * @endcode + * + * Other possible uses for system include a mail-sending plugin that actually + * sends (or duplicates) each message to SMS, Twitter, instant message, etc, + * or a plugin that queues up a large number of messages for more efficient + * bulk sending or for sending via a remote gateway so as to reduce the load + * on the local server. + * + * @param string $module + * The module name which was used by drupal_mail() to invoke hook_mail(). + * @param string $key + * A key to identify the e-mail sent. The final message id for the e-mail + * alter hook in drupal_mail() would have been {$module}_{$key}. + * + * @return \Drupal\Core\Mail\MailInterface + * An object that implements Drupal\Core\Mail\MailInterface. + * + * @throws \Exception + */ + public function get($module, $key) { + $message_id = $module . '_' . $key; + + $configuration = $this->mailConfig->get('interface'); + + // Look for overrides for the default mail plugin, starting from the most + // specific message_id, and falling back to the module name. + if (isset($configuration[$message_id])) { + $plugin_id = $configuration[$message_id]; + } + elseif (isset($configuration[$module])) { + $plugin_id = $configuration[$module]; + } + else { + $plugin_id = $configuration['default']; + } + + if (empty($this->instances[$plugin_id])) { + $plugin = $this->createInstance($plugin_id); + $interfaces = class_implements($plugin); + if (isset($interfaces['Drupal\Core\Mail\MailInterface'])) { + $this->instances[$plugin_id] = $plugin; + } + else { + throw new \Exception(String::format('Class %class does not implement interface %interface', array('%class' => get_class($plugin), '%interface' => 'Drupal\Core\Mail\MailInterface'))); + } + } + return $this->instances[$plugin_id]; } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/Mail/PhpMail.php b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php similarity index 94% rename from core/modules/system/lib/Drupal/system/Plugin/Mail/PhpMail.php rename to core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php index d98208f..71986e1 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Mail/PhpMail.php +++ b/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php @@ -2,10 +2,10 @@ /** * @file - * Definition of Drupal\system\Plugin\Mail\PhpMail. + * Definition of Drupal\Core\Mail\Plugin\Mail\PhpMail. */ -namespace Drupal\system\Plugin\Mail; +namespace Drupal\Core\Mail\Plugin\Mail; use Drupal\Component\Annotation\Plugin; use Drupal\Core\Mail\MailInterface; @@ -14,7 +14,9 @@ * The default Drupal mail backend using PHP's mail function. * * @Plugin( - * id = "PhpMail" + * id = "PhpMail", + * label = @Translation("PHP Mailer"), + * description = @Translation("Sends the message as plain text, using PHP's native mail() function.") * ) */ class PhpMail implements MailInterface { diff --git a/core/modules/system/lib/Drupal/system/Plugin/Mail/TestMailCollector.php b/core/lib/Drupal/Core/Mail/Plugin/Mail/TestMailCollector.php similarity index 64% rename from core/modules/system/lib/Drupal/system/Plugin/Mail/TestMailCollector.php rename to core/lib/Drupal/Core/Mail/Plugin/Mail/TestMailCollector.php index 785bb3d..47ac6e6 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Mail/TestMailCollector.php +++ b/core/lib/Drupal/Core/Mail/Plugin/Mail/TestMailCollector.php @@ -2,10 +2,10 @@ /** * @file - * Contains \Drupal\system\Plugin\Mail\TestMailCollector. + * Contains \Drupal\Core\Mail\Plugin\Mail\TestMailCollector. */ -namespace Drupal\system\Plugin\Mail; +namespace Drupal\Core\Mail\Plugin\Mail; use Drupal\Component\Annotation\Plugin; use Drupal\Core\Mail\MailInterface; @@ -17,13 +17,15 @@ * This class is for running tests or for development. * * @Plugin( - * id = "TestMailCollector" + * id = "TestMailCollector", + * label = @Translation("Mail Collector"), + * description = @Translation("Does not send the message, but stores it in Drupal within the state system. Used for development and testing.") * ) */ class TestMailCollector extends PhpMail implements MailInterface { /** - * Overrides \Drupal\system\Plugin\Mail\PhpMail::mail(). + * Overrides \Drupal\Core\Mail\Plugin\Mail\PhpMail::mail(). * * Accepts an e-mail message and stores it with the state system. */ diff --git a/core/modules/system/tests/modules/system_mail_failure_test/lib/Drupal/system_mail_failure_test/Plugin/Mail/TestPhpMailFailure.php b/core/modules/system/tests/modules/system_mail_failure_test/lib/Drupal/system_mail_failure_test/Plugin/Mail/TestPhpMailFailure.php index 6c0d2b7..a10d39c 100644 --- a/core/modules/system/tests/modules/system_mail_failure_test/lib/Drupal/system_mail_failure_test/Plugin/Mail/TestPhpMailFailure.php +++ b/core/modules/system/tests/modules/system_mail_failure_test/lib/Drupal/system_mail_failure_test/Plugin/Mail/TestPhpMailFailure.php @@ -8,7 +8,7 @@ namespace Drupal\system_mail_failure_test\Plugin\Mail; use Drupal\Component\Annotation\Plugin; -use Drupal\system\Plugin\Mail\PhpMail; +use Drupal\Core\Mail\Plugin\Mail\PhpMail; use Drupal\Core\Mail\MailInterface; /** @@ -21,13 +21,15 @@ * @endcode * * @Plugin( - * id = "TestPhpMailFailure" + * id = "TestPhpMailFailure", + * label = @Translation("Malfunctioning mail backend"), + * description = @Translation("An intentionally broken mail backend, used for tests.") * ) */ class TestPhpMailFailure extends PhpMail implements MailInterface { /** - * Overrides Drupal\system\Plugin\Mail\PhpMail::mail(). + * Overrides Drupal\Core\Mail\Plugin\Mail\PhpMail::mail(). */ public function mail(array $message) { // Instead of attempting to send a message, just return failure.