MailerAwareTrait.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Tools\Mailer;
  16. use Cake\Core\App;
  17. use Cake\Mailer\Exception\MissingMailerException;
  18. /**
  19. * Provides functionality for loading mailer classes
  20. * onto properties of the host object.
  21. *
  22. * Example users of this trait are Cake\Controller\Controller and
  23. * Cake\Console\Shell.
  24. */
  25. trait MailerAwareTrait {
  26. /**
  27. * Returns a mailer instance.
  28. *
  29. * @param string $name Mailer's name.
  30. * @param \Cake\Mailer\Email|null $email Email instance.
  31. * @return \Cake\Mailer\Mailer
  32. * @throws \Cake\Mailer\Exception\MissingMailerException if undefined mailer class.
  33. */
  34. public function getMailer($name, Email $email = null) {
  35. if ($email === null) {
  36. $email = new Email();
  37. }
  38. $className = App::className($name, 'Mailer', 'Mailer');
  39. if (empty($className)) {
  40. throw new MissingMailerException(compact('name'));
  41. }
  42. return new $className($email);
  43. }
  44. }