mjml-typo3/Classes/View/MjmlBasedView.php

53 lines
1.6 KiB
PHP
Raw Normal View History

2017-12-14 14:00:53 +01:00
<?php
namespace Saccas\Mjml\View;
use TYPO3\CMS\Core\Utility\GeneralUtility;
2017-12-14 19:08:22 +01:00
use TYPO3\CMS\Extbase\Mvc\Cli\Command;
2017-12-14 14:00:53 +01:00
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Core\Utility\CommandUtility;
class MjmlBasedView extends StandaloneView
{
2017-12-14 19:08:22 +01:00
public function render($actionName = null)
2017-12-14 14:00:53 +01:00
{
2017-12-14 19:08:22 +01:00
return $this->getHtmlFromMjml(parent::render($actionName));
2017-12-14 14:00:53 +01:00
}
protected function getHtmlFromMjml($mjml)
{
$configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['mjml']);
$temporaryMjmlFileWithPath = GeneralUtility::tempnam('mjml_', '.mjml');
2017-12-14 19:08:22 +01:00
GeneralUtility::writeFileToTypo3tempDir($temporaryMjmlFileWithPath, $mjml);
2017-12-14 14:00:53 +01:00
// see https://mjml.io/download and https://www.npmjs.com/package/mjml-cli
2017-12-14 19:08:22 +01:00
$cmd = $configuration['nodeBinaryPath'] . ' ' . $configuration['mjmlBinaryPath'] . $configuration['mjmlBinary'];
$args = $configuration['mjmlParams'] . ' ' . $temporaryMjmlFileWithPath;
2017-12-14 14:00:53 +01:00
$result = [];
$returnValue = '';
2017-12-14 19:08:22 +01:00
CommandUtility::exec($this->getEscapedCommand($cmd, $args), $result, $returnValue);
GeneralUtility::unlink_tempfile($temporaryMjmlFileWithPath);
2017-12-14 14:00:53 +01:00
return implode('',$result);
}
2017-12-14 19:08:22 +01:00
/**
* @param string $cmd
* @param string $args
* @return string
*/
private function getEscapedCommand(string $cmd, string $args) {
$escapedCmd = escapeshellcmd($cmd);
$argsArray = explode(' ', $args);
$escapedArgsArray = CommandUtility::escapeShellArguments($argsArray);
$escapedArgs = implode(' ', $escapedArgsArray);
return $escapedCmd . ' ' . $escapedArgs;
}
2017-12-14 14:00:53 +01:00
}