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: /**
31: * Returns a mailer instance.
32: *
33: * @param string $name Mailer's name.
34: * @param \Cake\Mailer\Email|null $email Email instance.
35: * @return \Cake\Mailer\Mailer
36: * @throws \Cake\Mailer\Exception\MissingMailerException if undefined mailer class.
37: */
38: protected function getMailer($name, Email $email = null)
39: {
40: if ($email === null) {
41: $email = new Email();
42: }
43:
44: $className = App::className($name, 'Mailer', 'Mailer');
45:
46: if (empty($className)) {
47: throw new MissingMailerException(compact('name'));
48: }
49:
50: return new $className($email);
51: }
52: }
53: