2016-10-02 15:22:17 +05:30
|
|
|
<?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) {
|
2017-11-26 07:14:41 +05:30
|
|
|
$this->_baseDomain = Yii::$app->urlManager->getHostInfo();
|
2016-10-02 15:22:17 +05:30
|
|
|
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
|
|
|
|
*/
|
2017-11-26 07:14:41 +05:30
|
|
|
public function getTemplate(string $templateName): TemplateBuilder {
|
2016-10-02 15:22:17 +05:30
|
|
|
return $this->renderer->getTemplate($templateName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TemplateBuilder $template
|
|
|
|
* @throws \Ely\Email\RendererException
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-11-26 07:14:41 +05:30
|
|
|
public function render(TemplateBuilder $template): string {
|
2016-10-02 15:22:17 +05:30
|
|
|
return $this->renderer->render($template);
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:14:41 +05:30
|
|
|
private function buildBasePath(): string {
|
2016-10-02 15:22:17 +05:30
|
|
|
return $this->_baseDomain . $this->basePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|