Обновлена версия Email Renderer

Добавлен компонент для настройки Email Renderer
Добавлен роут в nginx для отображения картинок из Email Renderer
This commit is contained in:
ErickSkrauch 2016-10-02 12:52:17 +03:00
parent 97e8e0d48e
commit b67a1879fe
6 changed files with 100 additions and 15 deletions

View File

@ -12,7 +12,6 @@ use common\models\EmailActivation;
use common\models\UsernameHistory;
use common\validators\LanguageValidator;
use common\validators\PasswordValidate;
use Ely\Email\Renderer;
use Exception;
use Ramsey\Uuid\Uuid;
use Yii;
@ -141,7 +140,7 @@ class RegistrationForm extends ApiForm {
throw new InvalidConfigException('Please specify fromEmail app in app params');
}
$htmlBody = (new Renderer())->getTemplate('register')
$htmlBody = Yii::$app->emailRenderer->getTemplate('register')
->setLocale($account->lang)
->setParams([
'username' => $account->username,

View File

@ -20,6 +20,7 @@ class Yii extends \yii\BaseYii {
* @property \yii\redis\Connection $redis
* @property \common\components\RabbitMQ\Component $amqp
* @property \GuzzleHttp\Client $guzzle
* @property \common\components\EmailRenderer $emailRenderer
*/
abstract class BaseApplication extends yii\base\Application {
}

View File

@ -0,0 +1,71 @@
<?php
namespace common\components;
use Ely\Email\Renderer;
use Ely\Email\TemplateBuilder;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class EmailRenderer extends Component {
/**
* @var string базовый путь после хоста. Должен начинаться слешем и заканчиваться без него.
* Например "/email-images"
*/
public $basePath = '';
/**
* @var Renderer
*/
private $renderer;
/**
* @var string
*/
private $_baseDomain;
public function __construct(array $config = []) {
parent::__construct($config);
if ($this->_baseDomain === null) {
$this->_baseDomain = Yii::$app->request->getHostInfo();
if ($this->_baseDomain === null) {
throw new InvalidConfigException('Cannot automatically obtain base domain');
}
}
$this->renderer = new Renderer($this->buildBasePath());
}
public function setBaseDomain(string $baseDomain) {
$this->_baseDomain = $baseDomain;
$this->renderer->setBaseDomain($this->buildBasePath());
}
public function getBaseDomain() : string {
return $this->_baseDomain;
}
/**
* @param string $templateName
* @return TemplateBuilder
*/
public function getTemplate(string $templateName) : TemplateBuilder {
return $this->renderer->getTemplate($templateName);
}
/**
* @param TemplateBuilder $template
* @throws \Ely\Email\RendererException
* @return string
*/
public function render(TemplateBuilder $template) : string {
return $this->renderer->render($template);
}
private function buildBasePath() : string {
return $this->_baseDomain . $this->basePath;
}
}

View File

@ -41,6 +41,10 @@ return [
'guzzle' => [
'class' => GuzzleHttp\Client::class,
],
'emailRenderer' => [
'class' => common\components\EmailRenderer::class,
'basePath' => '/images/emails',
],
],
'aliases' => [
'@bower' => '@vendor/bower-asset',

View File

@ -25,7 +25,7 @@
"ely/yii2-tempmail-validator": "~1.0.0",
"emarref/jwt": "~1.0.3",
"ely/amqp-controller": "^0.1.0",
"ely/email-renderer": "dev-master#4a751652b5a325d44d3bc79a464dda9232486cbc"
"ely/email-renderer": "dev-master#8c975737c6681af4bbd161ff27fe6326d39ba9d6"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",

View File

@ -1,21 +1,21 @@
server {
listen 80;
set $root_path '/var/www/html';
set $api_path '${root_path}/api/web';
set $frontend_path '${root_path}/frontend/dist';
root $root_path;
charset utf-8;
index index.html;
etag on;
root $root_path;
charset utf-8;
client_max_body_size 2M;
etag on;
# Это можно раскоментить для целей отладки
# rewrite_log on;
# error_log /var/log/nginx/error.log debug;
set $root_path '/var/www/html';
set $frontend_path '${root_path}/frontend/dist';
set $request_url $request_uri;
set $host_with_uri '${host}${request_uri}';
rewrite_log on;
error_log /var/log/nginx/error.log debug;
if ($host_with_uri ~ '^${AUTHSERVER_HOST}/auth') {
set $request_url '/api/authserver${request_uri}';
rewrite ^/auth /api/authserver$uri last;
@ -33,7 +33,6 @@ server {
location / {
alias $frontend_path;
index index.html;
try_files $uri /index.html =404;
}
@ -57,9 +56,20 @@ server {
}
# Раздача статики для frontend с указанием max-кэша. Сброс будет по #hash после ребилда webpackом
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|json|css|zip|rar|eot|ttf|woff|ico) {
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|json|css|zip|rar|eot|ttf|woff|ico)$ {
root $frontend_path;
expires max;
etag off;
access_log off;
}
# Запросы к статике для email, их нужно запустить внутрь vendor
location ^~ /images/emails/assets {
rewrite ^/images/emails/assets/(.+)$ /vendor/ely/emails-renderer/dist/assets/$1 last;
}
location ^~ /vendor/ely/emails-renderer/dist/assets {
alias '${root_path}/vendor/ely/email-renderer/dist/assets';
try_files $uri =404;
}
}