-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Twig] Add NotificationEmail #33605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Twig] Add NotificationEmail #33605
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Mime; | ||
|
||
use Symfony\Component\ErrorRenderer\Exception\FlattenException; | ||
use Symfony\Component\Mime\Header\Headers; | ||
use Symfony\Component\Mime\Part\AbstractPart; | ||
use Twig\Extra\CssInliner\CssInlinerExtension; | ||
use Twig\Extra\Inky\InkyExtension; | ||
use Twig\Extra\Markdown\MarkdownExtension; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class NotificationEmail extends TemplatedEmail | ||
{ | ||
public const IMPORTANCE_URGENT = 'urgent'; | ||
public const IMPORTANCE_HIGH = 'high'; | ||
public const IMPORTANCE_MEDIUM = 'medium'; | ||
public const IMPORTANCE_LOW = 'low'; | ||
|
||
private $theme = 'default'; | ||
private $context = [ | ||
'importance' => self::IMPORTANCE_LOW, | ||
'content' => '', | ||
'exception' => false, | ||
'action_text' => null, | ||
'action_url' => null, | ||
'markdown' => false, | ||
'raw' => false, | ||
]; | ||
|
||
public function __construct(Headers $headers = null, AbstractPart $body = null) | ||
{ | ||
if (!class_exists(CssInlinerExtension::class)) { | ||
throw new \LogicException(sprintf('You cannot use "%s" if the CSS Inliner Twig extension is not available; try running "composer require twig/cssinliner-extra".', static::class)); | ||
} | ||
|
||
if (!class_exists(InkyExtension::class)) { | ||
throw new \LogicException(sprintf('You cannot use "%s" if the Inky Twig extension is not available; try running "composer require twig/inky-extra".', static::class)); | ||
} | ||
|
||
parent::__construct($headers, $body); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function markdown(string $content) | ||
{ | ||
if (!class_exists(MarkdownExtension::class)) { | ||
throw new \LogicException(sprintf('You cannot use "%s" if the Markdown Twig extension is not available; try running "composer require twig/markdown-extra".', __METHOD__)); | ||
} | ||
|
||
$this->context['markdown'] = true; | ||
|
||
return $this->content($content); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function content(string $content, bool $raw = false) | ||
{ | ||
$this->context['content'] = $content; | ||
$this->context['raw'] = $raw; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function action(string $text, string $url) | ||
{ | ||
$this->context['action_text'] = $text; | ||
$this->context['action_url'] = $url; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return self | ||
*/ | ||
public function importance(string $importance) | ||
{ | ||
$this->context['importance'] = $importance; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param \Throwable|FlattenException | ||
* | ||
* @return $this | ||
*/ | ||
public function exception($exception) | ||
{ | ||
$exceptionAsString = $this->getExceptionAsString($exception); | ||
|
||
$this->context['exception'] = true; | ||
$this->attach($exceptionAsString, 'exception.txt', 'text/plain'); | ||
$this->importance(self::IMPORTANCE_URGENT); | ||
|
||
if (!$this->getSubject()) { | ||
$this->subject($exception->getMessage()); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function theme(string $theme) | ||
{ | ||
$this->theme = $theme; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTextTemplate(): ?string | ||
{ | ||
if ($template = parent::getTextTemplate()) { | ||
return $template; | ||
} | ||
|
||
return '@email/'.$this->theme.'/notification/body.txt.twig'; | ||
} | ||
|
||
public function getHtmlTemplate(): ?string | ||
{ | ||
if ($template = parent::getHtmlTemplate()) { | ||
return $template; | ||
} | ||
|
||
return '@email/'.$this->theme.'/notification/body.html.twig'; | ||
} | ||
|
||
public function getContext(): array | ||
{ | ||
return array_merge($this->context, parent::getContext()); | ||
} | ||
|
||
public function getPreparedHeaders(): Headers | ||
{ | ||
$headers = parent::getPreparedHeaders(); | ||
|
||
$importance = $this->context['importance'] ?? IMPORTANCE_LOW; | ||
$this->priority($this->determinePriority($importance)); | ||
$headers->setHeaderBody('Text', 'Subject', sprintf('[%s] %s', strtoupper($importance), $this->getSubject())); | ||
|
||
return $headers; | ||
} | ||
|
||
private function determinePriority(string $importance): int | ||
{ | ||
switch ($importance) { | ||
case self::IMPORTANCE_URGENT: | ||
return self::PRIORITY_HIGHEST; | ||
case self::IMPORTANCE_HIGH: | ||
return self::PRIORITY_HIGH; | ||
case self::IMPORTANCE_MEDIUM: | ||
return self::PRIORITY_NORMAL; | ||
case self::IMPORTANCE_LOW: | ||
default: | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self::PRIORITY_LOW; | ||
} | ||
} | ||
|
||
private function getExceptionAsString($exception): string | ||
{ | ||
if (class_exists(FlattenException::class)) { | ||
$exception = $exception instanceof FlattenException ? $exception : FlattenException::createFromThrowable($exception); | ||
|
||
return $exception->getAsString(); | ||
} | ||
|
||
$message = \get_class($exception); | ||
if ('' != $exception->getMessage()) { | ||
$message .= ': '.$exception->getMessage(); | ||
} | ||
|
||
$message .= ' in '.$exception->getFile().':'.$exception->getLine()."\n"; | ||
$message .= "Stack trace:\n".$exception->getTraceAsString()."\n\n"; | ||
|
||
return rtrim($message); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function __serialize(): array | ||
{ | ||
return [$this->context, parent::__serialize()]; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function __unserialize(array $data): void | ||
{ | ||
[$this->context, $parentData] = $data; | ||
|
||
parent::__unserialize($parentData); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Bridge/Twig/Resources/views/Email/default/notification/body.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends "@email/zurb_2/notification/body.html.twig" %} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Bridge/Twig/Resources/views/Email/default/notification/body.txt.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends "@email/zurb_2/notification/body.txt.twig" %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.