mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Removed ely/email-renderer package and implemented new emails renderer client [skip ci]
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
<?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->urlManager->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;
|
||||
}
|
||||
|
||||
}
|
||||
59
common/components/EmailsRenderer/Api.php
Normal file
59
common/components/EmailsRenderer/Api.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\components\EmailsRenderer;
|
||||
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
class Api {
|
||||
|
||||
private $baseUrl;
|
||||
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
private $client;
|
||||
|
||||
public function __construct(string $baseUrl) {
|
||||
$this->baseUrl = $baseUrl;
|
||||
}
|
||||
|
||||
public function setClient(ClientInterface $client): void {
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \common\components\EmailsRenderer\Request\TemplateRequest $request
|
||||
*
|
||||
* @return string
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getTemplate(Request\TemplateRequest $request): string {
|
||||
return $this->getClient()
|
||||
->request('GET', "/templates/{$request->getLocale()}/{$request->getName()}", [
|
||||
'query' => $request->getParams(),
|
||||
])
|
||||
->getBody()
|
||||
->getContents();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ClientInterface
|
||||
*/
|
||||
protected function getClient(): ClientInterface {
|
||||
if ($this->client === null) {
|
||||
$this->client = $this->createDefaultClient();
|
||||
}
|
||||
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
private function createDefaultClient(): ClientInterface {
|
||||
return new GuzzleClient([
|
||||
'timeout' => 5,
|
||||
'base_uri' => $this->baseUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
89
common/components/EmailsRenderer/Component.php
Normal file
89
common/components/EmailsRenderer/Component.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\components\EmailsRenderer;
|
||||
|
||||
use common\components\EmailsRenderer\Request\TemplateRequest;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\FileHelper;
|
||||
|
||||
/**
|
||||
* @property string $baseDomain
|
||||
*/
|
||||
class Component extends \yii\base\Component implements RendererInterface {
|
||||
|
||||
/**
|
||||
* @var string The address of the templates rendering service.
|
||||
*/
|
||||
public $serviceUrl;
|
||||
|
||||
/**
|
||||
* @var string базовый путь после хоста. Должен начинаться слешем и заканчиваться без него.
|
||||
* Например "/email-images"
|
||||
*/
|
||||
public $basePath = '';
|
||||
|
||||
/**
|
||||
* @var Api
|
||||
*/
|
||||
private $api;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $_baseDomain;
|
||||
|
||||
public function init(): void {
|
||||
parent::init();
|
||||
|
||||
if ($this->serviceUrl === null) {
|
||||
throw new InvalidConfigException('serviceUrl is required');
|
||||
}
|
||||
|
||||
if ($this->_baseDomain === null) {
|
||||
$this->_baseDomain = Yii::$app->urlManager->getHostInfo();
|
||||
if ($this->_baseDomain === null) {
|
||||
throw new InvalidConfigException('Cannot automatically obtain base domain');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setBaseDomain(string $baseDomain): void {
|
||||
$this->_baseDomain = $baseDomain;
|
||||
}
|
||||
|
||||
public function getBaseDomain(): string {
|
||||
return $this->_baseDomain;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templateName
|
||||
* @param string $locale
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function render(string $templateName, string $locale, array $params = []): string {
|
||||
$request = new TemplateRequest($templateName, $locale, ArrayHelper::merge($params, [
|
||||
'assetsHost' => $this->buildBasePath(),
|
||||
]));
|
||||
|
||||
return $this->getApi()->getTemplate($request);
|
||||
}
|
||||
|
||||
private function getApi(): Api {
|
||||
if ($this->api === null) {
|
||||
$this->api = new Api($this->serviceUrl);
|
||||
}
|
||||
|
||||
return $this->api;
|
||||
}
|
||||
|
||||
private function buildBasePath(): string {
|
||||
return FileHelper::normalizePath($this->_baseDomain . '/' . $this->basePath, '/');
|
||||
}
|
||||
|
||||
}
|
||||
10
common/components/EmailsRenderer/RendererInterface.php
Normal file
10
common/components/EmailsRenderer/RendererInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\components\EmailsRenderer;
|
||||
|
||||
interface RendererInterface {
|
||||
|
||||
public function render(string $templateName, string $locale, array $params = []): string;
|
||||
|
||||
}
|
||||
41
common/components/EmailsRenderer/Request/TemplateRequest.php
Normal file
41
common/components/EmailsRenderer/Request/TemplateRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\components\EmailsRenderer\Request;
|
||||
|
||||
class TemplateRequest {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $locale;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $params;
|
||||
|
||||
public function __construct(string $name, string $locale, array $params) {
|
||||
$this->name = $name;
|
||||
$this->locale = $locale;
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getLocale(): string {
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
public function getParams(): array {
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user