MailManagerWrapper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Drupal\webprofiler\Mail;
  3. use Drupal\Core\Cache\CacheBackendInterface;
  4. use Drupal\Core\Config\ConfigFactoryInterface;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\Logger\LoggerChannelFactoryInterface;
  7. use Drupal\Core\Mail\MailManagerInterface;
  8. use Drupal\Core\Plugin\DefaultPluginManager;
  9. use Drupal\Core\StringTranslation\StringTranslationTrait;
  10. use Drupal\Core\StringTranslation\TranslationInterface;
  11. use Drupal\webprofiler\DataCollector\MailDataCollector;
  12. /**
  13. * Class MailManagerWrapper
  14. */
  15. class MailManagerWrapper extends DefaultPluginManager implements MailManagerInterface {
  16. use StringTranslationTrait;
  17. /**
  18. * @var \Drupal\webprofiler\DataCollector\MailDataCollector
  19. */
  20. private $dataCollector;
  21. /**
  22. * @var \Drupal\Core\Mail\MailManagerInterface
  23. */
  24. private $mailManager;
  25. /**
  26. * The config factory.
  27. *
  28. * @var \Drupal\Core\Config\ConfigFactoryInterface
  29. */
  30. protected $configFactory;
  31. /**
  32. * The logger factory.
  33. *
  34. * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
  35. */
  36. protected $loggerFactory;
  37. /**
  38. * List of already instantiated mail plugins.
  39. *
  40. * @var array
  41. */
  42. protected $instances = array();
  43. /**
  44. * Constructs the MailManager object.
  45. *
  46. * @param \Traversable $namespaces
  47. * An object that implements \Traversable which contains the root paths
  48. * keyed by the corresponding namespace to look for plugin implementations.
  49. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  50. * Cache backend instance to use.
  51. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  52. * The module handler to invoke the alter hook with.
  53. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  54. * The configuration factory.
  55. * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
  56. * The logger channel factory.
  57. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  58. * The string translation service.
  59. * @param \Drupal\Core\Mail\MailManagerInterface $mailManager
  60. * @param \Drupal\webprofiler\DataCollector\MailDataCollector $dataCollector
  61. */
  62. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, MailManagerInterface $mailManager, MailDataCollector $dataCollector) {
  63. parent::__construct('Plugin/Mail', $namespaces, $module_handler, 'Drupal\Core\Mail\MailInterface', 'Drupal\Core\Annotation\Mail');
  64. $this->alterInfo('mail_backend_info');
  65. $this->setCacheBackend($cache_backend, 'mail_backend_plugins');
  66. $this->configFactory = $config_factory;
  67. $this->loggerFactory = $logger_factory;
  68. $this->stringTranslation = $string_translation;
  69. $this->dataCollector = $dataCollector;
  70. $this->mailManager = $mailManager;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE) {
  76. $message = $this->mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send);
  77. $instance = $this->mailManager->getInstance(['module' => $module, 'key' => $key]);
  78. $this->dataCollector->addMessage($message, $instance);
  79. return $message;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. *
  84. * Must call getInstance on the decorated MailManager.
  85. * @see https://www.drupal.org/node/2625554
  86. */
  87. public function getInstance(array $options) {
  88. return $this->mailManager->getInstance($options);
  89. }
  90. }