Skip to content

Commit

Permalink
chore: code + settings fixes
Browse files Browse the repository at this point in the history
fixes #12

Signed-off-by: Fred Carlsen <[email protected]>
  • Loading branch information
sjelfull committed May 26, 2024
1 parent 44ec51e commit a786de1
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 46 deletions.
12 changes: 10 additions & 2 deletions src/models/MJMLModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
34 changes: 29 additions & 5 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Craft;

use craft\base\Model;
use superbig\mjml\MJML;
use craft\behaviors\EnvAttributeParserBehavior;

/**
* @author Superbig
Expand All @@ -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'),
];
}
}
43 changes: 24 additions & 19 deletions src/services/MJMLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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'],
]);
Expand All @@ -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);
Expand All @@ -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";
Expand Down Expand Up @@ -146,7 +151,7 @@ public function parseCli($html = null)
return null;
}

return new MJMLModel([
return MJMLModel::create([
'html' => $output,
'mjml' => $html,
]);
Expand Down
47 changes: 31 additions & 16 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,59 @@

<h2>MJML API</h2>

{{ 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'),
})
}}

<h2>MJML local CLI</h2>

{{ 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,
})
}}
10 changes: 8 additions & 2 deletions src/twigextensions/MJMLTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions src/variables/MJMLVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use superbig\mjml\MJML;

use superbig\mjml\models\MJMLModel;
use yii\base\Exception;

/**
* @author Superbig
Expand All @@ -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);
}
Expand Down

0 comments on commit a786de1

Please sign in to comment.