From a786de13667dacda7b6cc4975dcba2712990a49e Mon Sep 17 00:00:00 2001 From: Fred Carlsen Date: Sun, 26 May 2024 13:01:03 +0200 Subject: [PATCH] chore: code + settings fixes fixes #12 Signed-off-by: Fred Carlsen --- src/models/MJMLModel.php | 12 +++++- src/models/Settings.php | 34 ++++++++++++++--- src/services/MJMLService.php | 43 ++++++++++++---------- src/templates/settings.twig | 47 ++++++++++++++++-------- src/twigextensions/MJMLTwigExtension.php | 10 ++++- src/variables/MJMLVariable.php | 6 ++- 6 files changed, 106 insertions(+), 46 deletions(-) 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 @@

MJML API

-{{ forms.textField({ +{{ forms.autosuggestField({ label: 'API App ID', instructions: 'Enter the app id received by email', id: 'appId', name: 'appId', - value: settings['appId']}) + value: settings['appId'] ?? null, + suggestEnvVars: true, + errors: settings.getErrors('appId'), +}) }} -{{ forms.textField({ +{{ forms.autosuggestField({ label: 'API Secret Key', instructions: 'Enter the secret key received by email', id: 'secretKey', name: 'secretKey', - value: settings['secretKey']}) + value: settings['secretKey'] ?? null, + suggestEnvVars: true, + errors: settings.getErrors('secretKey'), +}) }}

MJML local CLI

-{{ forms.textField({ - label: 'Node path', - instructions: 'Enter the path to where the your version of Node is located, i.e. `/usr/local/bin/node`', +{{ forms.autosuggestField({ + label: 'Node.js Path', + instructions: 'Enter the path to where the your version of Node.js is located, i.e. `/usr/local/bin/node`', id: 'nodePath', name: 'nodePath', - value: settings['nodePath']}) + suggestEnvVars: true, + errors: settings.getErrors('nodePath'), + value: settings.nodePath, +}) }} -{{ forms.textField({ - label: 'MJML cli path', - instructions: 'Enter the path to where the MJML cli installed with npm is located, i.e. `/usr/local/bin/mjml`', +{{ forms.autosuggestField({ + label: 'MJML CLI PAth', + instructions: 'Enter the path to where the MJML CLI installed with npm is located, i.e. `/usr/local/bin/mjml`', id: 'mjmlCliPath', name: 'mjmlCliPath', - value: settings['mjmlCliPath']}) + suggestEnvVars: true, + errors: settings.getErrors('mjmlCliPath'), + value: settings.mjmlCliPath, +}) }} -{{ forms.textField({ - label: 'MJML cli config args', - instructions: 'Enter the (optional) cli config args, e.g. `--config.minify true`', +{{ forms.autosuggestField({ + label: 'MJML CLI Config Arguments', + instructions: 'Enter the (optional) MJML CLI config arguments, e.g. `--config.minify true`', id: 'mjmlCliConfigArgs', name: 'mjmlCliConfigArgs', - value: settings['mjmlCliConfigArgs']}) + suggestEnvVars: true, + errors: settings.getErrors('mjmlCliConfigArgs'), + value: settings['mjmlCliConfigArgs'] ?? null, +}) }} diff --git a/src/twigextensions/MJMLTwigExtension.php b/src/twigextensions/MJMLTwigExtension.php index 0858ab1..8f400a6 100644 --- a/src/twigextensions/MJMLTwigExtension.php +++ b/src/twigextensions/MJMLTwigExtension.php @@ -13,6 +13,8 @@ use superbig\mjml\MJML; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; +use yii\base\ErrorException; +use yii\base\Exception; /** * @author Superbig @@ -40,7 +42,7 @@ public function getFunctions(): array ]; } - public function mjml($html = null): ?string + public function mjml($html = null): ?\Twig\Markup { $result = MJML::$plugin->mjmlService->parse($html); @@ -51,7 +53,11 @@ public function mjml($html = null): ?string return $result->output(); } - public function mjmlCli($html = null): ?string + /** + * @throws ErrorException + * @throws Exception + */ + public function mjmlCli($html = null): ?\Twig\Markup { $result = MJML::$plugin->mjmlService->parseCli($html); diff --git a/src/variables/MJMLVariable.php b/src/variables/MJMLVariable.php index e12f860..4756cd2 100644 --- a/src/variables/MJMLVariable.php +++ b/src/variables/MJMLVariable.php @@ -14,6 +14,7 @@ use superbig\mjml\MJML; use superbig\mjml\models\MJMLModel; +use yii\base\Exception; /** * @author Superbig @@ -33,12 +34,13 @@ public function include(string $template = '', $variables = null, $renderMethod } /** - * @param null $html + * @param string|null $html * * @return MJMLModel|null * @throws \yii\base\ErrorException + * @throws Exception */ - public function parseCli($html = null) + public function parseCli($html = null): ?MJMLModel { return MJML::$plugin->mjmlService->parseCli($html); }