From b67a1879fef85830e0e7781efd890cc1d7c16f0f Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 2 Oct 2016 12:52:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20Email=20R?= =?UTF-8?q?enderer=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA?= =?UTF-8?q?=D0=B8=20Email=20Renderer=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D1=80=D0=BE=D1=83=D1=82=20=D0=B2=20nginx?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=B0=D1=80=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=BE=D0=BA=20=D0=B8=D0=B7=20Email=20Renderer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../authentication/RegistrationForm.php | 3 +- autocompletion.php | 1 + common/components/EmailRenderer.php | 71 +++++++++++++++++++ common/config/config.php | 4 ++ composer.json | 2 +- docker/nginx/account.ely.by.conf.template | 34 +++++---- 6 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 common/components/EmailRenderer.php diff --git a/api/models/authentication/RegistrationForm.php b/api/models/authentication/RegistrationForm.php index 1dfc3ea..cffb9dc 100644 --- a/api/models/authentication/RegistrationForm.php +++ b/api/models/authentication/RegistrationForm.php @@ -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, diff --git a/autocompletion.php b/autocompletion.php index 68407f1..24c5140 100644 --- a/autocompletion.php +++ b/autocompletion.php @@ -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 { } diff --git a/common/components/EmailRenderer.php b/common/components/EmailRenderer.php new file mode 100644 index 0000000..3c493ca --- /dev/null +++ b/common/components/EmailRenderer.php @@ -0,0 +1,71 @@ +_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; + } + +} diff --git a/common/config/config.php b/common/config/config.php index 92f07f9..31b0a83 100644 --- a/common/config/config.php +++ b/common/config/config.php @@ -41,6 +41,10 @@ return [ 'guzzle' => [ 'class' => GuzzleHttp\Client::class, ], + 'emailRenderer' => [ + 'class' => common\components\EmailRenderer::class, + 'basePath' => '/images/emails', + ], ], 'aliases' => [ '@bower' => '@vendor/bower-asset', diff --git a/composer.json b/composer.json index 429f227..1d7c064 100644 --- a/composer.json +++ b/composer.json @@ -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": "*", diff --git a/docker/nginx/account.ely.by.conf.template b/docker/nginx/account.ely.by.conf.template index 03a4522..ae9653f 100644 --- a/docker/nginx/account.ely.by.conf.template +++ b/docker/nginx/account.ely.by.conf.template @@ -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; + } }