1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.1.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Mailer;
16:
17: use Cake\Core\App;
18: use Cake\Mailer\Exception\MissingMailerException;
19:
20: /**
21: * Provides functionality for loading mailer classes
22: * onto properties of the host object.
23: *
24: * Example users of this trait are Cake\Controller\Controller and
25: * Cake\Console\Shell.
26: */
27: trait MailerAwareTrait
28: {
29: /**
30: * Returns a mailer instance.
31: *
32: * @param string $name Mailer's name.
33: * @param \Cake\Mailer\Email|null $email Email instance.
34: * @return \Cake\Mailer\Mailer
35: * @throws \Cake\Mailer\Exception\MissingMailerException if undefined mailer class.
36: */
37: protected function getMailer($name, Email $email = null)
38: {
39: if ($email === null) {
40: $email = new Email();
41: }
42:
43: $className = App::className($name, 'Mailer', 'Mailer');
44:
45: if (empty($className)) {
46: throw new MissingMailerException(compact('name'));
47: }
48:
49: return new $className($email);
50: }
51: }
52: