diff --git a/src/models/MJMLModel.php b/src/models/MJMLModel.php index a598f7a..578e5b7 100644 --- a/src/models/MJMLModel.php +++ b/src/models/MJMLModel.php @@ -22,17 +22,25 @@ class MJMLModel extends Model { public string $html; public string $mjml; - public string $mjmlVersion; public function output(): \Twig\Markup { return Template::raw($this->html); } + /** + * @param array{html: string, mjml: string} $results + * @return MJMLModel + */ + public static function create(array $results): MJMLModel + { + return new self($results); + } + /** * @inheritdoc */ - public function rules(): array + public function defineRules(): array { return [ [['html', 'mjml'], 'string'], diff --git a/src/models/Settings.php b/src/models/Settings.php index 2277da6..cc18d96 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -13,7 +13,7 @@ use Craft; use craft\base\Model; -use superbig\mjml\MJML; +use craft\behaviors\EnvAttributeParserBehavior; /** * @author Superbig @@ -36,16 +36,40 @@ public function init(): void parent::init(); } - public function behaviors() + public function defineBehaviors(): array { - + return [ + 'parser' => [ + 'class' => EnvAttributeParserBehavior::class, + 'attributes' => [ + 'nodePath', + 'mjmlCliPath', + 'mjmlCliConfigArgs', + 'mjmlCliIncludesPath', + 'appId', + 'secretKey', + ], + ], + ]; } - public function rules(): array + public function defineRules(): array { return [ [['appId', 'secretKey', 'mjmlCliPath', 'nodePath', 'mjmlCliConfigArgs'], 'string'], - [['appId', 'secretKey'], 'required'], + [['appId', 'secretKey'], 'required', 'when' => fn(Settings $model) => !empty($model->appId) || !empty($model->secretKey)], + [['mjmlCliPath', 'nodePath'], 'required', 'when' => fn(Settings $model) => !empty($model->mjmlCliPath) || !empty($model->nodePath)], + ]; + } + + public function attributeLabels() + { + return [ + 'appId' => Craft::t('mjml', 'API App ID'), + 'secretKey' => Craft::t('mjml', 'API Secret Key'), + 'mjmlCliPath' => Craft::t('mjml', 'MJML Cli Path'), + 'nodePath' => Craft::t('mjml', 'Node.js Path'), + 'mjmlCliConfigArgs' => Craft::t('mjml', 'MJML Cli Config Arguments'), ]; } } diff --git a/src/services/MJMLService.php b/src/services/MJMLService.php index 32beeca..1edff75 100644 --- a/src/services/MJMLService.php +++ b/src/services/MJMLService.php @@ -12,6 +12,7 @@ use Craft; use craft\base\Component; +use craft\helpers\App; use craft\helpers\FileHelper; use craft\helpers\Json; use craft\web\View; @@ -21,6 +22,8 @@ use superbig\mjml\exceptions\MJMLException; use superbig\mjml\MJML; use superbig\mjml\models\MJMLModel; +use yii\base\ErrorException; +use yii\base\Exception; /** * @author Superbig @@ -29,21 +32,13 @@ */ class MJMLService extends Component { - // Public Methods - // ========================================================================= - - /** - * @param $html - * - * @return null|MJMLModel - */ - public function parse($html) + public function parse(string $html): ?MJMLModel { $settings = MJML::$plugin->getSettings(); $hash = md5($html); $client = new Client([ 'base_uri' => 'https://api.mjml.io/v1/', - 'auth' => [$settings->appId, $settings->secretKey], + 'auth' => [App::parseEnv($settings->appId), App::parseEnv($settings->secretKey)], ]); try { @@ -57,7 +52,7 @@ public function parse($html) return Json::decodeIfJson((string)$request->getBody()); }); - return new MJMLModel([ + return MJMLModel::create([ 'html' => $response['html'], 'mjml' => $response['mjml'], ]); @@ -84,7 +79,13 @@ public function include(string $template = '', $variables = [], $renderMethod = } $html = file_get_contents($templatePath); + + if (empty($html)) { + throw new MJMLException('Could not render template ' . $template . ' : The template was empty'); + } + $hash = md5($html); + /** @var MJMLModel|null $output */ $output = Craft::$app->getCache()->getOrSet("mjml-{$hash}-{$renderMethod}", function() use ($html, $renderMethod) { return $renderMethod === 'cli' ? $this->parseCli($html) : $this->parse($html); @@ -101,21 +102,25 @@ public function include(string $template = '', $variables = [], $renderMethod = } /** - * @param null $html + * @param string|null $html * * @return MJMLModel|null - * @throws \yii\base\ErrorException + * @throws ErrorException + * @throws Exception */ - public function parseCli($html = null) + public function parseCli(?string $html = null): ?MJMLModel { $settings = MJML::$plugin->getSettings(); - $configArgs = "{$settings->mjmlCliConfigArgs}"; + $configArgs = App::parseEnv($settings->mjmlCliConfigArgs); - if (!empty($settings->mjmlCliIncludesPath)) { - $configArgs = "{$configArgs} --config.filePath {$settings->mjmlCliIncludesPath}"; + $mjmlCliIncludesPath = App::parseEnv($settings->mjmlCliIncludesPath); + if (!empty($mjmlCliIncludesPath)) { + $configArgs = "{$configArgs} --config.filePath {$mjmlCliIncludesPath}"; } - $mjmlPath = "{$settings->nodePath} {$settings->mjmlCliPath}"; + $nodePath = App::parseEnv($settings->nodePath); + $mjmlCliPath = App::parseEnv($settings->mjmlCliPath); + $mjmlPath = "{$nodePath} {$mjmlCliPath}"; $hash = md5($html); $tempPath = Craft::$app->getPath()->getTempPath() . "/mjml/mjml-{$hash}.html"; $tempOutputPath = Craft::$app->getPath()->getTempPath() . "/mjml/mjml-output-{$hash}.html"; @@ -146,7 +151,7 @@ public function parseCli($html = null) return null; } - return new MJMLModel([ + return MJMLModel::create([ 'html' => $output, 'mjml' => $html, ]); diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 69fc0f3..024e9a6 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -17,44 +17,59 @@