Skip to content

Commit

Permalink
3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Sep 5, 2019
1 parent e48d4b9 commit 49d2326
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
23 changes: 6 additions & 17 deletions src/mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,6 +61,8 @@ class Mailable

public $callbacks = [];

public $markdownCallback = null;

protected function build()
{
//...
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function send(Mailable $mailable)
*/
public function sendNow(Mailable $mailable)
{

$message = $this->createMessage($mailable);

if (isset($this->to['address'])) {
Expand Down
50 changes: 23 additions & 27 deletions src/mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace yunwuxin\mail;

use Closure;
use ReflectionClass;
use ReflectionProperty;
use Swift_Attachment;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'));

Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 49d2326

Please sign in to comment.