From 49d2326cf6757b92315dca92420b76a95f7355e1 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Thu, 5 Sep 2019 12:41:21 +0800 Subject: [PATCH] 3.0 --- src/mail/Mailable.php | 23 ++++++-------------- src/mail/Mailer.php | 1 - src/mail/Message.php | 50 ++++++++++++++++++++----------------------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/src/mail/Mailable.php b/src/mail/Mailable.php index 7129780..584f9e3 100644 --- a/src/mail/Mailable.php +++ b/src/mail/Mailable.php @@ -11,21 +11,7 @@ namespace yunwuxin\mail; -use ReflectionClass; -use ReflectionProperty; -use RuntimeException; use think\Collection; -use think\facade\App; -use think\facade\Config; -use think\facade\Env; -use think\facade\View; -use think\helper\Str; -use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; -use Twig_Environment; -use Twig_SimpleFilter; -use Twig_SimpleFunction; -use yunwuxin\mail\twig\Loader; -use yunwuxin\mail\twig\TokenParser\Component; /** * Class Mailable @@ -75,6 +61,8 @@ class Mailable public $callbacks = []; + public $markdownCallback = null; + protected function build() { //... @@ -225,10 +213,11 @@ public function text($textView, array $data = []) return $this; } - public function markdown($markdown, array $data = []) + public function markdown($markdown, array $data = [], $callback = null) { - $this->markdown = $markdown; - $this->viewData = $data; + $this->markdown = $markdown; + $this->viewData = $data; + $this->markdownCallback = $callback; return $this; } diff --git a/src/mail/Mailer.php b/src/mail/Mailer.php index bb0c9ca..1f4d982 100644 --- a/src/mail/Mailer.php +++ b/src/mail/Mailer.php @@ -95,7 +95,6 @@ public function send(Mailable $mailable) */ public function sendNow(Mailable $mailable) { - $message = $this->createMessage($mailable); if (isset($this->to['address'])) { diff --git a/src/mail/Message.php b/src/mail/Message.php index 2ce8fc1..133ab82 100644 --- a/src/mail/Message.php +++ b/src/mail/Message.php @@ -11,6 +11,7 @@ namespace yunwuxin\mail; +use Closure; use ReflectionClass; use ReflectionProperty; use Swift_Attachment; @@ -42,9 +43,6 @@ class Message /** @var App */ protected $app; - /** @var Twig */ - protected $twig; - public function __construct(Mailable $mailable, View $view, App $app) { $this->swift = new Swift_Message(); @@ -97,7 +95,7 @@ protected function buildContent(Mailable $mailable) if (isset($mailable->markdown)) { - $html = $this->parseDown($mailable->markdown, $data); + $html = $this->parseDown($mailable->markdown, $data, $mailable->markdownCallback); $html = (new CssToInlineStyles())->convert($html, file_get_contents(__DIR__ . '/resource/css/default.css')); @@ -114,36 +112,34 @@ protected function buildContent(Mailable $mailable) return $this; } - protected function getTwig() + /** + * 解析markdown + * @param $view + * @param $data + * @param Closure $callback + * @return string + */ + protected function parseDown($view, $data, Closure $callback = null) { - if (!$this->twig) { - $config = $this->app->config->get('template', []); + $config = $this->app->config->get('template', []); - $this->twig = new Twig($this->app, $config); + $twig = new Twig($this->app, $config); - $this->twig->getTwig()->addFilter(new TwigFilter('markdown', function ($content) { - $parser = new Markdown(); - $parser->html5 = true; - return $parser->parse($content); - })); + $twig->getTwig()->addFilter(new TwigFilter('markdown', function ($content) { + $parser = new Markdown(); + $parser->html5 = true; + return $parser->parse($content); + })); - $this->twig->getTwig()->addTokenParser(new Component()); + $twig->getTwig()->addTokenParser(new Component()); - $this->twig->getLoader()->addPath(__DIR__ . '/resource/view', 'mail'); - } + $twig->getLoader()->addPath(__DIR__ . '/resource/view', 'mail'); - return $this->twig; - } + if ($callback) { + $callback($twig); + } - /** - * 解析markdown - * @param $view - * @param $data - * @return string - */ - protected function parseDown($view, $data) - { - return $this->getTwig()->getTwig()->render($view . '.twig', $data); + return $twig->getTwig()->render($view . '.twig', $data); } /**