2017-12-14 23:06:08 +01:00
|
|
|
<?php
|
|
|
|
namespace Saccas\Mjml\Domain\Renderer;
|
|
|
|
|
2021-04-12 17:19:22 +02:00
|
|
|
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
|
2017-12-14 23:06:08 +01:00
|
|
|
use TYPO3\CMS\Core\Utility\CommandUtility;
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
2018-01-19 16:08:14 +01:00
|
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
|
2017-12-14 23:06:08 +01:00
|
|
|
|
|
|
|
class Command implements RendererInterface
|
|
|
|
{
|
2022-09-15 15:58:08 +02:00
|
|
|
private ExtensionConfiguration $extensionConfiguration;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ExtensionConfiguration $extensionConfiguration
|
|
|
|
) {
|
|
|
|
$this->extensionConfiguration = $extensionConfiguration;
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:15:06 +01:00
|
|
|
public function getHtmlFromMjml($mjml): string
|
2017-12-14 23:06:08 +01:00
|
|
|
{
|
2022-09-15 15:58:08 +02:00
|
|
|
$conf = $this->extensionConfiguration->get('mjml');
|
2017-12-14 23:06:08 +01:00
|
|
|
|
|
|
|
$temporaryMjmlFileWithPath = GeneralUtility::tempnam('mjml_', '.mjml');
|
|
|
|
|
|
|
|
GeneralUtility::writeFileToTypo3tempDir($temporaryMjmlFileWithPath, $mjml);
|
|
|
|
|
2018-06-13 10:28:05 +02:00
|
|
|
$mjmlExtPath = ExtensionManagementUtility::extPath('mjml');
|
|
|
|
|
2017-12-14 23:06:08 +01:00
|
|
|
// see https://mjml.io/download and https://www.npmjs.com/package/mjml-cli
|
2018-06-13 10:28:05 +02:00
|
|
|
$cmd = $conf['nodeBinaryPath'] . ' ' . $mjmlExtPath . $conf['mjmlBinaryPath'] . $conf['mjmlBinary'];
|
|
|
|
$args = $temporaryMjmlFileWithPath . ' ' . $conf['mjmlParams'];
|
2017-12-14 23:06:08 +01:00
|
|
|
|
|
|
|
$result = [];
|
|
|
|
$returnValue = '';
|
2018-01-19 16:08:14 +01:00
|
|
|
|
2017-12-14 23:06:08 +01:00
|
|
|
CommandUtility::exec($this->getEscapedCommand($cmd, $args), $result, $returnValue);
|
|
|
|
|
|
|
|
GeneralUtility::unlink_tempfile($temporaryMjmlFileWithPath);
|
|
|
|
|
|
|
|
return implode('', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cmd
|
|
|
|
* @param string $args
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-01-26 10:15:06 +01:00
|
|
|
protected function getEscapedCommand(string $cmd, string $args): string
|
2017-12-14 23:06:08 +01:00
|
|
|
{
|
|
|
|
$escapedCmd = escapeshellcmd($cmd);
|
|
|
|
|
|
|
|
$argsArray = explode(' ', $args);
|
|
|
|
$escapedArgsArray = CommandUtility::escapeShellArguments($argsArray);
|
|
|
|
$escapedArgs = implode(' ', $escapedArgsArray);
|
|
|
|
|
|
|
|
return $escapedCmd . ' ' . $escapedArgs;
|
|
|
|
}
|
|
|
|
}
|