Renderer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Mailer;
  17. use Cake\View\View;
  18. use Cake\View\ViewVarsTrait;
  19. /**
  20. * Class for rendering email message.
  21. */
  22. class Renderer
  23. {
  24. use ViewVarsTrait;
  25. /**
  26. * Constant for folder name containing email templates.
  27. *
  28. * @var string
  29. */
  30. public const TEMPLATE_FOLDER = 'email';
  31. /**
  32. * Constructor
  33. */
  34. public function __construct()
  35. {
  36. $this->reset();
  37. }
  38. /**
  39. * Render text/HTML content.
  40. *
  41. * If there is no template set, the $content will be returned in a hash
  42. * of the specified content types for the email.
  43. *
  44. * @param string $content The content.
  45. * @param array<string> $types Content types to render. Valid array values are Message::MESSAGE_HTML, Message::MESSAGE_TEXT.
  46. * @return array<string, mixed> The rendered content with "html" and/or "text" keys.
  47. * @psalm-param array<\Cake\Mailer\Message::MESSAGE_HTML|\Cake\Mailer\Message::MESSAGE_TEXT> $types
  48. * @psalm-return array{html?: string, text?: string}
  49. */
  50. public function render(string $content, array $types = []): array
  51. {
  52. $rendered = [];
  53. $template = $this->viewBuilder()->getTemplate();
  54. if (empty($template)) {
  55. foreach ($types as $type) {
  56. $rendered[$type] = $content;
  57. }
  58. return $rendered;
  59. }
  60. $view = $this->createView();
  61. [$templatePlugin] = pluginSplit($view->getTemplate());
  62. [$layoutPlugin] = pluginSplit($view->getLayout());
  63. if ($templatePlugin) {
  64. $view->setPlugin($templatePlugin);
  65. } elseif ($layoutPlugin) {
  66. $view->setPlugin($layoutPlugin);
  67. }
  68. if ($view->get('content') === null) {
  69. $view->set('content', $content);
  70. }
  71. foreach ($types as $type) {
  72. $view->setTemplatePath(static::TEMPLATE_FOLDER . DIRECTORY_SEPARATOR . $type);
  73. $view->setLayoutPath(static::TEMPLATE_FOLDER . DIRECTORY_SEPARATOR . $type);
  74. $rendered[$type] = $view->render();
  75. }
  76. return $rendered;
  77. }
  78. /**
  79. * Reset view builder to defaults.
  80. *
  81. * @return $this
  82. */
  83. public function reset()
  84. {
  85. $this->_viewBuilder = null;
  86. $this->viewBuilder()
  87. ->setClassName(View::class)
  88. ->setLayout('default')
  89. ->setHelpers(['Html'], false);
  90. return $this;
  91. }
  92. /**
  93. * Clone ViewBuilder instance when renderer is cloned.
  94. *
  95. * @return void
  96. */
  97. public function __clone()
  98. {
  99. if ($this->_viewBuilder !== null) {
  100. $this->_viewBuilder = clone $this->_viewBuilder;
  101. }
  102. }
  103. }