vendor/sylius/mailer-bundle/src/Bundle/Renderer/Adapter/EmailTwigAdapter.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\MailerBundle\Renderer\Adapter;
  12. use Sylius\Component\Mailer\Event\EmailRenderEvent;
  13. use Sylius\Component\Mailer\Model\EmailInterface;
  14. use Sylius\Component\Mailer\Renderer\Adapter\AbstractAdapter;
  15. use Sylius\Component\Mailer\Renderer\RenderedEmail;
  16. use Sylius\Component\Mailer\SyliusMailerEvents;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. use Twig\Environment;
  19. use Twig\Loader\ArrayLoader;
  20. class EmailTwigAdapter extends AbstractAdapter
  21. {
  22.     /** @var Environment */
  23.     protected $twig;
  24.     /** @var EventDispatcherInterface|null */
  25.     protected $dispatcher;
  26.     public function __construct(Environment $twig, ?EventDispatcherInterface $dispatcher null)
  27.     {
  28.         $this->twig $twig;
  29.         $this->dispatcher $dispatcher;
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function render(EmailInterface $email, array $data = []): RenderedEmail
  35.     {
  36.         $renderedEmail $this->getRenderedEmail($email$data);
  37.         $event = new EmailRenderEvent($renderedEmail);
  38.         if ($this->dispatcher !== null) {
  39.             /** @var EmailRenderEvent $event */
  40.             $event $this->dispatcher->dispatch($eventSyliusMailerEvents::EMAIL_PRE_RENDER);
  41.         }
  42.         return $event->getRenderedEmail();
  43.     }
  44.     private function getRenderedEmail(EmailInterface $email, array $data): RenderedEmail
  45.     {
  46.         if (null !== $email->getTemplate()) {
  47.             return $this->provideEmailWithTemplate($email$data);
  48.         }
  49.         return $this->provideEmailWithoutTemplate($email$data);
  50.     }
  51.     /**
  52.      * @psalm-suppress InternalMethod
  53.      */
  54.     private function provideEmailWithTemplate(EmailInterface $email, array $data): RenderedEmail
  55.     {
  56.         $data $this->twig->mergeGlobals($data);
  57.         $template $this->twig->load((string) $email->getTemplate())->unwrap();
  58.         $subject $template->renderBlock('subject'$data);
  59.         $body $template->renderBlock('body'$data);
  60.         return new RenderedEmail($subject$body);
  61.     }
  62.     private function provideEmailWithoutTemplate(EmailInterface $email, array $data): RenderedEmail
  63.     {
  64.         $twig = new Environment(new ArrayLoader([]));
  65.         $subjectTemplate $twig->createTemplate((string) $email->getSubject());
  66.         $bodyTemplate $twig->createTemplate((string) $email->getContent());
  67.         $subject $subjectTemplate->render($data);
  68.         $body $bodyTemplate->render($data);
  69.         return new RenderedEmail($subject$body);
  70.     }
  71. }